Commit 3ee47002 authored by Austin Seipp's avatar Austin Seipp
Browse files

Make sure type literals aren't negative (#8306)



As Krzysztof pointed out in #8306, with NegativeLiterals and DataKinds,
definitions such as:

  type T = -1

were accepted, although type literals must be greater than zero.
Signed-off-by: default avatarAustin Seipp <austin@well-typed.com>
parent 5f212c86
...@@ -215,17 +215,23 @@ mkTopSpliceDecl (L _ (HsSpliceE (HsSplice _ expr))) = SpliceD (SpliceDecl expr ...@@ -215,17 +215,23 @@ mkTopSpliceDecl (L _ (HsSpliceE (HsSplice _ expr))) = SpliceD (SpliceDecl expr
mkTopSpliceDecl other_expr = SpliceD (SpliceDecl other_expr Implicit) mkTopSpliceDecl other_expr = SpliceD (SpliceDecl other_expr Implicit)
mkTyLit :: Located (HsTyLit) -> P (LHsType RdrName) -- Ensure a type literal is used correctly; notably, we need the proper extension enabled,
mkTyLit l = -- and if it's an integer literal, the literal must be >= 0. This can occur with
do allowed <- extension typeLiteralsEnabled -- -XNegativeLiterals enabled (see #8306)
if allowed mkTyLit :: Located HsTyLit -> P (LHsType RdrName)
then return (HsTyLit `fmap` l) mkTyLit lit = extension typeLiteralsEnabled >>= check
else parseErrorSDoc (getLoc l) where
(text "Illegal literal in type (use DataKinds to enable):" <+> negLit (L _ (HsStrTy _)) = False
ppr l) negLit (L _ (HsNumTy i)) = i < 0
check False =
parseErrorSDoc (getLoc lit)
(text "Illegal literal in type (use DataKinds to enable):" <+> ppr lit)
check True =
if not (negLit lit) then return (HsTyLit `fmap` lit)
else parseErrorSDoc (getLoc lit)
(text "Illegal literal in type (type literals must not be negative):" <+> ppr lit)
\end{code} \end{code}
%************************************************************************ %************************************************************************
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment