Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP fix for #3496 #3505

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2223,12 +2223,13 @@ module Util =
// printfn "transformGet: %A" kind
// printfn "transformGet: %A" (fableExpr.Type)

match kind with
| Fable.ExprGet (Fable.Value(kind = Fable.StringConstant "length"))
| Fable.FieldGet { Name = "length" } ->
let inline lenFunctionCall () =
let func = Expression.name "len"
let left, stmts = com.TransformAsExpr(ctx, fableExpr)
Expression.call (func, [ left ]), stmts
match kind with
| Fable.ExprGet (Fable.Value(kind = Fable.StringConstant "length")) ->
lenFunctionCall ()
| Fable.FieldGet { Name = "message" } ->
let func = Expression.name "str"
let left, stmts = com.TransformAsExpr(ctx, fableExpr)
Expand All @@ -2243,25 +2244,29 @@ module Util =
expr, stmts @ stmts' @ stmts''

| Fable.FieldGet i ->
//printfn "Fable.FieldGet: %A" (fieldName, fableExpr.Type)
let fieldName = i.Name |> Naming.toSnakeCase // |> Helpers.clean

let fableExpr =
match fableExpr with
// If we're accessing a virtual member with default implementation (see #701)
// from base class, we can use `super` in JS so we don't need the bound this arg
| Fable.Value (Fable.BaseValue (_, t), r) -> Fable.Value(Fable.BaseValue(None, t), r)
| _ -> fableExpr

let expr, stmts = com.TransformAsExpr(ctx, fableExpr)

let subscript =
match fableExpr.Type with
| Fable.AnonymousRecordType _ -> true
| Fable.GenericParam (_, _, [ Fable.Constraint.HasMember (_, false) ]) -> true
| _ -> false
// printfn "Fable.FieldGet: %A" (fieldName, fableExpr.Type)
get com ctx range expr fieldName subscript, stmts
// see TestArray.fs/``test Array slice with lower index work`` and https://github.com/fable-compiler/Fable/issues/3496
match fableExpr.Type, i.Name with
| Fable.AST.Fable.Type.Array _, "length"->
lenFunctionCall ()
| _ ->
//printfn "Fable.FieldGet: %A" (fieldName, fableExpr.Type)
let fieldName = i.Name |> Naming.toSnakeCase // |> Helpers.clean
let fableExpr =
match fableExpr with
// If we're accessing a virtual member with default implementation (see #701)
// from base class, we can use `super` in JS so we don't need the bound this arg
| Fable.Value (Fable.BaseValue (_, t), r) -> Fable.Value(Fable.BaseValue(None, t), r)
| _ -> fableExpr

let expr, stmts = com.TransformAsExpr(ctx, fableExpr)

let subscript =
match fableExpr.Type with
| Fable.AnonymousRecordType _ -> true
| Fable.GenericParam (_, _, [ Fable.Constraint.HasMember (_, false) ]) -> true
| _ -> false
// printfn "Fable.FieldGet: %A" (fieldName, fableExpr.Type)
get com ctx range expr fieldName subscript, stmts

| Fable.ListHead ->
// get range (com.TransformAsExpr(ctx, fableExpr)) "head"
Expand Down
1 change: 1 addition & 0 deletions tests/Python/Fable.Tests.Python.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="TestUri.fs" />
<Compile Include="TestPyInterop.fs" />

<Compile Include="TestNonRegression.fs" />
<Compile Include="Main.fs" />
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions tests/Python/TestNonRegression.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Fable.Tests.NonRegression

open Util.Testing

module Issue3496 =
type Class(length: int) =
member x.Length = length
static member StaticLength (length: int) = length
let returnLength (length: int) = length


[<Fact>]
let testLengthPassedToCtorIsOk() =
let c = Class(1)
equal 1 c.Length
equal 1 (returnLength 1)
equal 1 (Class.StaticLength 1)