Skip to content

Commit 8715547

Browse files
fix: compile struct tuple patterns inside array patterns (#5363)
Co-authored-by: Mangel Maxime <me@mangelmaxime.fr>
1 parent 5b557db commit 8715547

19 files changed

Lines changed: 270 additions & 5 deletions

‎src/Fable.Transforms/FSharp2Fable.Util.fs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,15 @@ module Patterns =
12051205
| AddressOf value -> value
12061206
| _ -> expr
12071207

1208+
[<return: Struct>]
1209+
let (|ArrayElementAddress|_|) (expr: FSharpExpr) =
1210+
match expr with
1211+
| ILAsm(asmCode, [ elementType ], [ arrayExpr; indexExpr ]) when
1212+
asmCode.StartsWith("[I_ldelema", StringComparison.Ordinal)
1213+
->
1214+
ValueSome(elementType, arrayExpr, indexExpr)
1215+
| _ -> ValueNone
1216+
12081217
[<return: Struct>]
12091218
let (|TypeDefinition|_|) (NonAbbreviatedType t) =
12101219
if t.HasTypeDefinition then

‎src/Fable.Transforms/FSharp2Fable.fs‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ let private transformExprList com ctx xs =
2121
let private transformExprOpt com ctx opt =
2222
trampolineOptionMap (transformExpr com ctx []) opt
2323

24+
let private transformAddressedValue (com: IFableCompiler) (ctx: Context) (fsExpr: FSharpExpr) =
25+
trampoline {
26+
match fsExpr with
27+
| ArrayElementAddress(elementType, arrayExpr, indexExpr) ->
28+
let! arrayExpr = transformExpr com ctx [] arrayExpr
29+
let! indexExpr = transformExpr com ctx [] indexExpr
30+
let typ = makeType ctx.GenericArgs elementType
31+
return Replacements.Api.getArrayElement com ctx (makeRangeFrom fsExpr) typ arrayExpr indexExpr
32+
| _ -> return! transformExpr com ctx [] fsExpr
33+
}
34+
2435
let private transformBaseConsCall
2536
com
2637
ctx
@@ -588,7 +599,7 @@ let private transformUnionCaseTest
588599
(unionCase: FSharpUnionCase)
589600
=
590601
trampoline {
591-
let! unionExpr = transformExpr com ctx [] unionExpr
602+
let! unionExpr = transformAddressedValue com ctx unionExpr
592603
let unionPattern = getUnionPattern fsType unionCase
593604

594605
match unionPattern with
@@ -1156,6 +1167,11 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) appliedGenArgs fs
11561167
"This explicit constructor uses a self identifier (`new() as x`), which Fable only supports on JavaScript, TypeScript and Python, in a class with no base class and without [<AttachMembers>]."
11571168
|> addErrorAndReturnNull com ctx.InlinePath r
11581169

1170+
| ArrayElementAddress _ ->
1171+
return
1172+
"Taking the address of an array element is not supported"
1173+
|> addErrorAndReturnNull com ctx.InlinePath (makeRangeFrom fsExpr)
1174+
11591175
| FSharpExprPatterns.Value var ->
11601176
let r = makeRangeFrom fsExpr
11611177

@@ -1619,7 +1635,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) appliedGenArgs fs
16191635
| IgnoreAddressOf inner when com.Options.Language <> Rust -> inner
16201636
| _ -> callee
16211637

1622-
let! transformedCallee = transformExpr com ctx [] callee
1638+
let! transformedCallee = transformAddressedValue com ctx callee
16231639
let callee = derefByRefCallee com ctx r callee calleeType transformedCallee
16241640

16251641
let fieldName = calleeType.AnonRecordTypeDetails.SortedFieldNames[fieldIndex]
@@ -1679,7 +1695,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) appliedGenArgs fs
16791695
match tupleElemValue with
16801696
| Some(Fable.IdentExpr id as e) when not id.IsMutable -> return e
16811697
| _ ->
1682-
let! tupleExpr = transformExpr com ctx [] tupleExpr
1698+
let! tupleExpr = transformAddressedValue com ctx tupleExpr
16831699
// let typ = makeType ctx.GenericArgs fsExpr.Type // Doesn't always work
16841700
let typ =
16851701
Seq.item tupleElemIndex tupleType.GenericArguments |> makeType ctx.GenericArgs
@@ -1690,7 +1706,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) appliedGenArgs fs
16901706
let r = makeRangeFrom fsExpr
16911707
// let fieldType = makeType ctx.GenericArgs fsExpr.Type // Doesn't always work
16921708
let fieldType = resolveFieldType ctx unionType field.FieldType
1693-
let! unionExpr = transformExpr com ctx [] unionExpr
1709+
let! unionExpr = transformAddressedValue com ctx unionExpr
16941710

