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 <>
parent 5f212c86
......@@ -215,17 +215,23 @@ mkTopSpliceDecl (L _ (HsSpliceE (HsSplice _ expr))) = SpliceD (SpliceDecl expr
mkTopSpliceDecl other_expr = SpliceD (SpliceDecl other_expr Implicit)
mkTyLit :: Located (HsTyLit) -> P (LHsType RdrName)
mkTyLit l =
do allowed <- extension typeLiteralsEnabled
if allowed
then return (HsTyLit `fmap` l)
else parseErrorSDoc (getLoc l)
(text "Illegal literal in type (use DataKinds to enable):" <+>
ppr l)
-- Ensure a type literal is used correctly; notably, we need the proper extension enabled,
-- and if it's an integer literal, the literal must be >= 0. This can occur with
-- -XNegativeLiterals enabled (see #8306)
mkTyLit :: Located HsTyLit -> P (LHsType RdrName)
mkTyLit lit = extension typeLiteralsEnabled >>= check
negLit (L _ (HsStrTy _)) = False
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)
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