Skip to content

Commit 433ec5e

Browse files
committed
Production harness checker needs the unsafeCoerce helper trick as well
1 parent f2f2287 commit 433ec5e

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

extra/Lamdera/Evergreen/MigrationHarness.hs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,25 @@ upgradeFor migrationSequence nextVersion valueType = do
169169
case nextVersion of
170170
WithMigrations _ ->
171171
[untrimming|
172-
-- upgrade${valueType}Previous : T$currentVersion_.$valueType -> UpgradeResult T$nextVersion_.$valueType T$nextVersion_.$cmdMsgType
172+
{-| upgrade${valueType}Previous : T$currentVersion_.$valueType -> UpgradeResult T$nextVersion_.$valueType T$nextVersion_.$cmdMsgType
173+
-}
173174
upgrade${valueType}Previous : previousModel -> UpgradeResult T$nextVersion_.$valueType T$nextVersion_.$cmdMsgType
174175
upgrade${valueType}Previous model_v$currentVersion_ =
175-
unsafeCoerce model_v$currentVersion_
176-
|> M$nextVersion_.$valueTypeTitleCase
176+
case unsafeCoerce model_v$currentVersion_ |> M$nextVersion_.$valueTypeTitleCase of
177+
ModelMigrated ( newValue, cmds ) ->
178+
Upgraded ( newValue, cmds )
179+
180+
ModelUnchanged ->
181+
-- Should be impossible in this context
182+
unchanged model_v$currentVersion_
177183

178184

179185
|]
180186

181187
WithoutMigrations _ ->
182188
[untrimming|
183-
-- upgrade${valueType}Previous : T$nextVersion_.$valueType -> UpgradeResult T$nextVersion_.$valueType T$nextVersion_.$cmdMsgType
189+
{-| upgrade${valueType}Previous : T$currentVersion_.$valueType -> UpgradeResult T$nextVersion_.$valueType T$nextVersion_.$cmdMsgType
190+
-}
184191
upgrade${valueType}Previous : previousModel -> UpgradeResult T$nextVersion_.$valueType T$nextVersion_.$cmdMsgType
185192
upgrade${valueType}Previous model_v$currentVersion_ =
186193
unchanged model_v$currentVersion_
@@ -531,7 +538,27 @@ genSupportingCode = do
531538
then
532539
-- In production, use shared injected code that the LBR/LFR runtime harnesses
533540
-- also reference to share types and type check everything together
534-
pure "import LamderaHelpers exposing (..)\n"
541+
pure [text|
542+
import Lamdera exposing (sendToFrontend)
543+
import LamderaHelpers exposing (..)
544+
545+
546+
{-|
547+
All local call-sites to this function will get replaced by the compiler
548+
to point to Lamdera.Effect.unsafeCoerce instead, and this def will be removed
549+
See lamdera-compiler/extra/Lamdera/Evergreen/ModifyAST.hs
550+
-}
551+
unsafeCoerce : a -> b
552+
unsafeCoerce =
553+
let
554+
-- This is a hack to ensure the Lamdera.Effect module gets included
555+
-- in overall compile scope given we cannot reference it directly
556+
forceInclusion =
557+
sendToFrontend
558+
in
559+
Debug.todo "unsafeCoerce"
560+
561+
|]
535562
else
536563
-- In development, we aren't building with the harnesses, so rather than an extra
537564
-- file dependency, just inject the additional helpers we need to type check our migrations

extra/Lamdera/Evergreen/ModifyAST.hs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ update canonical =
4141
newCanonical :: Can.Module = canonical { Can._decls = newDecls }
4242
in
4343
case moduleName of
44-
Module.Canonical (Name "author" "project") "LamderaHelpers" ->
45-
newCanonical
46-
Module.Canonical (Name "author" "project") "LamderaCheckBoth" ->
47-
newCanonical
48-
_ ->
49-
canonical
44+
Module.Canonical (Name "author" "project") "LamderaHelpers" -> newCanonical
45+
Module.Canonical (Name "author" "project") "LamderaCheckBoth" -> newCanonical
46+
Module.Canonical (Name "author" "project") "LamderaGenerated" -> newCanonical
47+
_ -> canonical
5048

5149

5250
removeUnsafeCoercePlaceholder :: Can.Decls -> Can.Decls
@@ -56,23 +54,18 @@ removeUnsafeCoercePlaceholder decls =
5654

5755
updateDecls :: Module.Canonical -> Can.Decls -> Can.Decls
5856
updateDecls fileName decls =
59-
case fileName of
60-
Module.Canonical (Name "author" "project") "LamderaHelpers" ->
61-
case decls of
62-
Can.Declare def nextDecl ->
63-
Can.Declare (updateDefs fileName def) (updateDecls fileName nextDecl)
57+
case decls of
58+
Can.Declare def nextDecl ->
59+
Can.Declare (updateDefs fileName def) (updateDecls fileName nextDecl)
6460

65-
Can.DeclareRec def remainingDefs nextDecl ->
66-
Can.DeclareRec
67-
(updateDefs fileName def)
68-
(map (updateDefs fileName) remainingDefs)
69-
(updateDecls fileName nextDecl)
61+
Can.DeclareRec def remainingDefs nextDecl ->
62+
Can.DeclareRec
63+
(updateDefs fileName def)
64+
(map (updateDefs fileName) remainingDefs)
65+
(updateDecls fileName nextDecl)
7066

71-
Can.SaveTheEnvironment ->
72-
Can.SaveTheEnvironment
73-
74-
_ ->
75-
decls
67+
Can.SaveTheEnvironment ->
68+
Can.SaveTheEnvironment
7669

7770

7871
updateExpr :: Module.Canonical -> Name.Name -> Can.Expr -> Can.Expr
@@ -101,6 +94,11 @@ updateExpr fileName functionName (Reporting.Annotation.At location_ expr_) =
10194
) params ->
10295
replaceCall location params
10396

