[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: 83c96c50 by mangoiv at 2026-11-17T18:02:53+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. A similar thing would happen in tcGhciStmts: that function uses the same mechanism to avoid that bug. - - - - - 11 changed files: - + changelog.d/28038 - compiler/GHC/Tc/Gen/Bind.hs - compiler/GHC/Tc/Module.hs - compiler/GHC/Tc/TyCl/PatSyn.hs - compiler/GHC/Tc/Utils/Env.hs - + testsuite/tests/ghci/should_run/T28038_ghci.script - + testsuite/tests/ghci/should_run/T28038_ghci.stderr - testsuite/tests/ghci/should_run/all.T - + testsuite/tests/typecheck/should_fail/T28038.hs - + testsuite/tests/typecheck/should_fail/T28038.stderr - testsuite/tests/typecheck/should_fail/all.T Changes: ===================================== changelog.d/28038 ===================================== @@ -0,0 +1,12 @@ +section: compiler +synopsis: + Fix a panic when trying to bind a type variable in patterns. +description: + GHC currently does not allow to bind type variables in patterns. When + a user would try that, in some situations, 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, + when discovering the error, + we now abort eagerly. +mrs: !16868 +issues: #28038 ===================================== 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/Module.hs ===================================== @@ -2572,7 +2572,12 @@ tcGhciStmts stmts ; traceTc "GHC.Tc.Module.tcGhciStmts: tc stmts" empty ; ((tc_stmts, ids), lie) <- captureTopConstraints $ tc_io_stmts $ \ _ -> - mapM tcLookupId names + for names (\ name -> do + thing <- tcLookupTermBndr name + case thing of + Left ButATyVar {} -> failIllegalTyVar (noUserRdr name) + Right id -> pure id + ) -- Look up the names right in the middle, -- where they will all be in scope ===================================== 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/ghci/should_run/T28038_ghci.script ===================================== @@ -0,0 +1,3 @@ +:set -XRequiredTypeArguments +data A where MkA :: forall a -> A +MkA b <- pure (MkA Int) ===================================== testsuite/tests/ghci/should_run/T28038_ghci.stderr ===================================== @@ -0,0 +1,4 @@ +<interactive>:3:1: error: [GHC-01928] + • Illegal term-level use of the type variable ‘b’ + • bound at <interactive>:3:5 + ===================================== testsuite/tests/ghci/should_run/all.T ===================================== @@ -95,6 +95,7 @@ test('GhciMainIs', just_ghci, compile_and_run, ['-main-is otherMain']) test('LargeBCO', [extra_files(['LargeBCO_A.hs']), req_th, extra_hc_opts("-O -fbyte-code-and-object-code -fprefer-byte-code")], compile_and_run, ['']) test('T24115', just_ghci + [extra_run_opts("-e ':add T24115.hs'")], ghci_script, ['T24115.script']) +test('T28038_ghci', just_ghci, ghci_script, ['T28038_ghci.script']) test('T10920', [only_ways(ghci_ways), extra_files(['LocalPrelude/Prelude.hs'])], ghci_script, ['T10920.script']) test('TopEnvIface', [only_ways(ghci_ways)], makefile_test, []) ===================================== 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/83c96c50eb6137da9d07899feb974c94... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/83c96c50eb6137da9d07899feb974c94... 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)