16951711
match getUnionPattern unionType unionCase with
16961712
| ErasedUnionCase ->

‎src/Fable.Transforms/Replacements.Api.fs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,19 @@ let makeRefFromMutableField (com: ICompiler) ctx r t (value: Expr) =
146146
| Dart -> Dart.Replacements.makeRefFromMutableField com ctx r t value
147147
| Beam -> Beam.Replacements.makeRefFromMutableField com ctx r t value
148148
| _ -> JS.Replacements.makeRefFromMutableField com ctx r t value
149+
150+
let getArrayElement (com: ICompiler) ctx r t (arrayExpr: Expr) (indexExpr: Expr) =
151+
let info: ReplaceCallInfo =
152+
{
153+
CompiledName = "GetArray"
154+
OverloadSuffix = ""
155+
SignatureArgTypes = [ arrayExpr.Type; indexExpr.Type ]
156+
HasSpread = false
157+
IsModuleValue = false
158+
IsInterface = false
159+
DeclaringEntityFullName = "Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions"
160+
GenericArgs = [ t ]
161+
}
162+
163+
tryCall com ctx r t info None [ arrayExpr; indexExpr ]
164+
|> Option.defaultWith (fun () -> AST.getExpr r t arrayExpr indexExpr)

‎tests/Beam/RecordTests.fs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,8 @@ let ``test copy-and-update of a let mutable anonymous struct record keeps the un
398398
let ``test field reads through a byref to an anonymous struct record work`` () =
399399
let mutable a = struct {| AnX = 1; AnY = 2 |}
400400
sumAnonymousStructRecordByRef &a |> equal 3
401+
402+
[<Fact>]
403+
let ``test Field reads of an anonymous struct record array element work`` () =
404+
let sumFirst (xs: struct {| X: int; Y: int |} array) = xs.[0].X + xs.[0].Y
405+
sumFirst [| struct {| X = 1; Y = 2 |} |] |> equal 3

‎tests/Beam/TupleTests.fs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,28 @@ let ``test Can create tuples of single element`` () =
123123
let t1b = Tuple.Create "b"
124124
t1.Item1 |> equal 4
125125
t1b.Item1 |> equal "b"
126+
127+
// A struct tuple pattern inside an array pattern reads the element through its address
128+
[<Fact>]
129+
let ``test Struct tuple patterns inside array patterns work`` () =
130+
let firstOfOnly (pairs: struct (int * string) array) =
131+
match pairs with
132+
| [| struct (n, _) |] -> n
133+
| [| struct (_, s); struct (n, _) |] -> n + s.Length
134+
| _ -> -1
135+
136+
firstOfOnly [| struct (4, "a") |] |> equal 4
137+
firstOfOnly [| struct (4, "abc"); struct (5, "d") |] |> equal 8
138+
firstOfOnly [||] |> equal -1
139+
140+
[<Fact>]
141+
let ``test Struct tuple patterns with a guard inside array patterns work`` () =
142+
let sumIfOrdered (pairs: struct (int * int) array) =
143+
match pairs with
144+
| [| struct (a, b) |] when a < b -> a + b
145+
| [| struct (a, _) |] -> a
146+
| _ -> -1
147+
148+
sumIfOrdered [| struct (1, 2) |] |> equal 3
149+
sumIfOrdered [| struct (5, 2) |] |> equal 5
150+
sumIfOrdered [||] |> equal -1

