Magnus pushed to branch wip/mangoiv/28038 at Glasgow Haskell Compiler / GHC

Commits:

7 changed files:

Changes:

  • changelog.d/28038
    1
    +section: compiler
    
    2
    +synopsis:
    
    3
    +  Fix a panic when trying to bind a type variable in patterns.
    
    4
    +description:
    
    5
    +  GHC currently does not allow to bind type variables in patterns. When
    
    6
    +  a user would try that, in some situations, we didn't abort eagerly
    
    7
    +  enough after discovering the error and thus ran into a panic looking up
    
    8
    +  the type in the env while trying to update the local context. Instead,
    
    9
    +  when discovering the error,
    
    10
    +  we now abort eagerly.
    
    11
    +mrs: !16868
    
    12
    +issues: #28038

  • compiler/GHC/Tc/Gen/Bind.hs
    ... ... @@ -1555,11 +1555,14 @@ lookupMBI :: Name -> TcM MonoBindInfo
    1555 1555
     -- names that lack a signature, which the pattern has brought
    
    1556 1556
     -- into scope.
    
    1557 1557
     lookupMBI name
    
    1558
    -  = do { mono_id <- tcLookupId name
    
    1559
    -       ; return (MBI { mbi_poly_name = name
    
    1560
    -                     , mbi_sig       = Nothing
    
    1561
    -                     , mbi_mono_id   = mono_id
    
    1562
    -                     , mbi_mono_mult = idMult mono_id }) }
    
    1558
    +  = do { mono_id_e <- tcLookupTermBndr name
    
    1559
    +       ; case mono_id_e of
    
    1560
    +         Left _ -> failIllegalTyVar (noUserRdr name)
    
    1561
    +         Right mono_id ->
    
    1562
    +           return (MBI { mbi_poly_name = name
    
    1563
    +                       , mbi_sig       = Nothing
    
    1564
    +                       , mbi_mono_id   = mono_id
    
    1565
    +                       , mbi_mono_mult = idMult mono_id }) }
    
    1563 1566
     
    
    1564 1567
     -------------------
    
    1565 1568
     tcLhsSigId :: LetBndrSpec -> (Name, TcIdSig) -> TcM MonoBindInfo
    

  • compiler/GHC/Tc/TyCl/PatSyn.hs
    ... ... @@ -651,12 +651,9 @@ collectPatSynArgInfo details =
    651 651
     -- type argument; then we report an illegal term-level use of @x@ (#27586).
    
    652 652
     tcLookupPatSynArg :: Name -> TcM Id
    
    653 653
     tcLookupPatSynArg arg_name
    
    654
    -  = do { thing <- tcLookup arg_name
    
    655
    -       ; case thing of
    
    656
    -           ATcId { tct_id = id } -> return id
    
    657
    -           AGlobal (AnId id)     -> return id
    
    658
    -           ATyVar {}             -> failIllegalTyVar (noUserRdr arg_name)
    
    659
    -           _                     -> pprPanic "tcLookupPatSynArg" (ppr arg_name) }
    
    654
    +  = do { tcLookupTermBndr arg_name >>= \ case
    
    655
    +         Left (ButATyVar _var) -> failIllegalTyVar (noUserRdr arg_name)
    
    656
    +         Right id -> return id }
    
    660 657
     
    
    661 658
     wrongNumberOfParmsErr :: Name -> Arity -> Arity -> TcM a
    
    662 659
     wrongNumberOfParmsErr name decl_arity missing
    

  • compiler/GHC/Tc/Utils/Env.hs
    ... ... @@ -43,6 +43,7 @@ module GHC.Tc.Utils.Env(
    43 43
     
    
    44 44
             tcLookup, tcLookupLocated, tcLookupLocalIds,
    
    45 45
             tcLookupId, tcLookupIdMaybe, tcLookupTyVar,
    
    46
    +        tcLookupTermBndr, NotATermBndr (..),
    
    46 47
             tcLookupTcTyCon,
    
    47 48
             tcLookupLcl_maybe,
    
    48 49
             getInLocalScope,
    
    ... ... @@ -766,6 +767,20 @@ tcLookupIdMaybe name
    766 767
                AGlobal (AnId id)    -> return $ Just id
    
    767 768
                _                    -> return Nothing }
    
    768 769
     
    
    770
    +-- | Encountered a thing that is not a term binder when
    
    771
    +--  calling 'tcLookupTermBndr'.
    
    772
    +newtype NotATermBndr
    
    773
    +  = ButATyVar TcTyVar
    
    774
    +
    
    775
    +tcLookupTermBndr :: Name -> TcM (Either NotATermBndr Id)
    
    776
    +tcLookupTermBndr name = do
    
    777
    +  thing <- tcLookup name
    
    778
    +  case thing of
    
    779
    +    ATcId { tct_id } -> pure $ Right $! tct_id
    
    780
    +    AGlobal (AnId id) -> pure $ Right $! id
    
    781
    +    ATyVar _ var -> pure $ Left $! ButATyVar var
    
    782
    +    _ -> pprPanic "tcLookupTermBndr: unexpected thing" (ppr thing)
    
    783
    +
    
    769 784
     tcLookupLocalIds :: [Name] -> TcM [TcId]
    
    770 785
     -- We expect the variables to all be bound, and all at
    
    771 786
     -- the same level as the lookup.  Only used in one place...
    

  • testsuite/tests/typecheck/should_fail/T28038.hs
    1
    +{-# LANGUAGE RequiredTypeArguments #-}
    
    2
    +module Main where
    
    3
    +
    
    4
    +data A where
    
    5
    +   A :: forall a -> A
    
    6
    +
    
    7
    +main :: IO ()
    
    8
    +main = do
    
    9
    +  let a = A Int
    
    10
    +  let A b = a
    
    11
    +  pure ()
    
    12
    +

  • testsuite/tests/typecheck/should_fail/T28038.stderr
    1
    +T28038.hs:10:7: error: [GHC-01928]
    
    2
    +    • Illegal term-level use of the type variable ‘b’
    
    3
    +    • bound at T28038.hs:10:9
    
    4
    +    • In a pattern binding: A b = a
    
    5
    +      In a stmt of a 'do' block: let A b = a
    
    6
    +      In the expression:
    
    7
    +        do let a = A Int
    
    8
    +           let A b = a
    
    9
    +           pure ()
    
    10
    +
    
    11
    +T28038.hs:10:7: error: [GHC-48361]
    
    12
    +    • Binding type variables is not allowed in pattern bindings
    
    13
    +    • In the pattern: A b
    
    14
    +      In a pattern binding: A b = a
    
    15
    +      In a stmt of a 'do' block: let A b = a
    
    16
    +

  • testsuite/tests/typecheck/should_fail/all.T
    ... ... @@ -768,3 +768,4 @@ test('T26532', normal, compile_fail, [''])
    768 768
     test('T27731', normal, compile_fail, [''])
    
    769 769
     test('T27844', normal, compile_fail, [''])
    
    770 770
     test('T26543a', normal, compile_fail, [''])
    
    771
    +test('T28038', normal, compile_fail, [''])