[Git][ghc/ghc][wip/mangoiv/28038] typechecker: use safe lookup for lookupMBI
Magnus pushed to branch wip/mangoiv/28038 at Glasgow Haskell Compiler / GHC Commits: 6d04f396 by mangoiv at 2026-11-17T17:30:50+01:00 typechecker: use safe lookup for lookupMBI GHC currently does not allow to bind type variables in patterns. When a user would try that, we didn't abort eagerly enough after discovering the error and thus ran into a panic looking up the type in the env while trying to update the local context. Instead, we now fail gracefully when discovering we're trying to look up a type variable in lookupMBI. - - - - - 6 changed files: - compiler/GHC/Tc/Gen/Bind.hs - compiler/GHC/Tc/TyCl/PatSyn.hs - compiler/GHC/Tc/Utils/Env.hs - + testsuite/tests/typecheck/should_fail/T28038.hs - + testsuite/tests/typecheck/should_fail/T28038.stderr - testsuite/tests/typecheck/should_fail/all.T Changes: ===================================== compiler/GHC/Tc/Gen/Bind.hs ===================================== @@ -1555,11 +1555,14 @@ lookupMBI :: Name -> TcM MonoBindInfo -- names that lack a signature, which the pattern has brought -- into scope. lookupMBI name - = do { mono_id <- tcLookupId name - ; return (MBI { mbi_poly_name = name - , mbi_sig = Nothing - , mbi_mono_id = mono_id - , mbi_mono_mult = idMult mono_id }) } + = do { mono_id_e <- tcLookupTermBndr name + ; case mono_id_e of + Left _ -> failIllegalTyVar (noUserRdr name) + Right mono_id -> + return (MBI { mbi_poly_name = name + , mbi_sig = Nothing + , mbi_mono_id = mono_id + , mbi_mono_mult = idMult mono_id }) } ------------------- tcLhsSigId :: LetBndrSpec -> (Name, TcIdSig) -> TcM MonoBindInfo ===================================== compiler/GHC/Tc/TyCl/PatSyn.hs ===================================== @@ -651,12 +651,9 @@ collectPatSynArgInfo details = -- type argument; then we report an illegal term-level use of @x@ (#27586). tcLookupPatSynArg :: Name -> TcM Id tcLookupPatSynArg arg_name - = do { thing <- tcLookup arg_name - ; case thing of - ATcId { tct_id = id } -> return id - AGlobal (AnId id) -> return id - ATyVar {} -> failIllegalTyVar (noUserRdr arg_name) - _ -> pprPanic "tcLookupPatSynArg" (ppr arg_name) } + = do { tcLookupTermBndr arg_name >>= \ case + Left (ButATyVar _var) -> failIllegalTyVar (noUserRdr arg_name) + Right id -> return id } wrongNumberOfParmsErr :: Name -> Arity -> Arity -> TcM a wrongNumberOfParmsErr name decl_arity missing ===================================== compiler/GHC/Tc/Utils/Env.hs ===================================== @@ -43,6 +43,7 @@ module GHC.Tc.Utils.Env( tcLookup, tcLookupLocated, tcLookupLocalIds, tcLookupId, tcLookupIdMaybe, tcLookupTyVar, + tcLookupTermBndr, NotATermBndr (..), tcLookupTcTyCon, tcLookupLcl_maybe, getInLocalScope, @@ -766,6 +767,20 @@ tcLookupIdMaybe name AGlobal (AnId id) -> return $ Just id _ -> return Nothing } +-- | Encountered a thing that is not a term binder when +-- calling 'tcLookupTermBndr'. +newtype NotATermBndr + = ButATyVar TcTyVar + +tcLookupTermBndr :: Name -> TcM (Either NotATermBndr Id) +tcLookupTermBndr name = do + thing <- tcLookup name + case thing of + ATcId { tct_id } -> pure $ Right $! tct_id + AGlobal (AnId id) -> pure $ Right $! id + ATyVar _ var -> pure $ Left $! ButATyVar var + _ -> pprPanic "tcLookupTermBndr: unexpected thing" (ppr thing) + tcLookupLocalIds :: [Name] -> TcM [TcId] -- We expect the variables to all be bound, and all at -- the same level as the lookup. Only used in one place... ===================================== testsuite/tests/typecheck/should_fail/T28038.hs ===================================== @@ -0,0 +1,12 @@ +{-# LANGUAGE RequiredTypeArguments #-} +module Main where + +data A where + A :: forall a -> A + +main :: IO () +main = do + let a = A Int + let A b = a + pure () + ===================================== testsuite/tests/typecheck/should_fail/T28038.stderr ===================================== @@ -0,0 +1,16 @@ +T28038.hs:10:7: error: [GHC-01928] + • Illegal term-level use of the type variable ‘b’ + • bound at T28038.hs:10:9 + • In a pattern binding: A b = a + In a stmt of a 'do' block: let A b = a + In the expression: + do let a = A Int + let A b = a + pure () + +T28038.hs:10:7: error: [GHC-48361] + • Binding type variables is not allowed in pattern bindings + • In the pattern: A b + In a pattern binding: A b = a + In a stmt of a 'do' block: let A b = a + ===================================== testsuite/tests/typecheck/should_fail/all.T ===================================== @@ -768,3 +768,4 @@ test('T26532', normal, compile_fail, ['']) test('T27731', normal, compile_fail, ['']) test('T27844', normal, compile_fail, ['']) test('T26543a', normal, compile_fail, ['']) +test('T28038', normal, compile_fail, ['']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6d04f396c0083570db0742341a1166f9... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6d04f396c0083570db0742341a1166f9... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Magnus (@MangoIV)