‎tests/Beam/UnionTypeTests.fs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,17 @@ let ``test Match with condition works`` () =
767767
let b2 = DeepWrappedB "not"
768768
b1 |> matchStringWhenStringNotHello |> equal "hello"
769769
b2 |> matchStringWhenStringNotHello |> equal "not hello"
770+
771+
[<Fact>]
772+
let ``test Struct union patterns inside array patterns work`` () =
773+
let describe (shapes: StructShape array) =
774+
match shapes with
775+
| [| StructShape.Square side |] -> side
776+
| [| StructShape.Circle radius |] -> int radius
777+
| [| _; StructShape.Square side |] -> side * 2
778+
| _ -> -1
779+
780+
describe [| StructShape.Square 3 |] |> equal 3
781+
describe [| StructShape.Circle 4.0 |] |> equal 4
782+
describe [| StructShape.Circle 4.0; StructShape.Square 5 |] |> equal 10
783+
describe [||] |> equal -1

‎tests/Dart/src/RecordTests.fs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,7 @@ let tests() =
218218
let mutable a = struct {| X = 1; Y = 2 |}
219219
let b = {| a with Y = 99 |}
220220
(b.X, b.Y) |> equal (1, 99)
221+
222+
testCase "Field reads of an anonymous struct record array element work" <| fun () ->
223+
let sumFirst (xs: struct {| X: int; Y: int |} array) = xs.[0].X + xs.[0].Y
224+
sumFirst [| struct {| X = 1; Y = 2 |} |] |> equal 3

‎tests/Dart/src/TupleTests.fs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,26 @@ let tests() =
4343
let t1b = Tuple.Create "b"
4444
t1.Item1 |> equal 4
4545
t1b.Item1 |> equal "b"
46+
47+
// A struct tuple pattern inside an array pattern reads the element through its address
48+
testCase "Struct tuple patterns inside array patterns work" <| fun () ->
49+
let firstOfOnly (pairs: struct (int * string) array) =
50+
match pairs with
51+
| [| struct (n, _) |] -> n
52+
| [| struct (_, s); struct (n, _) |] -> n + s.Length
53+
| _ -> -1
54+
55+
firstOfOnly [| struct (4, "a") |] |> equal 4
56+
firstOfOnly [| struct (4, "abc"); struct (5, "d") |] |> equal 8
57+
firstOfOnly [||] |> equal -1
58+
59+
testCase "Struct tuple patterns with a guard inside array patterns work" <| fun () ->
60+
let sumIfOrdered (pairs: struct (int * int) array) =
61+
match pairs with
62+
| [| struct (a, b) |] when a < b -> a + b
63+
| [| struct (a, _) |] -> a
64+
| _ -> -1
65+
66+
sumIfOrdered [| struct (1, 2) |] |> equal 3
67+
sumIfOrdered [| struct (5, 2) |] |> equal 5
68+
sumIfOrdered [||] |> equal -1

‎tests/Dart/src/UnionTests.fs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,16 @@ let tests() =
461461
let b2 = DeepWrappedB "not"
462462
b1 |> matchStringWhenStringNotHello |> equal "hello"
463463
b2 |> matchStringWhenStringNotHello |> equal "not hello"
464+
465+
testCase "Struct union patterns inside array patterns work" <| fun () ->
466+
let describe (shapes: StructShape array) =
467+
match shapes with
468+
| [| StructShape.Square side |] -> side
469+
| [| StructShape.Circle radius |] -> int radius
470+
| [| _; StructShape.Square side |] -> side * 2
471+
| _ -> -1
472+
473+
describe [| StructShape.Square 3 |] |> equal 3
474+
describe [| StructShape.Circle 4.0 |] |> equal 4
475+
describe [| StructShape.Circle 4.0; StructShape.Square 5 |] |> equal 10
476+
describe [||] |> equal -1

‎tests/Integration/Compiler/CompilerMessagesTests.fs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,4 +1140,15 @@ bigint.TryParse("1", NumberStyles.AllowBinarySpecifier, CultureInfo.InvariantCul
11401140
compile source
11411141
|> Assert.Code.noWarning "FABLE0103"
11421142
|> ignore
1143+
1144+
testCase "Taking the address of an array element results in error" <| fun _ ->
1145+
let source =
1146+
"""
1147+
let incr (x: byref<int>) = x <- x + 1
1148+
let xs = [| 1 |]
1149+
incr &xs.[0]
1150+
"""
1151+
compile source
1152+
|> Assert.Exists.errorWith "Taking the address of an array element is not supported"
1153+
|> ignore
11431154
]

0 commit comments

Comments
 (0)