[Git][ghc/ghc][master] Check for negative type literals in the type checker (#26861)
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: bd3eba86 by Vladislav Zavialov at 2026-02-27T05:48:01-05:00 Check for negative type literals in the type checker (#26861) GHC disallows negative type literals (e.g., -1), as tested by T8306 and T8412. This check is currently performed in the renamer: rnHsTyLit tyLit@(HsNumTy x i) = do when (i < 0) $ addErr $ TcRnNegativeNumTypeLiteral tyLit However, this check can be bypassed using RequiredTypeArguments (see the new test case T26861). Prior to this patch, such programs caused the compiler to hang instead of reporting a proper error. This patch addresses the issue by adding an equivalent check in the type checker, namely in tcHsType. The diff is deliberately minimal to facilitate backporting. A more comprehensive rework of HsTyLit is planned for a separate commit. - - - - - 4 changed files: - compiler/GHC/Tc/Gen/HsType.hs - + testsuite/tests/typecheck/should_fail/T26861.hs - + testsuite/tests/typecheck/should_fail/T26861.stderr - testsuite/tests/typecheck/should_fail/all.T Changes: ===================================== compiler/GHC/Tc/Gen/HsType.hs ===================================== @@ -1264,8 +1264,10 @@ tcHsType _ rn_ty@(HsStarTy _ _) exp_kind = checkExpKind rn_ty liftedTypeKind liftedTypeKind exp_kind --------- Literals -tcHsType _ rn_ty@(HsTyLit _ (HsNumTy _ n)) exp_kind - = do { checkWiredInTyCon naturalTyCon +tcHsType _ rn_ty@(HsTyLit _ (HsNumTy x n)) exp_kind + = do { when (n < 0) $ + addErr $ TcRnNegativeNumTypeLiteral (HsNumTy x n) + ; checkWiredInTyCon naturalTyCon ; checkExpKind rn_ty (mkNumLitTy n) naturalTy exp_kind } tcHsType _ rn_ty@(HsTyLit _ (HsStrTy _ s)) exp_kind ===================================== testsuite/tests/typecheck/should_fail/T26861.hs ===================================== @@ -0,0 +1,14 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE NegativeLiterals #-} +{-# LANGUAGE RequiredTypeArguments #-} + +module T26861 where + +import Data.Proxy +import GHC.TypeLits + +main :: IO () +main = print (natVis (-42)) + +natVis :: forall a -> KnownNat a => Integer +natVis n = natVal (Proxy @n) ===================================== testsuite/tests/typecheck/should_fail/T26861.stderr ===================================== @@ -0,0 +1,6 @@ +T26861.hs:11:23: error: [GHC-93632] + • Illegal literal in type (type literals must not be negative): -42 + • In the type ‘-42’ + In the first argument of ‘print’, namely ‘(natVis (-42))’ + In the expression: print (natVis (-42)) + ===================================== testsuite/tests/typecheck/should_fail/all.T ===================================== @@ -752,3 +752,4 @@ test('T23162a', normal, compile_fail, ['']) test('T23162b', normal, compile_fail, ['']) test('T23162c', normal, compile, ['']) test('T23162d', normal, compile, ['']) +test('T26861', normal, compile_fail, ['']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/bd3eba86180083a3bd1633994b9e6cd3... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/bd3eba86180083a3bd1633994b9e6cd3... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Marge Bot (@marge-bot)