97+
Can.Call (Reporting.Annotation.At location
98+
(Can.VarTopLevel (Module.Canonical (Name "author" "project") "LamderaGenerated") "unsafeCoerce")
99+
) params ->
100+
replaceCall location params
101+
104102
-- The recursive rest. Might be worth looking at revisiting recursion schemes again, esp if error messages have improved
105103
Can.VarLocal name -> Can.VarLocal name
106104
Can.VarTopLevel canonical name -> Can.VarTopLevel canonical name

test/Lamdera/Evergreen/TestMigrationHarness.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ suite = tests
154154

155155

156156
upgradeBackendModelPrevious =
157-
()
157+
unchanged
158158

159159

160160
decodeAndUpgradeBackendModel : Int -> Bytes -> UpgradeResult T1.BackendModel T1.BackendMsg
@@ -258,9 +258,11 @@ suite = tests
258258
2
259259

260260

261-
upgradeBackendModelPrevious : T1.BackendModel -> UpgradeResult T2.BackendModel T2.BackendMsg
261+
{-| upgradeBackendModelPrevious : T1.BackendModel -> UpgradeResult T2.BackendModel T2.BackendMsg
262+
-}
263+
upgradeBackendModelPrevious : previousModel -> UpgradeResult T2.BackendModel T2.BackendMsg
262264
upgradeBackendModelPrevious model_v1 =
263-
model_v1
265+
unsafeCoerce model_v1
264266
|> M2.backendModel
265267

266268

@@ -423,7 +425,9 @@ suite = tests
423425
2
424426

425427

426-
upgradeBackendModelPrevious : T2.BackendModel -> UpgradeResult T2.BackendModel T2.BackendMsg
428+
{-| upgradeBackendModelPrevious : T1.BackendModel -> UpgradeResult T2.BackendModel T2.BackendMsg
429+
-}
430+
upgradeBackendModelPrevious : previousModel -> UpgradeResult T2.BackendModel T2.BackendMsg
427431
upgradeBackendModelPrevious model_v1 =
428432
unchanged model_v1
429433

0 commit comments

Comments
 (0)