Skip to content

Commit 81b0286

Browse files
authored
[All] Fix missing HashSet implementations and tests (#4526)
1 parent e7a022c commit 81b0286

17 files changed

Lines changed: 1569 additions & 272 deletions

File tree

src/Fable.Cli/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
* [Beam] Fix `List.Cons` call replacement and test (by @ncave)
1313
* [Beam/Dart/Python/TypeScript] Fix `Array.Equals` to use reference equality instead of structural equality (by @ncave)
1414
* [Dart/Python/TypeScript/Rust] Fix `Seq.foldBack2` for sequences with different lengths (by @ncave)
15+
* [All] Fix missing `HashSet` implementations and tests (by @ncave)
1516
* [Rust] Fix `Array/HashMap/HashSet` internal representation (by @ncave)
16-
* [Rust] Fix missing `HashSet` implementations and tests (by @ncave)
17+
* [Rust] Fix F# classes reference equality semantics (by @ncave)
1718
* [Python] Fix missing `Array` module implementations and tests (by @ncave)
1819
* [Python] Fix object expressions implementing interfaces with `[<CLIEvent>]` members no longer produce unimplementable abstract Protocol members (fixes #3039)
1920
* [Python] Fix `DateTime.TryParse` incorrectly assigning `DateTimeKind.Local` to naive datetime strings (should be `DateTimeKind.Unspecified`) (fixes #3654)

src/Fable.Compiler/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
* [Beam] Fix `List.Cons` call replacement and test (by @ncave)
1313
* [Beam/Dart/Python/TypeScript] Fix `Array.Equals` to use reference equality instead of structural equality (by @ncave)
1414
* [Dart/Python/TypeScript/Rust] Fix `Seq.foldBack2` for sequences with different lengths (by @ncave)
15+
* [All] Fix missing `HashSet` implementations and tests (by @ncave)
1516
* [Rust] Fix `Array/HashMap/HashSet` internal representation (by @ncave)
16-
* [Rust] Fix missing `HashSet` implementations and tests (by @ncave)
17+
* [Rust] Fix F# classes reference equality semantics (by @ncave)
1718
* [Python] Fix missing `Array` module implementations and tests (by @ncave)
1819
* [Python] Fix object expressions implementing interfaces with `[<CLIEvent>]` members no longer produce unimplementable abstract Protocol members (fixes #3039)
1920
* [Python] Fix `DateTime.TryParse` incorrectly assigning `DateTimeKind.Local` to naive datetime strings (should be `DateTimeKind.Unspecified`) (fixes #3654)

src/Fable.Transforms/Beam/Replacements.fs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,6 +3563,9 @@ let private hashSets
35633563
| "ExceptWith", Some callee, [ other ] ->
35643564
Helper.LibCall(com, "fable_hashset", "except_with", t, [ callee; other ], ?loc = r)
35653565
|> Some
3566+
| "SymmetricExceptWith", Some callee, [ other ] ->
3567+
Helper.LibCall(com, "fable_hashset", "symmetric_except_with", t, [ callee; other ], ?loc = r)
3568+
|> Some
35663569
// IsSubsetOf / IsSupersetOf / IsProperSubsetOf / IsProperSupersetOf
35673570
| "IsSubsetOf", Some callee, [ other ] ->
35683571
Helper.LibCall(com, "fable_hashset", "is_subset_of", t, [ callee; other ], ?loc = r)
@@ -3576,10 +3579,25 @@ let private hashSets
35763579
| "IsProperSupersetOf", Some callee, [ other ] ->
35773580
Helper.LibCall(com, "fable_hashset", "is_proper_superset_of", t, [ callee; other ], ?loc = r)
35783581
|> Some
3579-
// CopyTo
3580-
| "CopyTo", Some callee, [ arr ] ->
3581-
Helper.LibCall(com, "fable_hashset", "copy_to", t, [ callee; arr ], ?loc = r)
3582+
| "Overlaps", Some callee, [ other ] ->
3583+
Helper.LibCall(com, "fable_hashset", "overlaps", t, [ callee; other ], ?loc = r)
3584+
|> Some
3585+
| "SetEquals", Some callee, [ other ] ->
3586+
Helper.LibCall(com, "fable_hashset", "set_equals", t, [ callee; other ], ?loc = r)
35823587
|> Some
3588+
// CopyTo
3589+
| "CopyTo", Some callee, args ->
3590+
match args with
3591+
| [ arr ] ->
3592+
Helper.LibCall(com, "fable_hashset", "copy_to", t, [ callee; arr ], ?loc = r)
3593+
|> Some
3594+
| [ arr; targetIndex ] ->
3595+
Helper.LibCall(com, "fable_hashset", "copy_to", t, [ callee; arr; targetIndex ], ?loc = r)
3596+
|> Some
3597+
| [ arr; targetIndex; count ] ->
3598+
Helper.LibCall(com, "fable_hashset", "copy_to", t, [ callee; arr; targetIndex; count ], ?loc = r)
3599+
|> Some
3600+
| _ -> None
35833601
// GetEnumerator
35843602
| "GetEnumerator", Some callee, _ ->
35853603
Helper.LibCall(com, "fable_hashset", "get_enumerator", t, [ callee ], ?loc = r)

src/Fable.Transforms/Dart/Replacements.fs

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,11 +2499,13 @@ let hashSets (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
24992499
match i.CompiledName, thisArg, args with
25002500
| ".ctor", _, _ ->
25012501
match i.SignatureArgTypes, args with
2502-
| [], _ -> Helper.GlobalCall("Set", t, [], genArgs = i.GenericArgs, ?loc = r) |> Some
2502+
| ([] | [ Number _ ]), _ -> Helper.GlobalCall("Set", t, [], genArgs = i.GenericArgs, ?loc = r) |> Some
25032503
| [ IEnumerable ], [ arg ] -> Helper.GlobalCall("Set", t, [ arg ], memb = "of", ?loc = r) |> Some
25042504
| [ IEnumerable; IEqualityComparer ], [ arg; eqComp ] ->
25052505
Helper.LibCall(com, "Types", "setWith", t, [ eqComp; arg ], ?loc = r) |> Some
2506-
| [ IEqualityComparer ], [ eqComp ] -> Helper.LibCall(com, "Types", "setWith", t, [ eqComp ], ?loc = r) |> Some
2506+
| [ IEqualityComparer ], [ eqComp ]
2507+
| [ Number _; IEqualityComparer ], [ _; eqComp ] ->
2508+
Helper.LibCall(com, "Types", "setWith", t, [ eqComp ], ?loc = r) |> Some
25072509
| _ -> None
25082510
// Const are read-only but I'm not sure how to detect this in runtime
25092511
// | "get_IsReadOnly", _, _ -> BoolConstant false |> makeValue r |> Some
@@ -2513,14 +2515,71 @@ let hashSets (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
25132515
let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst
25142516

25152517
Helper.InstanceCall(c, meth, t, args, i.SignatureArgTypes, ?loc = r) |> Some
2516-
// | ("IsProperSubsetOf" | "IsProperSupersetOf" | "IsSubsetOf" | "IsSupersetOf" as meth), Some c, args ->
2517-
// let meth = Naming.lowerFirst meth
2518-
// let args = injectArg com ctx r "Set" meth i.GenericArgs args
2519-
// Helper.LibCall(com, "Set", meth, t, c::args, ?loc=r) |> Some
2520-
// | "CopyTo" // TODO!!!
2521-
// | "SetEquals"
2522-
// | "Overlaps"
2523-
// | "SymmetricExceptWith"
2518+
| "UnionWith", Some c, [ other ] ->
2519+
Helper.LibCall(com, "Types", "hashSetUnionWith", t, [ c; other ], ?loc = r)
2520+
|> Some
2521+
| "IntersectWith", Some c, [ other ] ->
2522+
Helper.LibCall(com, "Types", "hashSetIntersectWith", t, [ c; other ], ?loc = r)
2523+
|> Some
2524+
| "ExceptWith", Some c, [ other ] ->
2525+
Helper.LibCall(com, "Types", "hashSetExceptWith", t, [ c; other ], ?loc = r)
2526+
|> Some
2527+
| "IsSubsetOf", Some c, [ other ] ->
2528+
Helper.LibCall(com, "Types", "hashSetIsSubsetOf", t, [ c; other ], ?loc = r)
2529+
|> Some
2530+
| "IsSupersetOf", Some c, [ other ] ->
2531+
Helper.LibCall(com, "Types", "hashSetIsSupersetOf", t, [ c; other ], ?loc = r)
2532+
|> Some
2533+
| "IsProperSubsetOf", Some c, [ other ] ->
2534+
Helper.LibCall(com, "Types", "hashSetIsProperSubsetOf", t, [ c; other ], ?loc = r)
2535+
|> Some
2536+
| "IsProperSupersetOf", Some c, [ other ] ->
2537+
Helper.LibCall(com, "Types", "hashSetIsProperSupersetOf", t, [ c; other ], ?loc = r)
2538+
|> Some
2539+
| "CopyTo", Some c, args ->
2540+
let count = getLength c
2541+
2542+
match args with
2543+
| [ target ] ->
2544+
Helper.LibCall(
2545+
com,
2546+
"Types",
2547+
"hashSetCopyToArray",
2548+
t,
2549+
[ c; target; makeIntConst 0; makeIntConst 0; count ],
2550+
?loc = r
2551+
)
2552+
|> Some
2553+
| [ target; targetIndex ] ->
2554+
Helper.LibCall(
2555+
com,
2556+
"Types",
2557+
"hashSetCopyToArray",
2558+
t,
2559+
[ c; target; makeIntConst 0; targetIndex; count ],
2560+
?loc = r
2561+
)
2562+
|> Some
2563+
| [ target; targetIndex; copyCount ] ->
2564+
Helper.LibCall(
2565+
com,
2566+
"Types",
2567+
"hashSetCopyToArray",
2568+
t,
2569+
[ c; target; makeIntConst 0; targetIndex; copyCount ],
2570+
?loc = r
2571+
)
2572+
|> Some
2573+
| _ -> None
2574+
| "SetEquals", Some c, [ other ] ->
2575+
Helper.LibCall(com, "Types", "hashSetSetEquals", t, [ c; other ], ?loc = r)
2576+
|> Some
2577+
| "Overlaps", Some c, [ other ] ->
2578+
Helper.LibCall(com, "Types", "hashSetOverlaps", t, [ c; other ], ?loc = r)
2579+
|> Some
2580+
| "SymmetricExceptWith", Some c, [ other ] ->
2581+
Helper.LibCall(com, "Types", "hashSetSymmetricExceptWith", t, [ c; other ], ?loc = r)
2582+
|> Some
25242583
| meth, Some c, args ->
25252584
match meth with
25262585
| "Add" -> Some "add"
@@ -4069,10 +4128,12 @@ let tryBaseConstructor com ctx (ent: EntityRef) (argTypes: Lazy<Type list>) genA
40694128
| Types.hashset ->
40704129
let args =
40714130
match argTypes.Value, args with
4072-
| [], _ -> [ makeArray Any []; makeEqualityComparer com ctx (Seq.head genArgs) ]
4131+
| ([] | [ Number _ ]), _ -> [ makeArray Any []; makeEqualityComparer com ctx (Seq.head genArgs) ]
40734132
| [ IEnumerable ], [ arg ] -> [ arg; makeEqualityComparer com ctx (Seq.head genArgs) ]
40744133
| [ IEnumerable; IEqualityComparer ], [ arg; eqComp ] -> [ arg; makeComparerFromEqualityComparer eqComp ]
4075-
| [ IEqualityComparer ], [ eqComp ] -> [ makeArray Any []; makeComparerFromEqualityComparer eqComp ]
4134+
| [ IEqualityComparer ], [ eqComp ]
4135+
| [ Number _; IEqualityComparer ], [ _; eqComp ] ->
4136+
[ makeArray Any []; makeComparerFromEqualityComparer eqComp ]
40764137
| _ -> FableError "Unexpected hashset constructor" |> raise
40774138

40784139
let entityName = FSharp2Fable.Helpers.cleanNameAsJsIdentifier "HashSet"

src/Fable.Transforms/Python/Replacements.fs

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,13 +2685,14 @@ let hashSets (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
26852685
match i.CompiledName, thisArg, args with
26862686
| ".ctor", _, _ ->
26872687
match i.SignatureArgTypes, args with
2688-
| [], _ -> makeHashSet com ctx r t (makeArray Any []) |> Some
2688+
| ([] | [ Number _ ]), _ -> makeHashSet com ctx r t (makeArray Any []) |> Some
26892689
| [ IEnumerable ], [ arg ] -> makeHashSet com ctx r t arg |> Some
26902690
| [ IEnumerable; IEqualityComparer ], [ arg; eqComp ] ->
26912691
makeComparerFromEqualityComparer eqComp
26922692
|> makeHashSetWithComparer com r t arg
26932693
|> Some
2694-
| [ IEqualityComparer ], [ eqComp ] ->
2694+
| [ IEqualityComparer ], [ eqComp ]
2695+
| [ Number _; IEqualityComparer ], [ _; eqComp ] ->
26952696
makeComparerFromEqualityComparer eqComp
26962697
|> makeHashSetWithComparer com r t (makeArray Any [])
26972698
|> Some
@@ -2714,15 +2715,62 @@ let hashSets (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
27142715
| "ExceptWith", Some c, [ arg ] ->
27152716
Helper.LibCall(com, "map_util", "exceptWithSet", t, [ c; arg ], ?loc = r)
27162717
|> Some
2717-
| ("IsProperSubsetOf" | "IsProperSupersetOf" | "IsSubsetOf" | "IsSupersetOf" as meth), Some c, args ->
2718-
let meth = Naming.lowerFirst meth
2719-
let args = injectArg com ctx r "Set" meth i.GenericArgs args
2718+
| "IsProperSubsetOf", Some c, [ arg ] ->
2719+
Helper.LibCall(com, "map_util", "isProperSubsetOfSet", t, [ c; arg ], ?loc = r)
2720+
|> Some
2721+
| "IsProperSupersetOf", Some c, [ arg ] ->
2722+
Helper.LibCall(com, "map_util", "isProperSupersetOfSet", t, [ c; arg ], ?loc = r)
2723+
|> Some
2724+
| "IsSubsetOf", Some c, [ arg ] ->
2725+
Helper.LibCall(com, "map_util", "isSubsetOfSet", t, [ c; arg ], ?loc = r)
2726+
|> Some
2727+
| "IsSupersetOf", Some c, [ arg ] ->
2728+
Helper.LibCall(com, "map_util", "isSupersetOfSet", t, [ c; arg ], ?loc = r)
2729+
|> Some
2730+
| "CopyTo", Some c, args ->
2731+
let count = Helper.GlobalCall("len", Int32.Number, [ c ], ?loc = r)
27202732

2721-
Helper.LibCall(com, "Set", meth, t, c :: args, ?loc = r) |> Some
2722-
// | "CopyTo" // TODO!!!
2723-
// | "SetEquals"
2724-
// | "Overlaps"
2725-
// | "SymmetricExceptWith"
2733+
match args with
2734+
| [ target ] ->
2735+
Helper.LibCall(
2736+
com,
2737+
"map_util",
2738+
"copyToArrayFromSet",
2739+
t,
2740+
[ c; target; makeIntConst 0; makeIntConst 0; count ],
2741+
?loc = r
2742+
)
2743+
|> Some
2744+
| [ target; targetIndex ] ->
2745+
Helper.LibCall(
2746+
com,
2747+
"map_util",
2748+
"copyToArrayFromSet",
2749+
t,
2750+
[ c; target; makeIntConst 0; targetIndex; count ],
2751+
?loc = r
2752+
)
2753+
|> Some
2754+
| [ target; targetIndex; copyCount ] ->
2755+
Helper.LibCall(
2756+
com,
2757+
"map_util",
2758+
"copyToArrayFromSet",
2759+
t,
2760+
[ c; target; makeIntConst 0; targetIndex; copyCount ],
2761+
?loc = r
2762+
)
2763+
|> Some
2764+
| _ -> None
2765+
| "SetEquals", Some c, [ other ] ->
2766+
Helper.LibCall(com, "map_util", "setEqualsSet", t, [ c; other ], ?loc = r)
2767+
|> Some
2768+
| "Overlaps", Some c, [ other ] ->
2769+
Helper.LibCall(com, "map_util", "overlapsWithSet", t, [ c; other ], ?loc = r)
2770+
|> Some
2771+
| "SymmetricExceptWith", Some c, [ other ] ->
2772+
Helper.LibCall(com, "map_util", "symmetricExceptWithSet", t, [ c; other ], ?loc = r)
2773+
|> Some
27262774
| _ -> None
27272775

27282776
let exceptions (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
@@ -4188,10 +4236,12 @@ let tryBaseConstructor com ctx (ent: EntityRef) (argTypes: Lazy<Type list>) genA
41884236
| Types.hashset ->
41894237
let args =
41904238
match argTypes.Value, args with
4191-
| [], _ -> [ makeArray Any []; makeEqualityComparer com ctx (Seq.head genArgs) ]
4239+
| ([] | [ Number _ ]), _ -> [ makeArray Any []; makeEqualityComparer com ctx (Seq.head genArgs) ]
41924240
| [ IEnumerable ], [ arg ] -> [ arg; makeEqualityComparer com ctx (Seq.head genArgs) ]
41934241
| [ IEnumerable; IEqualityComparer ], [ arg; eqComp ] -> [ arg; makeComparerFromEqualityComparer eqComp ]
4194-
| [ IEqualityComparer ], [ eqComp ] -> [ makeArray Any []; makeComparerFromEqualityComparer eqComp ]
4242+
| [ IEqualityComparer ], [ eqComp ]
4243+
| [ Number _; IEqualityComparer ], [ _; eqComp ] ->
4244+
[ makeArray Any []; makeComparerFromEqualityComparer eqComp ]
41954245
| _ -> FableError "Unexpected hashset constructor" |> raise
41964246

41974247
let entityName = Naming.cleanNameAsPyIdentifier "HashSet"

src/Fable.Transforms/Replacements.fs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,13 +2959,14 @@ let hashSets (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
29592959
match i.CompiledName, thisArg, args with
29602960
| ".ctor", _, _ ->
29612961
match i.SignatureArgTypes, args with
2962-
| [], _ -> makeHashSet com ctx r t (makeArray Any []) |> Some
2962+
| ([] | [ Number _ ]), _ -> makeHashSet com ctx r t (makeArray Any []) |> Some
29632963
| [ IEnumerable ], [ arg ] -> makeHashSet com ctx r t arg |> Some
29642964
| [ IEnumerable; IEqualityComparer ], [ arg; eqComp ] ->
29652965
makeComparerFromEqualityComparer eqComp
29662966
|> makeHashSetWithComparer com r t arg
29672967
|> Some
2968-
| [ IEqualityComparer ], [ eqComp ] ->
2968+
| [ IEqualityComparer ], [ eqComp ]
2969+
| [ Number _; IEqualityComparer ], [ _; eqComp ] ->
29692970
makeComparerFromEqualityComparer eqComp
29702971
|> makeHashSetWithComparer com r t (makeArray Any [])
29712972
|> Some
@@ -2982,10 +2983,32 @@ let hashSets (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
29822983
let meth = Naming.lowerFirst meth
29832984
let args = injectArg com ctx r "Set" meth i.GenericArgs args
29842985
Helper.LibCall(com, "Set", meth, t, c :: args, ?loc = r) |> Some
2985-
// | "CopyTo" // TODO!!!
2986-
// | "SetEquals"
2987-
// | "Overlaps"
2988-
// | "SymmetricExceptWith"
2986+
| "CopyTo", Some c, args ->
2987+
let count = getFieldWith r Int32.Number c "size"
2988+
2989+
match args with
2990+
| [ target ] ->
2991+
Helper.LibCall(com, "Set", "copyToArray", t, [ c; target; makeIntConst 0; makeIntConst 0; count ], ?loc = r)
2992+
|> Some
2993+
| [ target; targetIndex ] ->
2994+
Helper.LibCall(com, "Set", "copyToArray", t, [ c; target; makeIntConst 0; targetIndex; count ], ?loc = r)
2995+
|> Some
2996+
| [ target; targetIndex; copyCount ] ->
2997+
Helper.LibCall(
2998+
com,
2999+
"Set",
3000+
"copyToArray",
3001+
t,
3002+
[ c; target; makeIntConst 0; targetIndex; copyCount ],
3003+
?loc = r
3004+
)
3005+
|> Some
3006+
| _ -> None
3007+
| "SetEquals", Some c, [ other ] -> Helper.LibCall(com, "Set", "setEquals", t, [ c; other ], ?loc = r) |> Some
3008+
| "Overlaps", Some c, [ other ] -> Helper.LibCall(com, "Set", "overlaps", t, [ c; other ], ?loc = r) |> Some
3009+
| "SymmetricExceptWith", Some c, [ other ] ->
3010+
Helper.LibCall(com, "Set", "symmetricExceptWith", t, [ c; other ], ?loc = r)
3011+
|> Some
29893012
| _ -> None
29903013

29913014
let exceptions (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
@@ -4379,10 +4402,12 @@ let tryBaseConstructor com ctx (ent: EntityRef) (argTypes: Lazy<Type list>) genA
43794402
| Types.hashset ->
43804403
let args =
43814404
match argTypes.Value, args with
4382-
| [], _ -> [ makeArray Any []; makeEqualityComparer com ctx (Seq.head genArgs) ]
4405+
| ([] | [ Number _ ]), _ -> [ makeArray Any []; makeEqualityComparer com ctx (Seq.head genArgs) ]
43834406
| [ IEnumerable ], [ arg ] -> [ arg; makeEqualityComparer com ctx (Seq.head genArgs) ]
43844407
| [ IEnumerable; IEqualityComparer ], [ arg; eqComp ] -> [ arg; makeComparerFromEqualityComparer eqComp ]
4385-
| [ IEqualityComparer ], [ eqComp ] -> [ makeArray Any []; makeComparerFromEqualityComparer eqComp ]
4408+
| [ IEqualityComparer ], [ eqComp ]
4409+
| [ Number _; IEqualityComparer ], [ _; eqComp ] ->
4410+
[ makeArray Any []; makeComparerFromEqualityComparer eqComp ]
43864411
| _ -> FableError "Unexpected hashset constructor" |> raise
43874412

43884413
let entityName = FSharp2Fable.Helpers.cleanNameAsJsIdentifier "HashSet"

0 commit comments

Comments
 (0)