Skip to content

Commit 23222bf

Browse files
committed
Fixed erased type testing
1 parent e42c9a1 commit 23222bf

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

src/Fable.Transforms/FSharp2Fable.Util.fs

+13-7
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,15 @@ module Helpers =
436436
let makeRangeFrom (fsExpr: FSharpExpr) =
437437
Some (makeRange fsExpr.Range)
438438

439-
let isErasedType (com: Compiler) (t: FSharpType) =
440-
// TODO: value types
439+
let isErasedTypeDef (com: Compiler) (tdef: FSharpEntity) =
441440
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)
441+
&& (tdef.IsFSharpUnion || tdef.IsFSharpRecord) // || tdef.IsValueType)
442+
&& not (tdef.TryFullName = Some Types.reference) // no F# refs
443+
&& not (hasAttribute Atts.customEquality tdef.Attributes)
444+
&& not (hasAttribute Atts.customComparison tdef.Attributes)
445+
446+
let isErasedType (com: Compiler) (t: FSharpType) =
447+
t.HasTypeDefinition && (isErasedTypeDef com t.TypeDefinition)
447448

448449
let unionCaseTag (com: IFableCompiler) (ent: FSharpEntity) (unionCase: FSharpUnionCase) =
449450
try
@@ -1174,6 +1175,11 @@ module Util =
11741175
makeImportUserGenerated None Fable.Any selector path |> Some
11751176
| _ -> None
11761177

