Skip to content

Commit 7d1d79e

Browse files
committed
The LamderaCheckBoth harness generated in lamdera check also needs to support the unsafeCoerce trick
1 parent 008976f commit 7d1d79e

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

extra/Lamdera/Evergreen/MigrationHarness.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ genSupportingCode = do
534534
-- In development, we aren't building with the harnesses, so rather than an extra
535535
-- file dependency, just inject the additional helpers we need to type check our migrations
536536
pure [text|
537+
import Lamdera exposing (sendToFrontend)
538+
537539

538540
type UpgradeResult valueType msgType
539541
= AlreadyCurrent ( valueType, Cmd msgType )
@@ -579,6 +581,22 @@ genSupportingCode = do
579581
Upgraded ( unsafeCoerce model, Cmd.none )
580582

581583

584+
{-|
585+
All local call-sites to this function will get replaced by the compiler
586+
to point to Lamdera.Effect.unsafeCoerce instead, and this def will be removed
587+
See lamdera-compiler/extra/Lamdera/Evergreen/ModifyAST.hs
588+
-}
589+
unsafeCoerce : a -> b
590+
unsafeCoerce =
591+
let
592+
-- This is a hack to ensure the Lamdera.Effect module gets included
593+
-- in overall compile scope given we cannot reference it directly
594+
forceInclusion =
595+
sendToFrontend
596+
in
597+
Debug.todo "unsafeCoerce"
598+
599+
582600
upgradeIsCurrent : Result String ( newModel, Cmd msg ) -> Result String (UpgradeResult newModel msg)
583601
upgradeIsCurrent priorResult =
584602
priorResult |> Result.map AlreadyCurrent

extra/Lamdera/Evergreen/ModifyAST.hs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ update :: Can.Module -> Can.Module
3636
update canonical =
3737
let
3838
moduleName :: Module.Canonical = (Can._name canonical)
39+
decls :: Can.Decls = (Can._decls canonical) & removeUnsafeCoercePlaceholder
40+
newDecls :: Can.Decls = updateDecls moduleName decls
41+
newCanonical :: Can.Module = canonical { Can._decls = newDecls }
3942
in
4043
case moduleName of
4144
Module.Canonical (Name "author" "project") "LamderaHelpers" ->
42-
let
43-
decls :: Can.Decls = (Can._decls canonical) & removeUnsafeCoercePlaceholder
44-
newDecls :: Can.Decls = updateDecls moduleName decls
45-
in
46-
canonical { Can._decls = newDecls }
47-
45+
newCanonical
46+
Module.Canonical (Name "author" "project") "LamderaCheckBoth" ->
47+
newCanonical
4848
_ ->
4949
canonical
5050

@@ -77,23 +77,29 @@ updateDecls fileName decls =
7777

7878
updateExpr :: Module.Canonical -> Name.Name -> Can.Expr -> Can.Expr
7979
updateExpr fileName functionName (Reporting.Annotation.At location_ expr_) =
80+
let replaceCall location params =
81+
Can.Call
82+
(Reporting.Annotation.At location
83+
(Can.VarForeign
84+
(Module.Canonical (Name "lamdera" "core") "Lamdera.Effect")
85+
"unsafeCoerce"
86+
(Forall
87+
(Map.fromList [("a", ()), ("b", ())])
88+
(TLambda (TVar "a") (TVar "b"))
89+
)
90+
)
91+
) params
92+
in
8093
(case expr_ of
8194
Can.Call (Reporting.Annotation.At location
8295
(Can.VarTopLevel (Module.Canonical (Name "author" "project") "LamderaHelpers") "unsafeCoerce")
8396
) params ->
84-
Can.Call
85-
(Reporting.Annotation.At
86-
location
87-
(Can.VarForeign
88-
(Module.Canonical (Name "lamdera" "core") "Lamdera.Effect")
89-
"unsafeCoerce"
90-
(Forall
91-
(Map.fromList [("a", ()), ("b", ())])
92-
(TLambda (TVar "a") (TVar "b"))
93-
)
94-
)
95-
)
96-
params
97+
replaceCall location params
98+
99+
Can.Call (Reporting.Annotation.At location
100+
(Can.VarTopLevel (Module.Canonical (Name "author" "project") "LamderaCheckBoth") "unsafeCoerce")
101+
) params ->
102+
replaceCall location params
97103

98104
-- The recursive rest. Might be worth looking at revisiting recursion schemes again, esp if error messages have improved
99105
Can.VarLocal name -> Can.VarLocal name

test/Lamdera/Evergreen/TestMigrationGenerator.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ testExamples = withTestEnv $ do
123123

124124
testFiles & mapM (\folder -> do
125125
scope folder $ do
126-
io $ putStrLn $ "testing: " <> show folder
126+
io $ atomicPutStrLn $ "testing: " <> show folder
127127
let
128128
oldVersion = 1
129129
newVersion = 2

test/Test/Wire.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ wire = do
139139

140140

141141
testFiles & filter ((/=) "") & mapM (\filename -> do
142-
putStrLn $ "testing: " <> show filename
142+
atomicPutStrLn $ "testing: " <> show filename
143143
-- Bust Elm's caching with this one weird trick!
144144
touch $ project </> filename
145145
Lamdera.Compile.makeDev project [filename] `catch` catchTestException filename

0 commit comments

Comments
 (0)