Simon Peyton Jones pushed to branch wip/T26015 at Glasgow Haskell Compiler / GHC
Commits:
-
e46c6b18
by Rodrigo Mesquita at 2025-05-06T09:01:57-04:00
-
0ce0d263
by Rodrigo Mesquita at 2025-05-06T09:01:57-04:00
-
165f98d8
by Simon Peyton Jones at 2025-05-06T09:02:39-04:00
-
6a7d917b
by Simon Peyton Jones at 2025-05-08T09:28:29+01:00
14 changed files:
- compiler/GHC/HsToCore/Pmc/Solver/Types.hs
- compiler/GHC/Runtime/Eval.hs
- compiler/GHC/Tc/Solver.hs
- compiler/GHC/Tc/Solver/InertSet.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/Types/Constraint.hs
- compiler/GHC/Tc/Utils/Monad.hs
- compiler/GHC/Types/Name/Occurrence.hs
- + testsuite/tests/typecheck/should_fail/T26004.hs
- + testsuite/tests/typecheck/should_fail/T26004.stderr
- + testsuite/tests/typecheck/should_fail/T26015.hs
- + testsuite/tests/typecheck/should_fail/T26015.stderr
- testsuite/tests/typecheck/should_fail/T7453.stderr
- testsuite/tests/typecheck/should_fail/all.T
Changes:
| ... | ... | @@ -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
|
| ... | ... | @@ -23,7 +23,7 @@ module GHC.Runtime.Eval ( |
| 23 | 23 | setupBreakpoint,
|
| 24 | 24 | back, forward,
|
| 25 | 25 | setContext, getContext,
|
| 26 | - mkTopLevEnv,
|
|
| 26 | + mkTopLevEnv, mkTopLevImportedEnv,
|
|
| 27 | 27 | getNamesInScope,
|
| 28 | 28 | getRdrNamesInScope,
|
| 29 | 29 | moduleIsInterpreted,
|
| ... | ... | @@ -836,29 +836,36 @@ mkTopLevEnv hsc_env modl |
| 836 | 836 | Nothing -> pure $ Left "not a home module"
|
| 837 | 837 | Just details ->
|
| 838 | 838 | case mi_top_env (hm_iface details) of
|
| 839 | - (IfaceTopEnv exports imports) -> do
|
|
| 840 | - imports_env <-
|
|
| 841 | - runInteractiveHsc hsc_env
|
|
| 842 | - $ ioMsgMaybe $ hoistTcRnMessage $ runTcInteractive hsc_env
|
|
| 843 | - $ fmap (foldr plusGlobalRdrEnv emptyGlobalRdrEnv)
|
|
| 844 | - $ forM imports $ \iface_import -> do
|
|
| 845 | - let ImpUserSpec spec details = tcIfaceImport iface_import
|
|
| 846 | - iface <- loadInterfaceForModule (text "imported by GHCi") (is_mod spec)
|
|
| 847 | - pure $ case details of
|
|
| 848 | - ImpUserAll -> importsFromIface hsc_env iface spec Nothing
|
|
| 849 | - ImpUserEverythingBut ns -> importsFromIface hsc_env iface spec (Just ns)
|
|
| 850 | - ImpUserExplicit x _parents_of_implicits ->
|
|
| 851 | - -- TODO: Not quite right, is_explicit should refer to whether the user wrote A(..) or A(x,y).
|
|
| 852 | - -- It is only used for error messages. It seems dubious even to add an import context to these GREs as
|
|
| 853 | - -- they are not "imported" into the top-level scope of the REPL. I changed this for now so that
|
|
| 854 | - -- the test case produce the same output as before.
|
|
| 855 | - let spec' = ImpSpec { is_decl = spec, is_item = ImpSome { is_explicit = True, is_iloc = noSrcSpan } }
|
|
| 856 | - in mkGlobalRdrEnv $ gresFromAvails hsc_env (Just spec') x
|
|
| 839 | + (IfaceTopEnv exports _imports) -> do
|
|
| 840 | + imports_env <- mkTopLevImportedEnv hsc_env details
|
|
| 857 | 841 | let exports_env = mkGlobalRdrEnv $ gresFromAvails hsc_env Nothing (getDetOrdAvails exports)
|
| 858 | 842 | pure $ Right $ plusGlobalRdrEnv imports_env exports_env
|
| 859 | 843 | where
|
| 860 | 844 | hpt = hsc_HPT hsc_env
|
| 861 | 845 | |
| 846 | +-- | Make the top-level environment with all bindings imported by this module.
|
|
| 847 | +-- Exported bindings from this module are not included in the result.
|
|
| 848 | +mkTopLevImportedEnv :: HscEnv -> HomeModInfo -> IO GlobalRdrEnv
|
|
| 849 | +mkTopLevImportedEnv hsc_env details = do
|
|
| 850 | + runInteractiveHsc hsc_env
|
|
| 851 | + $ ioMsgMaybe $ hoistTcRnMessage $ runTcInteractive hsc_env
|
|
| 852 | + $ fmap (foldr plusGlobalRdrEnv emptyGlobalRdrEnv)
|
|
| 853 | + $ forM imports $ \iface_import -> do
|
|
| 854 | + let ImpUserSpec spec details = tcIfaceImport iface_import
|
|
| 855 | + iface <- loadInterfaceForModule (text "imported by GHCi") (is_mod spec)
|
|
| 856 | + pure $ case details of
|
|
| 857 | + ImpUserAll -> importsFromIface hsc_env iface spec Nothing
|
|
| 858 | + ImpUserEverythingBut ns -> importsFromIface hsc_env iface spec (Just ns)
|
|
| 859 | + ImpUserExplicit x _parents_of_implicits ->
|
|
| 860 | + -- TODO: Not quite right, is_explicit should refer to whether the user wrote A(..) or A(x,y).
|
|
| 861 | + -- It is only used for error messages. It seems dubious even to add an import context to these GREs as
|
|
| 862 | + -- they are not "imported" into the top-level scope of the REPL. I changed this for now so that
|
|
| 863 | + -- the test case produce the same output as before.
|
|
| 864 | + let spec' = ImpSpec { is_decl = spec, is_item = ImpSome { is_explicit = True, is_iloc = noSrcSpan } }
|
|
| 865 | + in mkGlobalRdrEnv $ gresFromAvails hsc_env (Just spec') x
|
|
| 866 | + where
|
|
| 867 | + IfaceTopEnv _ imports = mi_top_env (hm_iface details)
|
|
| 868 | + |
|
| 862 | 869 | -- | Get the interactive evaluation context, consisting of a pair of the
|
| 863 | 870 | -- set of modules from which we take the full top-level scope, and the set
|
| 864 | 871 | -- of modules from which we take just the exports respectively.
|
| ... | ... | @@ -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 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... | ... | @@ -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
|
| ... | ... | @@ -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
|
| ... | ... | @@ -1181,9 +1181,12 @@ dropMisleading (WC { wc_simple = simples, wc_impl = implics, wc_errors = errors |
| 1181 | 1181 | , wc_impl = mapBag drop_implic implics
|
| 1182 | 1182 | , wc_errors = filterBag keep_delayed_error errors }
|
| 1183 | 1183 | |
| 1184 | - keep_ct ct = case classifyPredType (ctPred ct) of
|
|
| 1185 | - ClassPred {} -> False
|
|
| 1186 | - _ -> True
|
|
| 1184 | + keep_ct ct
|
|
| 1185 | + = case classifyPredType (ctPred ct) of
|
|
| 1186 | + ClassPred cls _ -> isEqualityClass cls
|
|
| 1187 | + -- isEqualityClass: see (CERR2) in Note [Constraints and errors]
|
|
| 1188 | + -- in GHC.Tc.Utils.Monad
|
|
| 1189 | + _ -> True
|
|
| 1187 | 1190 | |
| 1188 | 1191 | keep_delayed_error (DE_Hole hole) = isOutOfScopeHole hole
|
| 1189 | 1192 | keep_delayed_error (DE_NotConcrete {}) = True
|
| ... | ... | @@ -1743,24 +1746,21 @@ will be able to report a more informative error: |
| 1743 | 1746 | ************************************************************************
|
| 1744 | 1747 | -}
|
| 1745 | 1748 | |
| 1746 | -type ApproxWC = ( Bag Ct -- Free quantifiable constraints
|
|
| 1747 | - , Bag Ct ) -- Free non-quantifiable constraints
|
|
| 1748 | - -- due to shape, or enclosing equality
|
|
| 1749 | +type ApproxWC = ( Bag Ct -- Free quantifiable constraints
|
|
| 1750 | + , TcTyCoVarSet ) -- Free vars of non-quantifiable constraints
|
|
| 1751 | + -- due to shape, or enclosing equality
|
|
| 1749 | 1752 | |
| 1750 | 1753 | approximateWC :: Bool -> WantedConstraints -> Bag Ct
|
| 1751 | 1754 | approximateWC include_non_quantifiable cts
|
| 1752 | - | include_non_quantifiable = quant `unionBags` no_quant
|
|
| 1753 | - | otherwise = quant
|
|
| 1754 | - where
|
|
| 1755 | - (quant, no_quant) = approximateWCX cts
|
|
| 1755 | + = fst (approximateWCX include_non_quantifiable cts)
|
|
| 1756 | 1756 | |
| 1757 | -approximateWCX :: WantedConstraints -> ApproxWC
|
|
| 1757 | +approximateWCX :: Bool -> WantedConstraints -> ApproxWC
|
|
| 1758 | 1758 | -- The "X" means "extended";
|
| 1759 | 1759 | -- we return both quantifiable and non-quantifiable constraints
|
| 1760 | 1760 | -- See Note [ApproximateWC]
|
| 1761 | 1761 | -- See Note [floatKindEqualities vs approximateWC]
|
| 1762 | -approximateWCX wc
|
|
| 1763 | - = float_wc False emptyVarSet wc (emptyBag, emptyBag)
|
|
| 1762 | +approximateWCX include_non_quantifiable wc
|
|
| 1763 | + = float_wc False emptyVarSet wc (emptyBag, emptyVarSet)
|
|
| 1764 | 1764 | where
|
| 1765 | 1765 | float_wc :: Bool -- True <=> there are enclosing equalities
|
| 1766 | 1766 | -> TcTyCoVarSet -- Enclosing skolem binders
|
| ... | ... | @@ -1786,17 +1786,23 @@ approximateWCX wc |
| 1786 | 1786 | -- There can be (insoluble) Given constraints in wc_simple,
|
| 1787 | 1787 | -- there so that we get error reports for unreachable code
|
| 1788 | 1788 | -- 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
|
|
| 1789 | + | insolubleCt ct = acc
|
|
| 1790 | + | pred_tvs `intersectsVarSet` skol_tvs = acc
|
|
| 1791 | + | include_non_quantifiable = add_to_quant
|
|
| 1792 | + | is_quantifiable encl_eqs (ctPred ct) = add_to_quant
|
|
| 1793 | + | otherwise = add_to_no_quant
|
|
| 1794 | + where
|
|
| 1795 | + pred = ctPred ct
|
|
| 1796 | + pred_tvs = tyCoVarsOfType pred
|
|
| 1797 | + add_to_quant = (ct `consBag` quant, no_quant)
|
|
| 1798 | + add_to_no_quant = (quant, no_quant `unionVarSet` pred_tvs)
|
|
| 1799 | + |
|
| 1800 | + is_quantifiable encl_eqs pred
|
|
| 1801 | + = case classifyPredType pred of
|
|
| 1793 | 1802 | -- See the classification in Note [ApproximateWC]
|
| 1794 | 1803 | 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
|
|
| 1804 | + | encl_eqs -> False -- encl_eqs: See Wrinkle (W1)
|
|
| 1805 | + | otherwise -> quantify_equality eq_rel ty1 ty2
|
|
| 1800 | 1806 | |
| 1801 | 1807 | ClassPred cls tys
|
| 1802 | 1808 | | Just {} <- isCallStackPred cls tys
|
| ... | ... | @@ -1804,17 +1810,14 @@ approximateWCX wc |
| 1804 | 1810 | -- the constraints bubble up to be solved from the outer
|
| 1805 | 1811 | -- context, or be defaulted when we reach the top-level.
|
| 1806 | 1812 | -- See Note [Overview of implicit CallStacks] in GHC.Tc.Types.Evidence
|
| 1807 | - -> add_to_no_quant
|
|
| 1813 | + -> False
|
|
| 1808 | 1814 | |
| 1809 | 1815 | | otherwise
|
| 1810 | - -> add_to_quant -- See Wrinkle (W2)
|
|
| 1816 | + -> True -- See Wrinkle (W2)
|
|
| 1811 | 1817 | |
| 1812 | - IrredPred {} -> add_to_quant -- See Wrinkle (W2)
|
|
| 1818 | + IrredPred {} -> True -- See Wrinkle (W2)
|
|
| 1813 | 1819 | |
| 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)
|
|
| 1820 | + ForAllPred {} -> False -- Never quantify these
|
|
| 1818 | 1821 | |
| 1819 | 1822 | -- See Note [Quantifying over equality constraints]
|
| 1820 | 1823 | quantify_equality NomEq ty1 ty2 = quant_fun ty1 || quant_fun ty2
|
| ... | ... | @@ -1852,7 +1855,7 @@ We proceed by classifying the constraint: |
| 1852 | 1855 | |
| 1853 | 1856 | Wrinkle (W1)
|
| 1854 | 1857 | When inferring most-general types (in simplifyInfer), we
|
| 1855 | - do *not* float an equality constraint if the implication binds
|
|
| 1858 | + do *not* quantify over equality constraint if the implication binds
|
|
| 1856 | 1859 | equality constraints, because that defeats the OutsideIn story.
|
| 1857 | 1860 | Consider data T a where { TInt :: T Int; MkT :: T a }
|
| 1858 | 1861 | f TInt = 3::Int
|
| ... | ... | @@ -1385,11 +1385,13 @@ tryCaptureConstraints thing_inside |
| 1385 | 1385 | tcTryM thing_inside
|
| 1386 | 1386 | |
| 1387 | 1387 | -- See Note [Constraints and errors]
|
| 1388 | - ; let lie_to_keep = case mb_res of
|
|
| 1389 | - Nothing -> dropMisleading lie
|
|
| 1390 | - Just {} -> lie
|
|
| 1391 | - |
|
| 1392 | - ; return (mb_res, lie_to_keep) }
|
|
| 1388 | + ; case mb_res of
|
|
| 1389 | + Just {} -> return (mb_res, lie)
|
|
| 1390 | + Nothing -> do { let pruned_lie = dropMisleading lie
|
|
| 1391 | + ; traceTc "tryCaptureConstraints" $
|
|
| 1392 | + vcat [ text "lie:" <+> ppr lie
|
|
| 1393 | + , text "dropMisleading lie:" <+> ppr pruned_lie ]
|
|
| 1394 | + ; return (Nothing, pruned_lie) } }
|
|
| 1393 | 1395 | |
| 1394 | 1396 | captureConstraints :: TcM a -> TcM (a, WantedConstraints)
|
| 1395 | 1397 | -- (captureConstraints m) runs m, and returns the type constraints it generates
|
| ... | ... | @@ -2066,28 +2068,51 @@ It's distressingly delicate though: |
| 2066 | 2068 | emitted some constraints with skolem-escape problems.
|
| 2067 | 2069 | |
| 2068 | 2070 | * If we discard too /few/ constraints, we may get the misleading
|
| 2069 | - class constraints mentioned above. But we may /also/ end up taking
|
|
| 2070 | - constraints built at some inner level, and emitting them at some
|
|
| 2071 | - outer level, and then breaking the TcLevel invariants
|
|
| 2072 | - See Note [TcLevel invariants] in GHC.Tc.Utils.TcType
|
|
| 2073 | - |
|
| 2074 | -So dropMisleading has a horridly ad-hoc structure. It keeps only
|
|
| 2075 | -/insoluble/ flat constraints (which are unlikely to very visibly trip
|
|
| 2076 | -up on the TcLevel invariant, but all /implication/ constraints (except
|
|
| 2077 | -the class constraints inside them). The implication constraints are
|
|
| 2078 | -OK because they set the ambient level before attempting to solve any
|
|
| 2079 | -inner constraints. Ugh! I hate this. But it seems to work.
|
|
| 2080 | - |
|
| 2081 | -However note that freshly-generated constraints like (Int ~ Bool), or
|
|
| 2082 | -((a -> b) ~ Int) are all CNonCanonical, and hence won't be flagged as
|
|
| 2083 | -insoluble. The constraint solver does that. So they'll be discarded.
|
|
| 2084 | -That's probably ok; but see th/5358 as a not-so-good example:
|
|
| 2085 | - t1 :: Int
|
|
| 2086 | - t1 x = x -- Manifestly wrong
|
|
| 2087 | - |
|
| 2088 | - foo = $(...raises exception...)
|
|
| 2089 | -We report the exception, but not the bug in t1. Oh well. Possible
|
|
| 2090 | -solution: make GHC.Tc.Utils.Unify.uType spot manifestly-insoluble constraints.
|
|
| 2071 | + class constraints mentioned above.
|
|
| 2072 | + |
|
| 2073 | + We may /also/ end up taking constraints built at some inner level, and
|
|
| 2074 | + emitting them (via the exception catching in `tryCaptureConstraints` at some
|
|
| 2075 | + outer level, and then breaking the TcLevel invariants See Note [TcLevel
|
|
| 2076 | + invariants] in GHC.Tc.Utils.TcType
|
|
| 2077 | + |
|
| 2078 | +So `dropMisleading` has a horridly ad-hoc structure:
|
|
| 2079 | + |
|
| 2080 | +* It keeps only /insoluble/ flat constraints (which are unlikely to very visibly
|
|
| 2081 | + trip up on the TcLevel invariant
|
|
| 2082 | + |
|
| 2083 | +* But it keeps all /implication/ constraints (except the class constraints
|
|
| 2084 | + inside them). The implication constraints are OK because they set the ambient
|
|
| 2085 | + level before attempting to solve any inner constraints.
|
|
| 2086 | + |
|
| 2087 | +Ugh! I hate this. But it seems to work.
|
|
| 2088 | + |
|
| 2089 | +Other wrinkles
|
|
| 2090 | + |
|
| 2091 | +(CERR1) Note that freshly-generated constraints like (Int ~ Bool), or
|
|
| 2092 | + ((a -> b) ~ Int) are all CNonCanonical, and hence won't be flagged as
|
|
| 2093 | + insoluble. The constraint solver does that. So they'll be discarded.
|
|
| 2094 | + That's probably ok; but see th/5358 as a not-so-good example:
|
|
| 2095 | + t1 :: Int
|
|
| 2096 | + t1 x = x -- Manifestly wrong
|
|
| 2097 | + |
|
| 2098 | + foo = $(...raises exception...)
|
|
| 2099 | + We report the exception, but not the bug in t1. Oh well. Possible
|
|
| 2100 | + solution: make GHC.Tc.Utils.Unify.uType spot manifestly-insoluble constraints.
|
|
| 2101 | + |
|
| 2102 | +(CERR2) In #26015 I found that from the constraints
|
|
| 2103 | + [W] alpha ~ Int -- A class constraint
|
|
| 2104 | + [W] F alpha ~# Bool -- An equality constraint
|
|
| 2105 | + we were dropping the first (becuase it's a class constraint) but not the
|
|
| 2106 | + second, and then getting a misleading error message from the second. As
|
|
| 2107 | + #25607 shows, we can get not just one but a zillion bogus messages, which
|
|
| 2108 | + conceal the one genuine error. Boo.
|
|
| 2109 | + |
|
| 2110 | + For now I have added an even more ad-hoc "drop class constraints except
|
|
| 2111 | + equality classes (~) and (~~)"; see `dropMisleading`. That just kicks the can
|
|
| 2112 | + down the road; but this problem seems somewhat rare anyway. The code in
|
|
| 2113 | + `dropMisleading` hasn't changed for years.
|
|
| 2114 | + |
|
| 2115 | +It would be great to have a more systematic solution to this entire mess.
|
|
| 2091 | 2116 | |
| 2092 | 2117 | |
| 2093 | 2118 | ************************************************************************
|
| ... | ... | @@ -92,6 +92,7 @@ module GHC.Types.Name.Occurrence ( |
| 92 | 92 | plusOccEnv, plusOccEnv_C,
|
| 93 | 93 | extendOccEnv_Acc, filterOccEnv, delListFromOccEnv, delFromOccEnv,
|
| 94 | 94 | alterOccEnv, minusOccEnv, minusOccEnv_C, minusOccEnv_C_Ns,
|
| 95 | + sizeOccEnv,
|
|
| 95 | 96 | pprOccEnv, forceOccEnv,
|
| 96 | 97 | intersectOccEnv_C,
|
| 97 | 98 | |
| ... | ... | @@ -803,6 +804,10 @@ minusOccEnv_C_Ns f (MkOccEnv as) (MkOccEnv bs) = |
| 803 | 804 | then Nothing
|
| 804 | 805 | else Just m
|
| 805 | 806 | |
| 807 | +sizeOccEnv :: OccEnv a -> Int
|
|
| 808 | +sizeOccEnv (MkOccEnv as) =
|
|
| 809 | + nonDetStrictFoldUFM (\ m !acc -> acc + sizeUFM m) 0 as
|
|
| 810 | + |
|
| 806 | 811 | instance Outputable a => Outputable (OccEnv a) where
|
| 807 | 812 | ppr x = pprOccEnv ppr x
|
| 808 | 813 |
| 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 |
| 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 |
| 1 | +{-# LANGUAGE MonoLocalBinds, GADTs, TypeFamilies #-}
|
|
| 2 | + |
|
| 3 | +module Foo where
|
|
| 4 | + |
|
| 5 | +type family F a
|
|
| 6 | +type instance F Int = Bool
|
|
| 7 | + |
|
| 8 | +data T where
|
|
| 9 | + T1 :: a -> T
|
|
| 10 | + T2 :: Int -> T
|
|
| 11 | + |
|
| 12 | +woo :: (a ~ Int) => Int -> F a
|
|
| 13 | +woo = error "urk"
|
|
| 14 | + |
|
| 15 | +f x = case x of
|
|
| 16 | + T1 y -> not (woo 3)
|
|
| 17 | + T2 -> True |
| 1 | +T26015.hs:17:10: error: [GHC-27346]
|
|
| 2 | + • The data constructor ‘T2’ should have 1 argument, but has been given none
|
|
| 3 | + • In the pattern: T2
|
|
| 4 | + In a case alternative: T2 -> True
|
|
| 5 | + In the expression:
|
|
| 6 | + case x of
|
|
| 7 | + T1 y -> not (woo 3)
|
|
| 8 | + T2 -> True
|
|
| 9 | + |
| 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 | + |
| ... | ... | @@ -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, ['']) |