1178+
let isErasedEntity (com: Compiler) (ent: Fable.Entity) =
1179+
match ent with
1180+
| :? FsEnt as fsEnt -> Helpers.isErasedTypeDef com fsEnt.FSharpEntity
1181+
| _ -> false
1182+
11771183
let isErasedOrStringEnumEntity (ent: Fable.Entity) =
11781184
ent.Attributes |> Seq.exists (fun att ->
11791185
match att.Entity.FullName with

src/Fable.Transforms/FSharp2Fable.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ let private transformNewUnion com ctx r fsType (unionCase: FSharpUnionCase) (arg
5858
| EraseKind.AsValue, _ -> failwith "Shouldn't happen, error?"
5959
| EraseKind.AsTuple, _ -> Fable.NewTuple argExprs |> makeValue r
6060
| EraseKind.AsNamedTuple, _ ->
61-
let caseTag = unionCaseTag tdef unionCase |> makeIntConst
61+
let caseTag = unionCaseTag com tdef unionCase |> makeIntConst
6262
let caseName = makeStrConst unionCase.CompiledName
6363
caseTag::caseName::argExprs |> Fable.NewTuple |> makeValue r
6464
| OptionUnion typ ->
@@ -231,7 +231,7 @@ let private transformUnionCaseTest (com: IFableCompiler) (ctx: Context) r
231231
return makeEqOp r unionExpr (transformStringEnum caseRule unionCase) BinaryEqualStrict
232232
| EraseKind.AsNamedTuple ->
233233
let tag1 = Fable.Get(unionExpr, Fable.TupleIndex(0), Fable.Number Int32, None)
234-
let tag2 = unionCaseTag tdef unionCase |> makeIntConst
234+
let tag2 = unionCaseTag com tdef unionCase |> makeIntConst
235235
return makeEqOp r tag1 tag2 BinaryEqualStrict
236236
// if unionCase.UnionCaseFields.Count = 0 then
237237
// return makeEqOp r unionExpr (transformStringEnum caseRule unionCase) BinaryEqualStrict

src/Fable.Transforms/Fable2Babel.fs

+6-3
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,6 @@ module Reflection =
256256
|]
257257
|> libReflectionCall com ctx r "class"
258258

259-
let private ofString s = StringLiteral s :> Expression
260-
let private ofArray babelExprs = ArrayExpression(List.toArray babelExprs) :> Expression
261-
262259
let transformTypeTest (com: IBabelCompiler) ctx range expr (typ: Fable.Type): Expression =
263260
let warnAndEvalToFalse msg =
264261
"Cannot type test (evals to false): " + msg
@@ -311,6 +308,12 @@ module Reflection =
311308
let ent = com.GetEntity(ent)
312309
if ent.IsInterface then
313310
warnAndEvalToFalse "interfaces"
311+
elif FSharp2Fable.Util.isErasedEntity com ent then
312+
let expr = com.TransformAsExpr(ctx, expr)
313+
let idx = if ent.IsFSharpUnion then 1 else 0
314+
let actual = Util.getExpr None expr (Util.ofInt idx)
315+
let expected = Util.ofString ent.FullName
316+
upcast BinaryExpression(BinaryEqualStrict, actual, expected, ?loc=range)
314317
else
315318
match tryJsConstructor com ctx ent with
316319
| Some cons ->

src/Fable.Transforms/Replacements.fs

+7
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,13 @@ let toString com (ctx: Context) r (args: Expr list) =
417417
// | DeclaredType(ent, _) when ent.IsFSharpUnion || ent.IsFSharpRecord || ent.IsValueType ->
418418
// Helper.InstanceCall(head, "toString", String, [], ?loc=r)
419419
// | DeclaredType(ent, _) ->
420+
// let ent = com.GetEntity(ent)
421+
// if FSharp2Fable.Util.isErasedEntity com ent then
422+
// if ent.IsFSharpUnion
423+
// then emitJsExpr r String [head] "$0.shift().join(' ')" // unions
424+
// else emitJsExpr r String [head] "$0[0]" // records and value types
425+
// else
426+
// Helper.LibCall(com, "Types", "toString", String, [head], ?loc=r)
420427
| _ -> Helper.LibCall(com, "Types", "toString", String, [head], ?loc=r)
421428

422429
let getParseParams (kind: NumberExtKind) =

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
"terser-bundle": "npm run terser -- dist/bundle.js -o dist/bundle.min.js --mangle --compress",
2727
"webpack-bundle": "npm run webpack -- -p --entry ./out-node/app.js --output ./dist/bundle.min.js --target node",
2828

29-
"prebuild-lib-dotnet": "git clean -fdx && npm run tsc -- -p ../../../fable-library --outDir ./out-lib",
29+
"build-lib-ts": "npm run tsc -- -p ../../../fable-library --outDir ./out-lib",
3030
"build-lib-dotnet": "dotnet run -c Release ../../../fable-library/Fable.Library.fsproj out-lib --fableLib out-lib",
31+
"build-lib-erased": "dotnet run -c Release ../../../fable-library/Fable.Library.fsproj out-lib --fableLib out-lib --eraseUnions",
3132
"prebuild-lib-dotnet-ts": "git clean -fdx && mkdir out-lib && cp -R ../../../fable-library/*.ts out-lib",
3233
"build-lib-dotnet-ts": "dotnet run -c Release ../../../fable-library/Fable.Library.fsproj out-lib --typescript --fableLib out-lib",
3334
"tsc-lib-init": "npm run tsc -- --init --target es2020 --module es2020 --allowJs",
@@ -46,13 +47,10 @@
4647
"build-tests-dotnet-ts": "dotnet run -c Release ../../../../tests/Main/Fable.Tests.fsproj out-tests --typescript",
4748
"build-tests-dotnet-opt": "dotnet run -c Release ../../../../tests/Main/Fable.Tests.fsproj out-tests --optimize",
4849
"build-tests-node": "node out-node/app.js ../../../../tests/Main/Fable.Tests.fsproj out-tests",
50+
"prebuild-tests-erased": "git clean -fdx && npm run build-lib-erased && npm run build-lib-ts",
51+
"build-tests-erased": "npm run build-tests-dotnet -- --fableLib out-lib --eraseUnions",
4952
"tests": "npm run mocha -- out-tests -r esm --colors --reporter dot",
5053

51-
"prebuild-fable-library": "dotnet run -c Release ../../../fable-library/Fable.Library.fsproj out-lib --fableLib out-lib --eraseUnions",
52-
"build-fable-library": "npm run tsc -- -p ../../../fable-library --outDir ./out-lib",
53-
"prebuild-tests": "git clean -fdx && npm run build-fable-library",
54-
"build-tests": "npm run build-tests-dotnet -- --fableLib out-lib --eraseUnions",
55-
5654
"tsc": "node ../../../../node_modules/typescript/bin/tsc",
5755
"babel": "node ../../../../node_modules/@babel/cli/bin/babel",
5856
"mocha": "node ../../../../node_modules/mocha/bin/mocha",

0 commit comments

Comments
 (0)