Skip to content

Commit

Permalink
Exclude custom equality and comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave committed Jan 5, 2021
1 parent 1e15146 commit 9ad1b26
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
13 changes: 9 additions & 4 deletions src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,14 @@ module Helpers =
let makeRangeFrom (fsExpr: FSharpExpr) =
Some (makeRange fsExpr.Range)

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

let unionCaseTag (ent: FSharpEntity) (unionCase: FSharpUnionCase) =
try
Expand Down Expand Up @@ -725,7 +730,7 @@ module Patterns =
Some (ErasedUnion(kind, tdef, typ.GenericArguments))
| _ -> None))
|> Option.defaultWith (fun () ->
if com.Options.EraseUnions
if isErasedType com typ
then ErasedUnion(EraseKind.AsNamedTuple, tdef, typ.GenericArguments)
else DiscriminatedUnion(tdef, typ.GenericArguments))

Expand Down
10 changes: 5 additions & 5 deletions src/Fable.Transforms/FSharp2Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
let! callee = transformExpr com ctx callee
let typ = makeType ctx.GenericArgs fsExpr.Type
let fieldName = calleeType.AnonRecordTypeDetails.SortedFieldNames.[fieldIndex]
if isErasedRecord com calleeType then
if isErasedType com calleeType then
return Fable.Get(callee, Fable.FieldIndex(fieldName, fieldIndex), typ, r)
else
let key = FsField(fieldName, lazy typ) :> Fable.Field |> Fable.FieldKey
Expand All @@ -684,7 +684,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
| Some callee -> callee
| None -> entityRef com (FsEnt calleeType.TypeDefinition)
let typ = makeType ctx.GenericArgs fsExpr.Type
if isErasedRecord com calleeType then
if isErasedType com calleeType then
let index = calleeType.TypeDefinition.FSharpFields |> Seq.findIndex (fun x -> x.Name = field.Name)
return Fable.Get(callee, Fable.FieldIndex(field.Name, index + 1), typ, r)
else
Expand Down Expand Up @@ -736,7 +736,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
match callee with
| Some callee -> callee
| None -> entityRef com (FsEnt calleeType.TypeDefinition)
if isErasedRecord com calleeType then
if isErasedType com calleeType then
let index = calleeType.TypeDefinition.FSharpFields |> Seq.findIndex (fun x -> x.Name = field.Name)
return Fable.Set(callee, Fable.FieldIndexSet(field.Name, index + 1), value, r)
else
Expand Down Expand Up @@ -812,7 +812,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
| BasicPatterns.NewRecord(fsType, argExprs) ->
let r = makeRangeFrom fsExpr
let! argExprs = transformExprList com ctx argExprs
if isErasedRecord com fsType then
if isErasedType com fsType then
let recordName = (makeStrConst (getFsTypeFullName fsType))
return recordName::argExprs |> Fable.NewTuple |> makeValue r
else
Expand All @@ -822,7 +822,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
| BasicPatterns.NewAnonRecord(fsType, argExprs) ->
let r = makeRangeFrom fsExpr
let! argExprs = transformExprList com ctx argExprs
if isErasedRecord com fsType then
if isErasedType com fsType then
return argExprs |> Fable.NewTuple |> makeValue r
else
let fieldNames = fsType.AnonRecordTypeDetails.SortedFieldNames
Expand Down
8 changes: 4 additions & 4 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,10 @@ let structuralHash (com: ICompiler) r (arg: Expr) =
| Array _ -> "arrayHash"
| Builtin (BclDateTime|BclDateTimeOffset) -> "dateHash"
| Builtin (BclInt64|BclUInt64|BclDecimal) -> "fastStructuralHash"
| DeclaredType(ent, _) ->
let ent = com.GetEntity(ent)
if not ent.IsInterface then "safeHash"
else "structuralHash"
// | DeclaredType(ent, _) ->
// let ent = com.GetEntity(ent)
// if not ent.IsInterface then "safeHash"
// else "structuralHash"
| _ -> "structuralHash"
Helper.LibCall(com, "Util", methodName, Number Int32, [arg], ?loc=r)

Expand Down
2 changes: 2 additions & 0 deletions src/fable-standalone/test/bench-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

Expand Down Expand Up @@ -38,6 +39,7 @@
"build-test-node": "node dist/bundle.js ../../../../../fable-test/fable-test.fsproj out-test",
"build-test-node-es": "node out-node/app.js ../../../../../fable-test/fable-test.fsproj out-test",
"build-test-node-ts": "node out-node/app.js ../../../../../fable-test/fable-test.fsproj out-test --typescript",
"build-test-erased": "dotnet run -c Release ../../../../../fable-test/fable-test.fsproj out-test --fableLib out-lib --eraseUnions",
"test-node": "node ./out-test/src/test.js",

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

0 comments on commit 9ad1b26

Please sign in to comment.