fix: don't hang on module expansions (#1340)

This commit is contained in:
Scott Olsen 2021-10-22 00:59:51 -04:00 committed by GitHub
parent 320bc67bac
commit 499a03e63e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -41,6 +41,19 @@ expand eval ctx xobj =
Lst _ -> expandList xobj
Arr _ -> expandArray xobj
Sym _ _ -> expandSymbol xobj
-- This case is needed to ensure we expand naked mod names to initers consistently.
-- Consider both:
-- (width (address &(B 2)))
-- (width B)
-- The first case is correct code and was handled by expandList. The second case is an error and previously resulted in a loop because
-- module expansion wasn't handled in expandSymbol, but handling it there
-- by ending the expansion loop breaks init expansion in the first case,
-- since expandList calls expand.
-- So, we have no choice but to add a case here to cut the recursion and to expand this form consistently in all places.
Mod e _ ->
let pathToModule = pathToEnv e
implicitInit = XObj (Sym (SymPath pathToModule "init") Symbol) (xobjInfo xobj) (xobjTy xobj)
in pure (ctx, Right implicitInit)
_ -> pure (ctx, Right xobj)
where
expandList :: XObj -> IO (Context, Either EvalError XObj)

View File

@ -228,6 +228,8 @@ primitiveRegisterType _ ctx [XObj (Sym (SymPath [] t) _) _ _] =
primitiveRegisterTypeWithoutFields ctx t Nothing
primitiveRegisterType _ ctx [x] =
pure (evalError ctx ("`register-type` takes a symbol, but it got " ++ pretty x) (xobjInfo x))
primitiveRegisterType _ ctx [x@(XObj (Sym (SymPath [] _) _) _ _), XObj (Str "") _ _] =
pure (evalError ctx ("cannot register type " ++ pretty x ++ " with an empty string override, this will produce invalid C") (xobjInfo x))
primitiveRegisterType _ ctx [XObj (Sym (SymPath [] t) _) _ _, XObj (Str override) _ _] =
primitiveRegisterTypeWithoutFields ctx t (Just override)
primitiveRegisterType _ ctx [x@(XObj (Sym (SymPath [] t) _) _ _), XObj (Str override) _ _, members] =