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

Commits:

6 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/Pat.hs
    ... ... @@ -1900,11 +1900,6 @@ tcConTyArg :: Subst -> Checker (HsTyPat GhcRn, TyVar) ()
    1900 1900
     tcConTyArg tenv penv (rn_ty, con_tv) thing_inside
    
    1901 1901
       = do { (sig_wcs, sig_ibs, arg_ty) <- tcHsTyPat rn_ty (substTy tenv (varType con_tv))
    
    1902 1902
     
    
    1903
    -       ; case NE.nonEmpty sig_ibs of
    
    1904
    -           Just sig_ibs_ne | inPatBind penv ->
    
    1905
    -             addErr (TcRnCannotBindTyVarsInPatBind sig_ibs_ne)
    
    1906
    -           _ -> pure ()
    
    1907
    -
    
    1908 1903
               -- This unification is straight from Figure 7 of
    
    1909 1904
               -- "Type Variables in Patterns", Haskell'18
    
    1910 1905
               -- OK to drop coercions here. These unifications are all about
    
    ... ... @@ -1912,6 +1907,14 @@ tcConTyArg tenv penv (rn_ty, con_tv) thing_inside
    1912 1907
               -- See Note [Type applications in patterns] (W1)
    
    1913 1908
            ; _ <- unifyType Nothing arg_ty (substTyVar tenv con_tv)
    
    1914 1909
     
    
    1910
    +       ; case NE.nonEmpty sig_ibs of
    
    1911
    +           Just sig_ibs_ne | inPatBind penv -> do
    
    1912
    +             addErr (TcRnCannotBindTyVarsInPatBind sig_ibs_ne)
    
    1913
    +             failIfErrsM
    
    1914
    +           _ -> pure ()
    
    1915
    +
    
    1916
    +
    
    1917
    +
    
    1915 1918
            ; result <- tcExtendNameTyVarEnv sig_wcs $
    
    1916 1919
                        tcExtendNameTyVarEnv sig_ibs $
    
    1917 1920
                        thing_inside
    

  • 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-48361]
    
    2
    +    • Binding type variables is not allowed in pattern bindings
    
    3
    +    • In the pattern: A b
    
    4
    +      In a pattern binding: A b = a
    
    5
    +      In a stmt of a 'do' block: let A b = a
    
    6
    +

  • testsuite/tests/typecheck/should_fail/TyAppPat_PatternBindingExistential.stderr
    ... ... @@ -3,18 +3,6 @@ TyAppPat_PatternBindingExistential.hs:10:1: error: [GHC-48361]
    3 3
         • In the pattern: Some @a x
    
    4 4
           In a pattern binding: Some @a x = Some (5 :: Integer)
    
    5 5
     
    
    6
    -TyAppPat_PatternBindingExistential.hs:10:9: error: [GHC-25897]
    
    7
    -    • Couldn't match expected type ‘p’ with actual type ‘a’
    
    8
    -      ‘a’ is a rigid type variable bound by
    
    9
    -        a pattern with constructor: Some :: forall a. a -> Some,
    
    10
    -        in a pattern binding
    
    11
    -        at TyAppPat_PatternBindingExistential.hs:10:1-9
    
    12
    -      ‘p’ is a rigid type variable bound by
    
    13
    -        the inferred type of x :: p
    
    14
    -        at TyAppPat_PatternBindingExistential.hs:10:1-31
    
    15
    -    • In the pattern: Some @a x
    
    16
    -      In a pattern binding: Some @a x = Some (5 :: Integer)
    
    17
    -
    
    18 6
     TyAppPat_PatternBindingExistential.hs:13:3: error: [GHC-39999]
    
    19 7
         • Ambiguous type variable ‘a0’ arising from a use of ‘print’
    
    20 8
           prevents the constraint ‘(Show a0)’ from being solved.
    

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