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 3, 2021
1 parent 26f8219 commit 4bb9d33
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
12 changes: 8 additions & 4 deletions src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,13 @@ 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 (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 +729,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

0 comments on commit 4bb9d33

Please sign in to comment.