Skip to content

Commit

Permalink
Fix #2342: DU tests must be recompiled
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsogarciacaro committed Jan 11, 2021
1 parent e85be78 commit 79e4b4e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,11 @@ module Helpers =
let makeRangeFrom (fsExpr: FSharpExpr) =
Some (makeRange fsExpr.Range)

let unionCaseTag (ent: FSharpEntity) (unionCase: FSharpUnionCase) =
let unionCaseTag (com: IFableCompiler) (ent: FSharpEntity) (unionCase: FSharpUnionCase) =
try
// If the order of cases changes in the declaration, the tag has to change too.
// Mark all files using the case tag as watch dependencies.
com.AddWatchDependency(FsEnt.SourcePath ent)
ent.UnionCases |> Seq.findIndex (fun uci -> unionCase.Name = uci.Name)
with _ ->
failwithf "Cannot find case %s in %s" unionCase.Name (FsEnt.FullName ent)
Expand Down
13 changes: 7 additions & 6 deletions src/Fable.Transforms/FSharp2Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ let private transformNewUnion com ctx r fsType (unionCase: FSharpUnionCase) (arg
Fable.NewList(headAndTail, typ) |> makeValue r
| DiscriminatedUnion(tdef, genArgs) ->
let genArgs = makeGenArgs ctx.GenericArgs genArgs
let tag = unionCaseTag tdef unionCase
// If the order of cases changes in the declaration, the tag has to change too
// mark all files calling the union constructor as watch dependencies
com.AddWatchDependency(FsEnt.SourcePath tdef)
let tag = unionCaseTag com tdef unionCase
Fable.NewUnion(argExprs, tag, FsEnt.Ref tdef, genArgs) |> makeValue r

let private transformTraitCall com (ctx: Context) r typ (sourceTypes: Fable.Type list) traitName (flags: MemberFlags) (argTypes: Fable.Type list) (argExprs: Fable.Expr list) =
Expand Down Expand Up @@ -263,7 +260,7 @@ let private transformUnionCaseTest (com: IFableCompiler) (ctx: Context) r
| StringEnum(_, rule) ->
return makeEqOp r unionExpr (transformStringEnum rule unionCase) BinaryEqualStrict
| DiscriminatedUnion(tdef,_) ->
let tag = unionCaseTag tdef unionCase
let tag = unionCaseTag com tdef unionCase
return Fable.Test(unionExpr, Fable.UnionCaseTest(tag), r)
}

Expand Down Expand Up @@ -730,7 +727,11 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
let field = FsField field :> Fable.Field |> Fable.FieldKey |> Some
return Fable.Set(callee, field, value, makeRangeFrom fsExpr)

| BasicPatterns.UnionCaseTag(unionExpr, _unionType) ->
| BasicPatterns.UnionCaseTag(unionExpr, unionType) ->
// TODO: This is an inconsistency. For new unions and union tests we calculate
// the tag in this step but here we delay the calculation until Fable2Babel
do tryDefinition unionType
|> Option.iter (fun (tdef, _) -> com.AddWatchDependency(FsEnt.SourcePath tdef))
let! unionExpr = transformExpr com ctx unionExpr
return Fable.Get(unionExpr, Fable.UnionTag, Fable.Any, makeRangeFrom fsExpr)

Expand Down

0 comments on commit 79e4b4e

Please sign in to comment.