Skip to content

Commit

Permalink
The LamderaCheckBoth harness generated in lamdera check also needs to…
Browse files Browse the repository at this point in the history
… support the unsafeCoerce trick
  • Loading branch information
supermario committed May 3, 2024
1 parent 008976f commit 7d1d79e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
18 changes: 18 additions & 0 deletions extra/Lamdera/Evergreen/MigrationHarness.hs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ genSupportingCode = do
-- In development, we aren't building with the harnesses, so rather than an extra
-- file dependency, just inject the additional helpers we need to type check our migrations
pure [text|
import Lamdera exposing (sendToFrontend)


type UpgradeResult valueType msgType
= AlreadyCurrent ( valueType, Cmd msgType )
Expand Down Expand Up @@ -579,6 +581,22 @@ genSupportingCode = do
Upgraded ( unsafeCoerce model, Cmd.none )


{-|
All local call-sites to this function will get replaced by the compiler
to point to Lamdera.Effect.unsafeCoerce instead, and this def will be removed
See lamdera-compiler/extra/Lamdera/Evergreen/ModifyAST.hs
-}
unsafeCoerce : a -> b
unsafeCoerce =
let
-- This is a hack to ensure the Lamdera.Effect module gets included
-- in overall compile scope given we cannot reference it directly
forceInclusion =
sendToFrontend
in
Debug.todo "unsafeCoerce"


upgradeIsCurrent : Result String ( newModel, Cmd msg ) -> Result String (UpgradeResult newModel msg)
upgradeIsCurrent priorResult =
priorResult |> Result.map AlreadyCurrent
Expand Down
44 changes: 25 additions & 19 deletions extra/Lamdera/Evergreen/ModifyAST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ update :: Can.Module -> Can.Module
update canonical =
let
moduleName :: Module.Canonical = (Can._name canonical)
decls :: Can.Decls = (Can._decls canonical) & removeUnsafeCoercePlaceholder
newDecls :: Can.Decls = updateDecls moduleName decls
newCanonical :: Can.Module = canonical { Can._decls = newDecls }
in
case moduleName of
Module.Canonical (Name "author" "project") "LamderaHelpers" ->
let
decls :: Can.Decls = (Can._decls canonical) & removeUnsafeCoercePlaceholder
newDecls :: Can.Decls = updateDecls moduleName decls
in
canonical { Can._decls = newDecls }

newCanonical
Module.Canonical (Name "author" "project") "LamderaCheckBoth" ->
newCanonical
_ ->
canonical

Expand Down Expand Up @@ -77,23 +77,29 @@ updateDecls fileName decls =

updateExpr :: Module.Canonical -> Name.Name -> Can.Expr -> Can.Expr
updateExpr fileName functionName (Reporting.Annotation.At location_ expr_) =
let replaceCall location params =
Can.Call
(Reporting.Annotation.At location
(Can.VarForeign
(Module.Canonical (Name "lamdera" "core") "Lamdera.Effect")
"unsafeCoerce"
(Forall
(Map.fromList [("a", ()), ("b", ())])
(TLambda (TVar "a") (TVar "b"))
)
)
) params
in
(case expr_ of
Can.Call (Reporting.Annotation.At location
(Can.VarTopLevel (Module.Canonical (Name "author" "project") "LamderaHelpers") "unsafeCoerce")
) params ->
Can.Call
(Reporting.Annotation.At
location
(Can.VarForeign
(Module.Canonical (Name "lamdera" "core") "Lamdera.Effect")
"unsafeCoerce"
(Forall
(Map.fromList [("a", ()), ("b", ())])
(TLambda (TVar "a") (TVar "b"))
)
)
)
params
replaceCall location params

Can.Call (Reporting.Annotation.At location
(Can.VarTopLevel (Module.Canonical (Name "author" "project") "LamderaCheckBoth") "unsafeCoerce")
) params ->
replaceCall location params

-- The recursive rest. Might be worth looking at revisiting recursion schemes again, esp if error messages have improved
Can.VarLocal name -> Can.VarLocal name
Expand Down
2 changes: 1 addition & 1 deletion test/Lamdera/Evergreen/TestMigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ testExamples = withTestEnv $ do

testFiles & mapM (\folder -> do
scope folder $ do
io $ putStrLn $ "testing: " <> show folder
io $ atomicPutStrLn $ "testing: " <> show folder
let
oldVersion = 1
newVersion = 2
Expand Down
2 changes: 1 addition & 1 deletion test/Test/Wire.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ wire = do


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

0 comments on commit 7d1d79e

Please sign in to comment.