feat: add compiler error on maximum sumtype constructors (#1319)

This commit is contained in:
Veit Heller 2021-09-18 08:49:19 +02:00 committed by GitHub
parent b745b1fcdb
commit 0188264463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 3 deletions

View File

@ -237,6 +237,8 @@ Note that match works with *values* (not references) takes ownership over the va
Note that this code would not take ownership over `might-be-a-string`. Also, the `s` in the first case is a reference, since it wouldn't be safe to destructure the `Maybe` into values in this situation. Note that this code would not take ownership over `might-be-a-string`. Also, the `s` in the first case is a reference, since it wouldn't be safe to destructure the `Maybe` into values in this situation.
**Note:** A sumtype cannot have more than 128 inhabitants, also known as constructors. If that reads to you like a byte limitation, youre on the right track. While this is a limitation, it has not proved to be a problem as of yet.
### Modules and Name Lookup ### Modules and Name Lookup
Functions and variables can be stored in modules which are named and can be nested. To use a symbol inside a module Functions and variables can be stored in modules which are named and can be nested. To use a symbol inside a module
you need to qualify it with the module name, like this: `Float.cos`. you need to qualify it with the module name, like this: `Float.cos`.

View File

@ -19,6 +19,7 @@ data PrimitiveError
| StructNotFound XObj | StructNotFound XObj
| NonTypeInTypeEnv SymPath XObj | NonTypeInTypeEnv SymPath XObj
| InvalidSumtypeCase XObj | InvalidSumtypeCase XObj
| TooManySumtypeCases
data PrimitiveWarning data PrimitiveWarning
= NonExistentInterfaceWarning XObj = NonExistentInterfaceWarning XObj
@ -74,6 +75,8 @@ instance Show PrimitiveError where
show (PrimitiveError.InvalidSumtypeCase xobj) = show (PrimitiveError.InvalidSumtypeCase xobj) =
"Can't get members for an invalid sumtype case: " "Can't get members for an invalid sumtype case: "
++ pretty xobj ++ pretty xobj
show TooManySumtypeCases =
"Got too many sumtype cases (>128) for type"
instance Show PrimitiveWarning where instance Show PrimitiveWarning where
show (NonExistentInterfaceWarning x) = show (NonExistentInterfaceWarning x) =

View File

@ -413,7 +413,7 @@ primitiveMembers _ ctx xobj@(XObj (Sym path _) _ _) =
go (XObj (Lst [(XObj (Deftype _) _ _), _, (XObj (Arr members) _ _)]) _ _) = go (XObj (Lst [(XObj (Deftype _) _ _), _, (XObj (Arr members) _ _)]) _ _) =
pure (ctx, Right (XObj (Arr (map (\(a, b) -> XObj (Lst [a, b]) Nothing Nothing) (pairwise members))) Nothing Nothing)) pure (ctx, Right (XObj (Arr (map (\(a, b) -> XObj (Lst [a, b]) Nothing Nothing) (pairwise members))) Nothing Nothing))
go (XObj (Lst ((XObj (DefSumtype _) _ _) : _ : cases)) _ _) = go (XObj (Lst ((XObj (DefSumtype _) _ _) : _ : cases)) _ _) =
pure $ (ctx, (either Left (\a -> Right (XObj (Arr (concat a)) Nothing Nothing)) (mapM getMembersFromCase cases))) pure (ctx, (either Left (\a -> Right (XObj (Arr (concat a)) Nothing Nothing)) (mapM getMembersFromCase cases)))
go x = pure (toEvalError ctx x (NonTypeInTypeEnv path x)) go x = pure (toEvalError ctx x (NonTypeInTypeEnv path x))
getMembersFromCase :: XObj -> Either EvalError [XObj] getMembersFromCase :: XObj -> Either EvalError [XObj]
getMembersFromCase (XObj (Lst members) _ _) = getMembersFromCase (XObj (Lst members) _ _) =
@ -576,8 +576,10 @@ primitiveDeftype xobj ctx (name : rest@(XObj (Arr a) _ _ : _)) =
if all isUnqualifiedSym objs if all isUnqualifiedSym objs
then deftype ctx name (selectConstructor rest) then deftype ctx name (selectConstructor rest)
else pure (toEvalError ctx xobj (QualifiedTypeMember rest)) else pure (toEvalError ctx xobj (QualifiedTypeMember rest))
primitiveDeftype _ ctx (name : rest) = primitiveDeftype x ctx (name : rest) =
deftype ctx name (selectConstructor rest) if length rest > 128
then pure (toEvalError ctx x TooManySumtypeCases)
else deftype ctx name (selectConstructor rest)
primitiveDeftype _ _ _ = error "primitivedeftype" primitiveDeftype _ _ _ = error "primitivedeftype"
type ModuleCreator = Context -> String -> [Ty] -> [XObj] -> Maybe Info -> Either TypeError (String, XObj, [XObj]) type ModuleCreator = Context -> String -> [Ty] -> [XObj] -> Maybe Info -> Either TypeError (String, XObj, [XObj])