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

Commits:

11 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/Module.hs
    ... ... @@ -2572,7 +2572,12 @@ tcGhciStmts stmts
    2572 2572
           ; traceTc "GHC.Tc.Module.tcGhciStmts: tc stmts" empty
    
    2573 2573
           ; ((tc_stmts, ids), lie) <- captureTopConstraints $
    
    2574 2574
                                       tc_io_stmts $ \ _ ->
    
    2575
    -                                  mapM tcLookupId names
    
    2575
    +                                  for names (\ name -> do
    
    2576
    +                                    thing <- tcLookupTermBndr name
    
    2577
    +                                    case thing of
    
    2578
    +                                      Left ButATyVar {} -> failIllegalTyVar (noUserRdr name)
    
    2579
    +                                      Right id -> pure id
    
    2580
    +                                    )
    
    2576 2581
                             -- Look up the names right in the middle,
    
    2577 2582
                             -- where they will all be in scope
    
    2578 2583
     
    

  • 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/ghci/should_run/T28038_ghci.script
    1
    +:set -XRequiredTypeArguments
    
    2
    +data A where MkA :: forall a -> A
    
    3
    +MkA b <- pure (MkA Int)

  • testsuite/tests/ghci/should_run/T28038_ghci.stderr
    1
    +<interactive>:3:1: error: [GHC-01928]
    
    2
    +    • Illegal term-level use of the type variable ‘b’
    
    3
    +    • bound at <interactive>:3:5
    
    4
    +

  • testsuite/tests/ghci/should_run/all.T
    ... ... @@ -95,6 +95,7 @@ test('GhciMainIs', just_ghci, compile_and_run, ['-main-is otherMain'])
    95 95
     test('LargeBCO', [extra_files(['LargeBCO_A.hs']), req_th, extra_hc_opts("-O -fbyte-code-and-object-code -fprefer-byte-code")], compile_and_run, [''])
    
    96 96
     
    
    97 97
     test('T24115', just_ghci + [extra_run_opts("-e ':add T24115.hs'")], ghci_script, ['T24115.script'])
    
    98
    +test('T28038_ghci', just_ghci, ghci_script, ['T28038_ghci.script'])
    
    98 99
     
    
    99 100
     test('T10920', [only_ways(ghci_ways), extra_files(['LocalPrelude/Prelude.hs'])], ghci_script, ['T10920.script'])
    
    100 101
     test('TopEnvIface', [only_ways(ghci_ways)], makefile_test, [])
    

  • 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, [''])