Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC

Commits:

9 changed files:

Changes:

  • compiler/GHC/HsToCore/Pmc/Solver/Types.hs
    ... ... @@ -64,7 +64,7 @@ import GHC.Builtin.Names
    64 64
     import GHC.Builtin.Types
    
    65 65
     import GHC.Builtin.Types.Prim
    
    66 66
     import GHC.Tc.Solver.InertSet (InertSet, emptyInert)
    
    67
    -import GHC.Tc.Utils.TcType (isStringTy)
    
    67
    +import GHC.Tc.Utils.TcType (isStringTy, topTcLevel)
    
    68 68
     import GHC.Types.CompleteMatch
    
    69 69
     import GHC.Types.SourceText (SourceText(..), mkFractionalLit, FractionalLit
    
    70 70
                                 , fractionalLitFromRational
    
    ... ... @@ -129,7 +129,7 @@ instance Outputable TyState where
    129 129
       ppr (TySt n inert) = ppr n <+> ppr inert
    
    130 130
     
    
    131 131
     initTyState :: TyState
    
    132
    -initTyState = TySt 0 emptyInert
    
    132
    +initTyState = TySt 0 (emptyInert topTcLevel)
    
    133 133
     
    
    134 134
     -- | The term oracle state. Stores 'VarInfo' for encountered 'Id's. These
    
    135 135
     -- entries are possibly shared when we figure out that two variables must be
    

  • compiler/GHC/Tc/Solver.hs
    ... ... @@ -915,21 +915,22 @@ simplifyInfer top_lvl rhs_tclvl infer_mode sigs name_taus wanteds
    915 915
            ; let psig_theta = concatMap sig_inst_theta partial_sigs
    
    916 916
     
    
    917 917
            -- First do full-blown solving
    
    918
    -       -- NB: we must gather up all the bindings from doing
    
    919
    -       -- this solving; hence (runTcSWithEvBinds ev_binds_var).
    
    920
    -       -- And note that since there are nested implications,
    
    921
    -       -- calling solveWanteds will side-effect their evidence
    
    922
    -       -- bindings, so we can't just revert to the input
    
    923
    -       -- constraint.
    
    924
    -
    
    918
    +       -- NB: we must gather up all the bindings from doing this solving; hence
    
    919
    +       -- (runTcSWithEvBinds ev_binds_var).  And note that since there are
    
    920
    +       -- nested implications, calling solveWanteds will side-effect their
    
    921
    +       -- evidence bindings, so we can't just revert to the input constraint.
    
    922
    +       --
    
    923
    +       -- See also Note [Inferring principal types]
    
    925 924
            ; ev_binds_var <- TcM.newTcEvBinds
    
    926 925
            ; psig_evs     <- newWanteds AnnOrigin psig_theta
    
    927 926
            ; wanted_transformed
    
    928
    -            <- setTcLevel rhs_tclvl $
    
    929
    -               runTcSWithEvBinds ev_binds_var $
    
    927
    +            <- runTcSWithEvBinds ev_binds_var $
    
    928
    +               setTcLevelTcS rhs_tclvl        $
    
    930 929
                    solveWanteds (mkSimpleWC psig_evs `andWC` wanteds)
    
    930
    +               -- setLevelTcS: we do setLevel /inside/ the runTcS, so that
    
    931
    +               --              we initialise the InertSet inert_given_eq_lvl as far
    
    932
    +               --              out as possible, maximising oppportunities to unify
    
    931 933
                    -- psig_evs : see Note [Add signature contexts as wanteds]
    
    932
    -               -- See Note [Inferring principal types]
    
    933 934
     
    
    934 935
            -- Find quant_pred_candidates, the predicates that
    
    935 936
            -- we'll consider quantifying over
    
    ... ... @@ -1430,13 +1431,15 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted
    1430 1431
     
    
    1431 1432
                  -- Step 1 of Note [decideAndPromoteTyVars]
    
    1432 1433
                  -- Get candidate constraints, decide which we can potentially quantify
    
    1433
    -             (can_quant_cts, no_quant_cts) = approximateWCX wanted
    
    1434
    +             -- The `no_quant_tvs` are free in constraints we can't quantify.
    
    1435
    +             (can_quant_cts, no_quant_tvs) = approximateWCX False wanted
    
    1434 1436
                  can_quant = ctsPreds can_quant_cts
    
    1435
    -             no_quant  = ctsPreds no_quant_cts
    
    1437
    +             can_quant_tvs = tyCoVarsOfTypes can_quant
    
    1436 1438
     
    
    1437 1439
                  -- Step 2 of Note [decideAndPromoteTyVars]
    
    1438 1440
                  -- Apply the monomorphism restriction
    
    1439 1441
                  (post_mr_quant, mr_no_quant) = applyMR dflags infer_mode can_quant
    
    1442
    +             mr_no_quant_tvs              = tyCoVarsOfTypes mr_no_quant
    
    1440 1443
     
    
    1441 1444
                  -- The co_var_tvs are tvs mentioned in the types of covars or
    
    1442 1445
                  -- coercion holes. We can't quantify over these covars, so we
    
    ... ... @@ -1448,30 +1451,33 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted
    1448 1451
                                              ++ tau_tys ++ post_mr_quant)
    
    1449 1452
                  co_var_tvs = closeOverKinds co_vars
    
    1450 1453
     
    
    1451
    -             -- outer_tvs are mentioned in `wanted, and belong to some outer level.
    
    1454
    +             -- outer_tvs are mentioned in `wanted`, and belong to some outer level.
    
    1452 1455
                  -- We definitely can't quantify over them
    
    1453 1456
                  outer_tvs = outerLevelTyVars rhs_tclvl $
    
    1454
    -                         tyCoVarsOfTypes can_quant `unionVarSet` tyCoVarsOfTypes no_quant
    
    1457
    +                         can_quant_tvs `unionVarSet` no_quant_tvs
    
    1455 1458
     
    
    1456
    -             -- Step 3 of Note [decideAndPromoteTyVars]
    
    1459
    +             -- Step 3 of Note [decideAndPromoteTyVars], (a-c)
    
    1457 1460
                  -- Identify mono_tvs: the type variables that we must not quantify over
    
    1461
    +             -- At top level we are much less keen to create mono tyvars, to avoid
    
    1462
    +             -- spooky action at a distance.
    
    1458 1463
                  mono_tvs_without_mr
    
    1459
    -               | is_top_level = outer_tvs
    
    1460
    -               | otherwise    = outer_tvs                                 -- (a)
    
    1461
    -                                `unionVarSet` tyCoVarsOfTypes no_quant    -- (b)
    
    1462
    -                                `unionVarSet` co_var_tvs                  -- (c)
    
    1464
    +               | is_top_level = outer_tvs    -- See (DP2)
    
    1465
    +               | otherwise    = outer_tvs                    -- (a)
    
    1466
    +                                `unionVarSet` no_quant_tvs   -- (b)
    
    1467
    +                                `unionVarSet` co_var_tvs     -- (c)
    
    1463 1468
     
    
    1469
    +             -- Step 3 of Note [decideAndPromoteTyVars], (d)
    
    1464 1470
                  mono_tvs_with_mr
    
    1465 1471
                    = -- Even at top level, we don't quantify over type variables
    
    1466 1472
                      -- mentioned in constraints that the MR tells us not to quantify
    
    1467 1473
                      -- See Note [decideAndPromoteTyVars] (DP2)
    
    1468
    -                 mono_tvs_without_mr `unionVarSet` tyCoVarsOfTypes mr_no_quant
    
    1474
    +                 mono_tvs_without_mr `unionVarSet` mr_no_quant_tvs
    
    1469 1475
     
    
    1470 1476
                  --------------------------------------------------------------------
    
    1471 1477
                  -- Step 4 of Note [decideAndPromoteTyVars]
    
    1472 1478
                  -- Use closeWrtFunDeps to find any other variables that are determined by mono_tvs
    
    1473
    -             add_determined tvs = closeWrtFunDeps post_mr_quant tvs
    
    1474
    -                                  `delVarSetList` psig_qtvs
    
    1479
    +             add_determined tvs preds = closeWrtFunDeps preds tvs
    
    1480
    +                                        `delVarSetList` psig_qtvs
    
    1475 1481
                      -- Why delVarSetList psig_qtvs?
    
    1476 1482
                      -- If the user has explicitly asked for quantification, then that
    
    1477 1483
                      -- request "wins" over the MR.
    
    ... ... @@ -1480,8 +1486,8 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted
    1480 1486
                      -- (i.e. says "no" to isQuantifiableTv)? That's OK: explanation
    
    1481 1487
                      -- in Step 2 of Note [Deciding quantification].
    
    1482 1488
     
    
    1483
    -             mono_tvs_with_mr_det    = add_determined mono_tvs_with_mr
    
    1484
    -             mono_tvs_without_mr_det = add_determined mono_tvs_without_mr
    
    1489
    +             mono_tvs_with_mr_det    = add_determined mono_tvs_with_mr    post_mr_quant
    
    1490
    +             mono_tvs_without_mr_det = add_determined mono_tvs_without_mr can_quant
    
    1485 1491
     
    
    1486 1492
                  --------------------------------------------------------------------
    
    1487 1493
                  -- Step 5 of Note [decideAndPromoteTyVars]
    
    ... ... @@ -1518,7 +1524,7 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted
    1518 1524
                , text "newly_mono_tvs =" <+> ppr newly_mono_tvs
    
    1519 1525
                , text "can_quant =" <+> ppr can_quant
    
    1520 1526
                , text "post_mr_quant =" <+> ppr post_mr_quant
    
    1521
    -           , text "no_quant =" <+> ppr no_quant
    
    1527
    +           , text "no_quant_tvs =" <+> ppr no_quant_tvs
    
    1522 1528
                , text "mr_no_quant =" <+> ppr mr_no_quant
    
    1523 1529
                , text "final_quant =" <+> ppr final_quant
    
    1524 1530
                , text "co_vars =" <+> ppr co_vars ]
    
    ... ... @@ -1605,8 +1611,8 @@ The plan
    1605 1611
       The body of z tries to unify the type of x (call it alpha[1]) with
    
    1606 1612
       (beta[2] -> gamma[2]). This unification fails because alpha is untouchable, leaving
    
    1607 1613
            [W] alpha[1] ~ (beta[2] -> gamma[2])
    
    1608
    -  We need to know not to quantify over beta or gamma, because they are in the
    
    1609
    -  equality constraint with alpha. Actual test case:   typecheck/should_compile/tc213
    
    1614
    +  We don't want to quantify over beta or gamma because they are fixed by alpha,
    
    1615
    +  which is monomorphic. Actual test case:   typecheck/should_compile/tc213
    
    1610 1616
     
    
    1611 1617
       Another example. Suppose we have
    
    1612 1618
           class C a b | a -> b
    
    ... ... @@ -1643,9 +1649,22 @@ Wrinkles
    1643 1649
       promote type variables.  But for bindings affected by the MR we have no choice
    
    1644 1650
       but to promote.
    
    1645 1651
     
    
    1652
    +  An example is in #26004.
    
    1653
    +      f w e = case e of
    
    1654
    +        T1 -> let y = not w in False
    
    1655
    +        T2 -> True
    
    1656
    +  When generalising `f` we have a constraint
    
    1657
    +      forall. (a ~ Bool) => alpha ~ Bool
    
    1658
    +  where our provisional type for `f` is `f :: T alpha -> blah`.
    
    1659
    +  In a /nested/ setting, we might simply not-generalise `f`, hoping to learn
    
    1660
    +  about `alpha` from f's call sites (test T5266b is an example).  But at top
    
    1661
    +  level, to avoid spooky action at a distance.
    
    1662
    +
    
    1646 1663
     Note [The top-level Any principle]
    
    1647 1664
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1648
    -Key principle: we never want to show the programmer a type with `Any` in it.
    
    1665
    +Key principles:
    
    1666
    +  * we never want to show the programmer a type with `Any` in it.
    
    1667
    +  * avoid "spooky action at a distance" and silent defaulting
    
    1649 1668
     
    
    1650 1669
     Most /top level/ bindings have a type signature, so none of this arises.  But
    
    1651 1670
     where a top-level binding lacks a signature, we don't want to infer a type like
    
    ... ... @@ -1654,11 +1673,18 @@ and then subsequently default alpha[0]:=Any. Exposing `Any` to the user is bad
    1654 1673
     bad bad.  Better to report an error, which is what may well happen if we
    
    1655 1674
     quantify over alpha instead.
    
    1656 1675
     
    
    1676
    +Moreover,
    
    1677
    + * If (elsewhere in this module) we add a call to `f`, say (f True), then
    
    1678
    +   `f` will get the type `Bool -> Int`
    
    1679
    + * If we add /another/ call, say (f 'x'), we will then get a type error.
    
    1680
    + * If we have no calls, the final exported type of `f` may get set by
    
    1681
    +   defaulting, and might not be principal (#26004).
    
    1682
    +
    
    1657 1683
     For /nested/ bindings, a monomorphic type like `f :: alpha[0] -> Int` is fine,
    
    1658 1684
     because we can see all the call sites of `f`, and they will probably fix
    
    1659 1685
     `alpha`.  In contrast, we can't see all of (or perhaps any of) the calls of
    
    1660 1686
     top-level (exported) functions, reducing the worries about "spooky action at a
    
    1661
    -distance".
    
    1687
    +distance".  This also moves in the direction of `MonoLocalBinds`, which we like.
    
    1662 1688
     
    
    1663 1689
     Note [Do not quantify over constraints that determine a variable]
    
    1664 1690
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

  • compiler/GHC/Tc/Solver/InertSet.hs
    ... ... @@ -374,20 +374,20 @@ instance Outputable InertSet where
    374 374
              where
    
    375 375
                dicts = bagToList (dictsToBag solved_dicts)
    
    376 376
     
    
    377
    -emptyInertCans :: InertCans
    
    378
    -emptyInertCans
    
    377
    +emptyInertCans :: TcLevel -> InertCans
    
    378
    +emptyInertCans given_eq_lvl
    
    379 379
       = IC { inert_eqs          = emptyTyEqs
    
    380 380
            , inert_funeqs       = emptyFunEqs
    
    381
    -       , inert_given_eq_lvl = topTcLevel
    
    381
    +       , inert_given_eq_lvl = given_eq_lvl
    
    382 382
            , inert_given_eqs    = False
    
    383 383
            , inert_dicts        = emptyDictMap
    
    384 384
            , inert_safehask     = emptyDictMap
    
    385 385
            , inert_insts        = []
    
    386 386
            , inert_irreds       = emptyBag }
    
    387 387
     
    
    388
    -emptyInert :: InertSet
    
    389
    -emptyInert
    
    390
    -  = IS { inert_cans           = emptyInertCans
    
    388
    +emptyInert :: TcLevel -> InertSet
    
    389
    +emptyInert given_eq_lvl
    
    390
    +  = IS { inert_cans           = emptyInertCans given_eq_lvl
    
    391 391
            , inert_cycle_breakers = emptyBag :| []
    
    392 392
            , inert_famapp_cache   = emptyFunEqs
    
    393 393
            , inert_solved_dicts   = emptyDictMap }
    
    ... ... @@ -678,6 +678,23 @@ should update inert_given_eq_lvl?
    678 678
        imply nominal ones. For example, if (G a ~R G b) and G's argument's
    
    679 679
        role is nominal, then we can deduce a ~N b.
    
    680 680
     
    
    681
    +(TGE6) A subtle point is this: when initialising the solver, giving it
    
    682
    +   an empty InertSet, we must conservatively initialise `inert_given_lvl`
    
    683
    +   to the /current/ TcLevel.  This matters when doing let-generalisation.
    
    684
    +   Consider #26004:
    
    685
    +      f w e = case e of
    
    686
    +                  T1 -> let y = not w in False   -- T1 is a GADT
    
    687
    +                  T2 -> True
    
    688
    +   When let-generalising `y`, we will have (w :: alpha[1]) in the type
    
    689
    +   envt; and we are under GADT pattern match.  So when we solve the
    
    690
    +   constraints from y's RHS, in simplifyInfer, we must NOT unify
    
    691
    +       alpha[1] := Bool
    
    692
    +   Since we don't know what enclosing equalities there are, we just
    
    693
    +   conservatively assume that there are some.
    
    694
    +
    
    695
    +   This initialisation in done in `runTcSWithEvBinds`, which passes
    
    696
    +   the current TcLevl to `emptyInert`.
    
    697
    +
    
    681 698
     Historical note: prior to #24938 we also ignored Given equalities that
    
    682 699
     did not mention an "outer" type variable.  But that is wrong, as #24938
    
    683 700
     showed. Another example is immortalised in test LocalGivenEqs2
    

  • compiler/GHC/Tc/Solver/Monad.hs
    ... ... @@ -20,7 +20,7 @@ module GHC.Tc.Solver.Monad (
    20 20
         runTcSSpecPrag,
    
    21 21
         failTcS, warnTcS, addErrTcS, wrapTcS, ctLocWarnTcS,
    
    22 22
         runTcSEqualities,
    
    23
    -    nestTcS, nestImplicTcS, setEvBindsTcS,
    
    23
    +    nestTcS, nestImplicTcS, setEvBindsTcS, setTcLevelTcS,
    
    24 24
         emitImplicationTcS, emitTvImplicationTcS,
    
    25 25
         emitImplication,
    
    26 26
         emitFunDepWanteds,
    
    ... ... @@ -947,8 +947,9 @@ added. This is initialised from the innermost implication constraint.
    947 947
     -- | See Note [TcSMode]
    
    948 948
     data TcSMode
    
    949 949
       = TcSVanilla    -- ^ Normal constraint solving
    
    950
    +  | TcSPMCheck    -- ^ Used when doing patterm match overlap checks
    
    950 951
       | TcSEarlyAbort -- ^ Abort early on insoluble constraints
    
    951
    -  | TcSSpecPrag -- ^ Fully solve all constraints
    
    952
    +  | TcSSpecPrag   -- ^ Fully solve all constraints
    
    952 953
       deriving (Eq)
    
    953 954
     
    
    954 955
     {- Note [TcSMode]
    
    ... ... @@ -957,6 +958,11 @@ The constraint solver can operate in different modes:
    957 958
     
    
    958 959
     * TcSVanilla: Normal constraint solving mode. This is the default.
    
    959 960
     
    
    961
    +* TcSPMCheck: Used by the pattern match overlap checker.
    
    962
    +      Like TcSVanilla, but the idea is that the returned InertSet will
    
    963
    +      later be resumed, so we do not want to restore type-equality cycles
    
    964
    +      See also Note [Type equality cycles] in GHC.Tc.Solver.Equality
    
    965
    +
    
    960 966
     * TcSEarlyAbort: Abort (fail in the monad) as soon as we come across an
    
    961 967
       insoluble constraint. This is used to fail-fast when checking for hole-fits.
    
    962 968
       See Note [Speeding up valid hole-fits].
    
    ... ... @@ -1135,7 +1141,7 @@ runTcS tcs
    1135 1141
     runTcSEarlyAbort :: TcS a -> TcM a
    
    1136 1142
     runTcSEarlyAbort tcs
    
    1137 1143
       = do { ev_binds_var <- TcM.newTcEvBinds
    
    1138
    -       ; runTcSWithEvBinds' True TcSEarlyAbort ev_binds_var tcs }
    
    1144
    +       ; runTcSWithEvBinds' TcSEarlyAbort ev_binds_var tcs }
    
    1139 1145
     
    
    1140 1146
     -- | Run the 'TcS' monad in 'TcSSpecPrag' mode, which either fully solves
    
    1141 1147
     -- individual Wanted quantified constraints or leaves them alone.
    
    ... ... @@ -1143,7 +1149,7 @@ runTcSEarlyAbort tcs
    1143 1149
     -- See Note [TcSSpecPrag].
    
    1144 1150
     runTcSSpecPrag :: EvBindsVar -> TcS a -> TcM a
    
    1145 1151
     runTcSSpecPrag ev_binds_var tcs
    
    1146
    -  = runTcSWithEvBinds' True TcSSpecPrag ev_binds_var tcs
    
    1152
    +  = runTcSWithEvBinds' TcSSpecPrag ev_binds_var tcs
    
    1147 1153
     
    
    1148 1154
     {- Note [TcSSpecPrag]
    
    1149 1155
     ~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -1200,7 +1206,7 @@ runTcSEqualities thing_inside
    1200 1206
     runTcSInerts :: InertSet -> TcS a -> TcM (a, InertSet)
    
    1201 1207
     runTcSInerts inerts tcs = do
    
    1202 1208
       ev_binds_var <- TcM.newTcEvBinds
    
    1203
    -  runTcSWithEvBinds' False TcSVanilla ev_binds_var $ do
    
    1209
    +  runTcSWithEvBinds' TcSPMCheck ev_binds_var $ do
    
    1204 1210
         setInertSet inerts
    
    1205 1211
         a <- tcs
    
    1206 1212
         new_inerts <- getInertSet
    
    ... ... @@ -1209,21 +1215,23 @@ runTcSInerts inerts tcs = do
    1209 1215
     runTcSWithEvBinds :: EvBindsVar
    
    1210 1216
                       -> TcS a
    
    1211 1217
                       -> TcM a
    
    1212
    -runTcSWithEvBinds = runTcSWithEvBinds' True TcSVanilla
    
    1218
    +runTcSWithEvBinds = runTcSWithEvBinds' TcSVanilla
    
    1213 1219
     
    
    1214
    -runTcSWithEvBinds' :: Bool  -- True <=> restore type equality cycles
    
    1215
    -                           -- Don't if you want to reuse the InertSet.
    
    1216
    -                           -- See also Note [Type equality cycles]
    
    1217
    -                           -- in GHC.Tc.Solver.Equality
    
    1218
    -                   -> TcSMode
    
    1220
    +runTcSWithEvBinds' :: TcSMode
    
    1219 1221
                        -> EvBindsVar
    
    1220 1222
                        -> TcS a
    
    1221 1223
                        -> TcM a
    
    1222
    -runTcSWithEvBinds' restore_cycles mode ev_binds_var tcs
    
    1224
    +runTcSWithEvBinds' mode ev_binds_var tcs
    
    1223 1225
       = do { unified_var <- TcM.newTcRef 0
    
    1224
    -       ; step_count <- TcM.newTcRef 0
    
    1225
    -       ; inert_var <- TcM.newTcRef emptyInert
    
    1226
    -       ; wl_var <- TcM.newTcRef emptyWorkList
    
    1226
    +       ; step_count  <- TcM.newTcRef 0
    
    1227
    +
    
    1228
    +       -- Make a fresh, empty inert set
    
    1229
    +       -- Subtle point: see (TGE6) in Note [Tracking Given equalities]
    
    1230
    +       --               in GHC.Tc.Solver.InertSet
    
    1231
    +       ; tc_lvl      <- TcM.getTcLevel
    
    1232
    +       ; inert_var   <- TcM.newTcRef (emptyInert tc_lvl)
    
    1233
    +
    
    1234
    +       ; wl_var      <- TcM.newTcRef emptyWorkList
    
    1227 1235
            ; unif_lvl_var <- TcM.newTcRef Nothing
    
    1228 1236
            ; let env = TcSEnv { tcs_ev_binds           = ev_binds_var
    
    1229 1237
                               , tcs_unified            = unified_var
    
    ... ... @@ -1240,9 +1248,13 @@ runTcSWithEvBinds' restore_cycles mode ev_binds_var tcs
    1240 1248
            ; when (count > 0) $
    
    1241 1249
              csTraceTcM $ return (text "Constraint solver steps =" <+> int count)
    
    1242 1250
     
    
    1243
    -       ; when restore_cycles $
    
    1244
    -         do { inert_set <- TcM.readTcRef inert_var
    
    1245
    -            ; restoreTyVarCycles inert_set }
    
    1251
    +       -- Restore tyvar cycles: see Note [Type equality cycles] in
    
    1252
    +       --                       GHC.Tc.Solver.Equality
    
    1253
    +       -- But /not/ in TCsPMCheck mode: see Note [TcSMode]
    
    1254
    +       ; case mode of
    
    1255
    +            TcSPMCheck -> return ()
    
    1256
    +            _ -> do { inert_set <- TcM.readTcRef inert_var
    
    1257
    +                    ; restoreTyVarCycles inert_set }
    
    1246 1258
     
    
    1247 1259
     #if defined(DEBUG)
    
    1248 1260
            ; ev_binds <- TcM.getTcEvBindsMap ev_binds_var
    
    ... ... @@ -1284,6 +1296,10 @@ setEvBindsTcS :: EvBindsVar -> TcS a -> TcS a
    1284 1296
     setEvBindsTcS ref (TcS thing_inside)
    
    1285 1297
      = TcS $ \ env -> thing_inside (env { tcs_ev_binds = ref })
    
    1286 1298
     
    
    1299
    +setTcLevelTcS :: TcLevel -> TcS a -> TcS a
    
    1300
    +setTcLevelTcS lvl (TcS thing_inside)
    
    1301
    + = TcS $ \ env -> TcM.setTcLevel lvl (thing_inside env)
    
    1302
    +
    
    1287 1303
     nestImplicTcS :: EvBindsVar
    
    1288 1304
                   -> TcLevel -> TcS a
    
    1289 1305
                   -> TcS a
    

  • compiler/GHC/Tc/Types/Constraint.hs
    ... ... @@ -1743,24 +1743,21 @@ will be able to report a more informative error:
    1743 1743
     ************************************************************************
    
    1744 1744
     -}
    
    1745 1745
     
    
    1746
    -type ApproxWC = ( Bag Ct    -- Free quantifiable constraints
    
    1747
    -                , Bag Ct )  -- Free non-quantifiable constraints
    
    1748
    -                            -- due to shape, or enclosing equality
    
    1746
    +type ApproxWC = ( Bag Ct          -- Free quantifiable constraints
    
    1747
    +                , TcTyCoVarSet )  -- Free vars of non-quantifiable constraints
    
    1748
    +                                  -- due to shape, or enclosing equality
    
    1749 1749
     
    
    1750 1750
     approximateWC :: Bool -> WantedConstraints -> Bag Ct
    
    1751 1751
     approximateWC include_non_quantifiable cts
    
    1752
    -  | include_non_quantifiable = quant `unionBags` no_quant
    
    1753
    -  | otherwise                = quant
    
    1754
    -  where
    
    1755
    -    (quant, no_quant) = approximateWCX cts
    
    1752
    +  = fst (approximateWCX include_non_quantifiable cts)
    
    1756 1753
     
    
    1757
    -approximateWCX :: WantedConstraints -> ApproxWC
    
    1754
    +approximateWCX :: Bool -> WantedConstraints -> ApproxWC
    
    1758 1755
     -- The "X" means "extended";
    
    1759 1756
     --    we return both quantifiable and non-quantifiable constraints
    
    1760 1757
     -- See Note [ApproximateWC]
    
    1761 1758
     -- See Note [floatKindEqualities vs approximateWC]
    
    1762
    -approximateWCX wc
    
    1763
    -  = float_wc False emptyVarSet wc (emptyBag, emptyBag)
    
    1759
    +approximateWCX include_non_quantifiable wc
    
    1760
    +  = float_wc False emptyVarSet wc (emptyBag, emptyVarSet)
    
    1764 1761
       where
    
    1765 1762
         float_wc :: Bool           -- True <=> there are enclosing equalities
    
    1766 1763
                  -> TcTyCoVarSet   -- Enclosing skolem binders
    
    ... ... @@ -1786,17 +1783,23 @@ approximateWCX wc
    1786 1783
                -- There can be (insoluble) Given constraints in wc_simple,
    
    1787 1784
                -- there so that we get error reports for unreachable code
    
    1788 1785
                -- See `given_insols` in GHC.Tc.Solver.Solve.solveImplication
    
    1789
    -       | insolubleCt ct                              = acc
    
    1790
    -       | tyCoVarsOfCt ct `intersectsVarSet` skol_tvs = acc
    
    1791
    -       | otherwise
    
    1792
    -       = case classifyPredType (ctPred ct) of
    
    1786
    +       | insolubleCt ct                       = acc
    
    1787
    +       | pred_tvs `intersectsVarSet` skol_tvs = acc
    
    1788
    +       | include_non_quantifiable             = add_to_quant
    
    1789
    +       | is_quantifiable encl_eqs (ctPred ct) = add_to_quant
    
    1790
    +       | otherwise                            = add_to_no_quant
    
    1791
    +       where
    
    1792
    +         pred     = ctPred ct
    
    1793
    +         pred_tvs = tyCoVarsOfType pred
    
    1794
    +         add_to_quant    = (ct `consBag` quant, no_quant)
    
    1795
    +         add_to_no_quant = (quant, no_quant `unionVarSet` pred_tvs)
    
    1796
    +
    
    1797
    +    is_quantifiable encl_eqs pred
    
    1798
    +       = case classifyPredType pred of
    
    1793 1799
                -- See the classification in Note [ApproximateWC]
    
    1794 1800
                EqPred eq_rel ty1 ty2
    
    1795
    -             | not encl_eqs      -- See Wrinkle (W1)
    
    1796
    -             , quantify_equality eq_rel ty1 ty2
    
    1797
    -             -> add_to_quant
    
    1798
    -             | otherwise
    
    1799
    -             -> add_to_no_quant
    
    1801
    +             | encl_eqs  -> False  -- encl_eqs: See Wrinkle (W1)
    
    1802
    +             | otherwise -> quantify_equality eq_rel ty1 ty2
    
    1800 1803
     
    
    1801 1804
                ClassPred cls tys
    
    1802 1805
                  | Just {} <- isCallStackPred cls tys
    
    ... ... @@ -1804,17 +1807,14 @@ approximateWCX wc
    1804 1807
                    -- the constraints bubble up to be solved from the outer
    
    1805 1808
                    -- context, or be defaulted when we reach the top-level.
    
    1806 1809
                    -- See Note [Overview of implicit CallStacks] in GHC.Tc.Types.Evidence
    
    1807
    -             -> add_to_no_quant
    
    1810
    +             -> False
    
    1808 1811
     
    
    1809 1812
                  | otherwise
    
    1810
    -             -> add_to_quant  -- See Wrinkle (W2)
    
    1813
    +             -> True  -- See Wrinkle (W2)
    
    1811 1814
     
    
    1812
    -           IrredPred {}  -> add_to_quant  -- See Wrinkle (W2)
    
    1815
    +           IrredPred {}  -> True  -- See Wrinkle (W2)
    
    1813 1816
     
    
    1814
    -           ForAllPred {} -> add_to_no_quant  -- Never quantify these
    
    1815
    -       where
    
    1816
    -         add_to_quant    = (ct `consBag` quant, no_quant)
    
    1817
    -         add_to_no_quant = (quant, ct `consBag` no_quant)
    
    1817
    +           ForAllPred {} -> False  -- Never quantify these
    
    1818 1818
     
    
    1819 1819
         -- See Note [Quantifying over equality constraints]
    
    1820 1820
         quantify_equality NomEq  ty1 ty2 = quant_fun ty1 || quant_fun ty2
    
    ... ... @@ -1852,7 +1852,7 @@ We proceed by classifying the constraint:
    1852 1852
     
    
    1853 1853
     Wrinkle (W1)
    
    1854 1854
       When inferring most-general types (in simplifyInfer), we
    
    1855
    -  do *not* float an equality constraint if the implication binds
    
    1855
    +  do *not* quantify over equality constraint if the implication binds
    
    1856 1856
       equality constraints, because that defeats the OutsideIn story.
    
    1857 1857
       Consider data T a where { TInt :: T Int; MkT :: T a }
    
    1858 1858
              f TInt = 3::Int
    

  • testsuite/tests/typecheck/should_fail/T26004.hs
    1
    +{-# LANGUAGE GADTs #-}
    
    2
    +{-# LANGUAGE NoMonoLocalBinds #-}
    
    3
    +
    
    4
    +module T26004 where
    
    5
    +
    
    6
    +data T a where
    
    7
    +  T1 :: T Bool
    
    8
    +  T2 :: T a
    
    9
    +
    
    10
    +-- This funcion should be rejected:
    
    11
    +-- we should not infer a non-principal type for `f`
    
    12
    +f w e = case e of
    
    13
    +  T1 -> let y = not w in False
    
    14
    +  T2 -> True

  • testsuite/tests/typecheck/should_fail/T26004.stderr
    1
    +
    
    2
    +T26004.hs:13:21: error: [GHC-25897]
    
    3
    +    • Could not deduce ‘p ~ Bool’
    
    4
    +      from the context: a ~ Bool
    
    5
    +        bound by a pattern with constructor: T1 :: T Bool,
    
    6
    +                 in a case alternative
    
    7
    +        at T26004.hs:13:3-4
    
    8
    +      ‘p’ is a rigid type variable bound by
    
    9
    +        the inferred type of f :: p -> T a -> Bool
    
    10
    +        at T26004.hs:(12,1)-(14,12)
    
    11
    +    • In the first argument of ‘not’, namely ‘w’
    
    12
    +      In the expression: not w
    
    13
    +      In an equation for ‘y’: y = not w
    
    14
    +    • Relevant bindings include
    
    15
    +        w :: p (bound at T26004.hs:12:3)
    
    16
    +        f :: p -> T a -> Bool (bound at T26004.hs:12:1)
    
    17
    +    Suggested fix: Consider giving ‘f’ a type signature

  • testsuite/tests/typecheck/should_fail/T7453.stderr
    1
    -
    
    2
    -T7453.hs:9:15: error: [GHC-25897]
    
    3
    -    • Couldn't match type ‘t’ with ‘p’
    
    4
    -      Expected: Id t
    
    5
    -        Actual: Id p
    
    1
    +T7453.hs:10:30: error: [GHC-25897]
    
    2
    +    • Couldn't match expected type ‘t’ with actual type ‘p’
    
    6 3
           ‘t’ is a rigid type variable bound by
    
    7 4
             the type signature for:
    
    8 5
               z :: forall t. Id t
    
    ... ... @@ -10,29 +7,17 @@ T7453.hs:9:15: error: [GHC-25897]
    10 7
           ‘p’ is a rigid type variable bound by
    
    11 8
             the inferred type of cast1 :: p -> a
    
    12 9
             at T7453.hs:(7,1)-(10,30)
    
    13
    -    • In the expression: aux
    
    14
    -      In an equation for ‘z’:
    
    15
    -          z = aux
    
    16
    -            where
    
    17
    -                aux = Id v
    
    18
    -      In an equation for ‘cast1’:
    
    19
    -          cast1 v
    
    20
    -            = runId z
    
    21
    -            where
    
    22
    -                z :: Id t
    
    23
    -                z = aux
    
    24
    -                  where
    
    25
    -                      aux = Id v
    
    10
    +    • In the first argument of ‘Id’, namely ‘v’
    
    11
    +      In the expression: Id v
    
    12
    +      In an equation for ‘aux’: aux = Id v
    
    26 13
         • Relevant bindings include
    
    27
    -        aux :: Id p (bound at T7453.hs:10:21)
    
    14
    +        aux :: Id t (bound at T7453.hs:10:21)
    
    28 15
             z :: Id t (bound at T7453.hs:9:11)
    
    29 16
             v :: p (bound at T7453.hs:7:7)
    
    30 17
             cast1 :: p -> a (bound at T7453.hs:7:1)
    
    31 18
     
    
    32
    -T7453.hs:15:15: error: [GHC-25897]
    
    33
    -    • Couldn't match type ‘t1’ with ‘p’
    
    34
    -      Expected: () -> t1
    
    35
    -        Actual: () -> p
    
    19
    +T7453.hs:16:33: error: [GHC-25897]
    
    20
    +    • Couldn't match expected type ‘t1’ with actual type ‘p’
    
    36 21
           ‘t1’ is a rigid type variable bound by
    
    37 22
             the type signature for:
    
    38 23
               z :: forall t1. () -> t1
    
    ... ... @@ -40,21 +25,11 @@ T7453.hs:15:15: error: [GHC-25897]
    40 25
           ‘p’ is a rigid type variable bound by
    
    41 26
             the inferred type of cast2 :: p -> t
    
    42 27
             at T7453.hs:(13,1)-(16,33)
    
    43
    -    • In the expression: aux
    
    44
    -      In an equation for ‘z’:
    
    45
    -          z = aux
    
    46
    -            where
    
    47
    -                aux = const v
    
    48
    -      In an equation for ‘cast2’:
    
    49
    -          cast2 v
    
    50
    -            = z ()
    
    51
    -            where
    
    52
    -                z :: () -> t
    
    53
    -                z = aux
    
    54
    -                  where
    
    55
    -                      aux = const v
    
    28
    +    • In the first argument of ‘const’, namely ‘v’
    
    29
    +      In the expression: const v
    
    30
    +      In an equation for ‘aux’: aux = const v
    
    56 31
         • Relevant bindings include
    
    57
    -        aux :: forall {b}. b -> p (bound at T7453.hs:16:21)
    
    32
    +        aux :: b -> t1 (bound at T7453.hs:16:21)
    
    58 33
             z :: () -> t1 (bound at T7453.hs:15:11)
    
    59 34
             v :: p (bound at T7453.hs:13:7)
    
    60 35
             cast2 :: p -> t (bound at T7453.hs:13:1)
    
    ... ... @@ -86,3 +61,4 @@ T7453.hs:21:15: error: [GHC-25897]
    86 61
             z :: t1 (bound at T7453.hs:21:11)
    
    87 62
             v :: p (bound at T7453.hs:19:7)
    
    88 63
             cast3 :: p -> t (bound at T7453.hs:19:1)
    
    64
    +

  • testsuite/tests/typecheck/should_fail/all.T
    ... ... @@ -735,3 +735,4 @@ test('T24938', normal, compile_fail, [''])
    735 735
     test('T25325', normal, compile_fail, [''])
    
    736 736
     test('T25004', normal, compile_fail, [''])
    
    737 737
     test('T25004k', normal, compile_fail, [''])
    
    738
    +test('T26004', normal, compile_fail, [''])