Skip to content

Commit 0188264

Browse files
authored
feat: add compiler error on maximum sumtype constructors (#1319)
1 parent b745b1f commit 0188264

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

docs/LanguageGuide.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ Note that match works with *values* (not references) takes ownership over the va
237237

238238
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.
239239

240+
**Note:** A sumtype cannot have more than 128 inhabitants, also known as constructors. If that reads to you like a byte limitation, you’re on the right track. While this is a limitation, it has not proved to be a problem as of yet.
241+
240242
### Modules and Name Lookup
241243
Functions and variables can be stored in modules which are named and can be nested. To use a symbol inside a module
242244
you need to qualify it with the module name, like this: `Float.cos`.

src/PrimitiveError.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ data PrimitiveError
1919
| StructNotFound XObj
2020
| NonTypeInTypeEnv SymPath XObj
2121
| InvalidSumtypeCase XObj
22+
| TooManySumtypeCases
2223

2324
data PrimitiveWarning
2425
= NonExistentInterfaceWarning XObj
@@ -74,6 +75,8 @@ instance Show PrimitiveError where
7475
show (PrimitiveError.InvalidSumtypeCase xobj) =
7576
"Can't get members for an invalid sumtype case: "
7677
++ pretty xobj
78+
show TooManySumtypeCases =
79+
"Got too many sumtype cases (>128) for type"
7780

7881
instance Show PrimitiveWarning where
7982
show (NonExistentInterfaceWarning x) =

src/Primitives.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ primitiveMembers _ ctx xobj@(XObj (Sym path _) _ _) =
413413
go (XObj (Lst [(XObj (Deftype _) _ _), _, (XObj (Arr members) _ _)]) _ _) =
414414
pure (ctx, Right (XObj (Arr (map (\(a, b) -> XObj (Lst [a, b]) Nothing Nothing) (pairwise members))) Nothing Nothing))
415415
go (XObj (Lst ((XObj (DefSumtype _) _ _) : _ : cases)) _ _) =
416-
pure $ (ctx, (either Left (\a -> Right (XObj (Arr (concat a)) Nothing Nothing)) (mapM getMembersFromCase cases)))
416+
pure (ctx, (either Left (\a -> Right (XObj (Arr (concat a)) Nothing Nothing)) (mapM getMembersFromCase cases)))
417417
go x = pure (toEvalError ctx x (NonTypeInTypeEnv path x))
418418
getMembersFromCase :: XObj -> Either EvalError [XObj]
419419
getMembersFromCase (XObj (Lst members) _ _) =
@@ -576,8 +576,10 @@ primitiveDeftype xobj ctx (name : rest@(XObj (Arr a) _ _ : _)) =
576576
if all isUnqualifiedSym objs
577577
then deftype ctx name (selectConstructor rest)
578578
else pure (toEvalError ctx xobj (QualifiedTypeMember rest))
579-
primitiveDeftype _ ctx (name : rest) =
580-
deftype ctx name (selectConstructor rest)
579+
primitiveDeftype x ctx (name : rest) =
580+
if length rest > 128
581+
then pure (toEvalError ctx x TooManySumtypeCases)
582+
else deftype ctx name (selectConstructor rest)
581583
primitiveDeftype _ _ _ = error "primitivedeftype"
582584

583585
type ModuleCreator = Context -> String -> [Ty] -> [XObj] -> Maybe Info -> Either TypeError (String, XObj, [XObj])

0 commit comments

Comments
 (0)