Skip to content

Commit 9ad1b26

Browse files
committed
Exclude custom equality and comparison
1 parent 1e15146 commit 9ad1b26

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

src/Fable.Transforms/FSharp2Fable.Util.fs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,14 @@ module Helpers =
436436
let makeRangeFrom (fsExpr: FSharpExpr) =
437437
Some (makeRange fsExpr.Range)
438438

439-
let isErasedRecord (com: Compiler) (t: FSharpType) =
440-
// TODO: check for custom equality or comparison
441-
com.Options.EraseUnions && t.HasTypeDefinition && t.TypeDefinition.IsFSharpRecord
439+
let isErasedType (com: Compiler) (t: FSharpType) =
440+
// TODO: value types
441+
com.Options.EraseUnions
442+
&& t.HasTypeDefinition
443+
&& (t.TypeDefinition.IsFSharpRecord || t.TypeDefinition.IsFSharpUnion)
444+
&& not (t.TypeDefinition.TryFullName = Some Types.reference) // F# refs are records
445+
&& not (hasAttribute Atts.customEquality t.TypeDefinition.Attributes)
446+
&& not (hasAttribute Atts.customComparison t.TypeDefinition.Attributes)
442447

443448
let unionCaseTag (ent: FSharpEntity) (unionCase: FSharpUnionCase) =
444449
try
@@ -725,7 +730,7 @@ module Patterns =
725730
Some (ErasedUnion(kind, tdef, typ.GenericArguments))
726731
| _ -> None))
727732
|> Option.defaultWith (fun () ->
728-
if com.Options.EraseUnions
733+
if isErasedType com typ
729734
then ErasedUnion(EraseKind.AsNamedTuple, tdef, typ.GenericArguments)
730735
else DiscriminatedUnion(tdef, typ.GenericArguments))
731736

src/Fable.Transforms/FSharp2Fable.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
670670
let! callee = transformExpr com ctx callee
671671
let typ = makeType ctx.GenericArgs fsExpr.Type
672672
let fieldName = calleeType.AnonRecordTypeDetails.SortedFieldNames.[fieldIndex]
673-
if isErasedRecord com calleeType then
673+
if isErasedType com calleeType then
674674
return Fable.Get(callee, Fable.FieldIndex(fieldName, fieldIndex), typ, r)
675675
else
676676
let key = FsField(fieldName, lazy typ) :> Fable.Field |> Fable.FieldKey
@@ -684,7 +684,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
684684
| Some callee -> callee
685685
| None -> entityRef com (FsEnt calleeType.TypeDefinition)
686686
let typ = makeType ctx.GenericArgs fsExpr.Type
687-
if isErasedRecord com calleeType then
687+
if isErasedType com calleeType then
688688
let index = calleeType.TypeDefinition.FSharpFields |> Seq.findIndex (fun x -> x.Name = field.Name)
689689
return Fable.Get(callee, Fable.FieldIndex(field.Name, index + 1), typ, r)
690690
else
@@ -736,7 +736,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
736736
match callee with
737737
| Some callee -> callee
738738
| None -> entityRef com (FsEnt calleeType.TypeDefinition)
739-
if isErasedRecord com calleeType then
739+
if isErasedType com calleeType then
740740
let index = calleeType.TypeDefinition.FSharpFields |> Seq.findIndex (fun x -> x.Name = field.Name)
741741
return Fable.Set(callee, Fable.FieldIndexSet(field.Name, index + 1), value, r)
742742
else
@@ -812,7 +812,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
812812
| BasicPatterns.NewRecord(fsType, argExprs) ->
813813
let r = makeRangeFrom fsExpr
814814
let! argExprs = transformExprList com ctx argExprs
815-
if isErasedRecord com fsType then
815+
if isErasedType com fsType then
816816
let recordName = (makeStrConst (getFsTypeFullName fsType))
817817
return recordName::argExprs |> Fable.NewTuple |> makeValue r
818818
else
@@ -822,7 +822,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
822822
| BasicPatterns.NewAnonRecord(fsType, argExprs) ->
823823
let r = makeRangeFrom fsExpr
824824
let! argExprs = transformExprList com ctx argExprs
825-
if isErasedRecord com fsType then
825+
if isErasedType com fsType then
826826
return argExprs |> Fable.NewTuple |> makeValue r
827827
else
828828
let fieldNames = fsType.AnonRecordTypeDetails.SortedFieldNames

src/Fable.Transforms/Replacements.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,10 @@ let structuralHash (com: ICompiler) r (arg: Expr) =
772772
| Array _ -> "arrayHash"
773773
| Builtin (BclDateTime|BclDateTimeOffset) -> "dateHash"
774774
| Builtin (BclInt64|BclUInt64|BclDecimal) -> "fastStructuralHash"
775-
| DeclaredType(ent, _) ->
776-
let ent = com.GetEntity(ent)
777-
if not ent.IsInterface then "safeHash"
778-
else "structuralHash"
775+
// | DeclaredType(ent, _) ->
776+
// let ent = com.GetEntity(ent)
777+
// if not ent.IsInterface then "safeHash"
778+
// else "structuralHash"
779779
| _ -> "structuralHash"
780780
Helper.LibCall(com, "Util", methodName, Number Int32, [arg], ?loc=r)
781781

src/fable-standalone/test/bench-compiler/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
"prebuild-dotnet": "git clean -fdx",
99
"build-dotnet": "dotnet run -c Release bench-compiler.fsproj out-node",
10+
"build-dotnet-erased": "dotnet run -c Release bench-compiler.fsproj out-node --fableLib out-lib --eraseUnions",
1011
"build-dotnet-opt": "dotnet run -c Release bench-compiler.fsproj out-node --optimize",
1112
"postbuild-dotnet": "npm run rollup-bundle",
1213

@@ -38,6 +39,7 @@
3839
"build-test-node": "node dist/bundle.js ../../../../../fable-test/fable-test.fsproj out-test",
3940
"build-test-node-es": "node out-node/app.js ../../../../../fable-test/fable-test.fsproj out-test",
4041
"build-test-node-ts": "node out-node/app.js ../../../../../fable-test/fable-test.fsproj out-test --typescript",
42+
"build-test-erased": "dotnet run -c Release ../../../../../fable-test/fable-test.fsproj out-test --fableLib out-lib --eraseUnions",
4143
"test-node": "node ./out-test/src/test.js",
4244

4345
"build-tests-dotnet": "dotnet run -c Release ../../../../tests/Main/Fable.Tests.fsproj out-tests",

0 commit comments

Comments
 (0)