Simon Peyton Jones pushed to branch wip/T26004 at Glasgow Haskell Compiler / GHC
Commits:
-
731d1d93
by Simon Peyton Jones at 2025-05-01T11:35:23+01:00
-
6f213c9d
by Simon Peyton Jones at 2025-05-01T11:39:37+01:00
-
db5c721f
by Simon Peyton Jones at 2025-05-01T11:48:46+01:00
6 changed files:
- compiler/GHC/Tc/Solver.hs
- compiler/GHC/Tc/Types/Constraint.hs
- + testsuite/tests/typecheck/should_fail/T26004.hs
- + testsuite/tests/typecheck/should_fail/T26004.stderr
- testsuite/tests/typecheck/should_fail/T7453.stderr
- testsuite/tests/typecheck/should_fail/all.T
Changes:
... | ... | @@ -1430,15 +1430,15 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted |
1430 | 1430 | |
1431 | 1431 | -- Step 1 of Note [decideAndPromoteTyVars]
|
1432 | 1432 | -- Get candidate constraints, decide which we can potentially quantify
|
1433 | - (can_quant_cts, no_quant_cts) = approximateWCX wanted
|
|
1433 | + -- The `no_quant_tvs` are free in constraints we can't quantify.
|
|
1434 | + (can_quant_cts, no_quant_tvs) = approximateWCX False wanted
|
|
1434 | 1435 | can_quant = ctsPreds can_quant_cts
|
1435 | - no_quant = ctsPreds no_quant_cts
|
|
1436 | 1436 | can_quant_tvs = tyCoVarsOfTypes can_quant
|
1437 | - no_quant_tvs = tyCoVarsOfTypes no_quant
|
|
1438 | 1437 | |
1439 | 1438 | -- Step 2 of Note [decideAndPromoteTyVars]
|
1440 | 1439 | -- Apply the monomorphism restriction
|
1441 | 1440 | (post_mr_quant, mr_no_quant) = applyMR dflags infer_mode can_quant
|
1441 | + mr_no_quant_tvs = tyCoVarsOfTypes mr_no_quant
|
|
1442 | 1442 | |
1443 | 1443 | -- The co_var_tvs are tvs mentioned in the types of covars or
|
1444 | 1444 | -- coercion holes. We can't quantify over these covars, so we
|
... | ... | @@ -1457,9 +1457,10 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted |
1457 | 1457 | |
1458 | 1458 | -- Step 3 of Note [decideAndPromoteTyVars], (a-c)
|
1459 | 1459 | -- Identify mono_tvs: the type variables that we must not quantify over
|
1460 | + -- At top level we are much less keen to create mono tyvars, to avoid
|
|
1461 | + -- spooky action at a distance.
|
|
1460 | 1462 | mono_tvs_without_mr
|
1461 | --- This does not work well (#26004)
|
|
1462 | --- | is_top_level = outer_tvs
|
|
1463 | + | is_top_level = outer_tvs -- See (DP2)
|
|
1463 | 1464 | | otherwise = outer_tvs -- (a)
|
1464 | 1465 | `unionVarSet` no_quant_tvs -- (b)
|
1465 | 1466 | `unionVarSet` co_var_tvs -- (c)
|
... | ... | @@ -1469,7 +1470,7 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted |
1469 | 1470 | = -- Even at top level, we don't quantify over type variables
|
1470 | 1471 | -- mentioned in constraints that the MR tells us not to quantify
|
1471 | 1472 | -- See Note [decideAndPromoteTyVars] (DP2)
|
1472 | - mono_tvs_without_mr `unionVarSet` tyCoVarsOfTypes mr_no_quant
|
|
1473 | + mono_tvs_without_mr `unionVarSet` mr_no_quant_tvs
|
|
1473 | 1474 | |
1474 | 1475 | --------------------------------------------------------------------
|
1475 | 1476 | -- Step 4 of Note [decideAndPromoteTyVars]
|
... | ... | @@ -1523,7 +1524,7 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted |
1523 | 1524 | , text "newly_mono_tvs =" <+> ppr newly_mono_tvs
|
1524 | 1525 | , text "can_quant =" <+> ppr can_quant
|
1525 | 1526 | , text "post_mr_quant =" <+> ppr post_mr_quant
|
1526 | - , text "no_quant =" <+> ppr no_quant
|
|
1527 | + , text "no_quant_tvs =" <+> ppr no_quant_tvs
|
|
1527 | 1528 | , text "mr_no_quant =" <+> ppr mr_no_quant
|
1528 | 1529 | , text "final_quant =" <+> ppr final_quant
|
1529 | 1530 | , text "co_vars =" <+> ppr co_vars ]
|
... | ... | @@ -1648,9 +1649,22 @@ Wrinkles |
1648 | 1649 | promote type variables. But for bindings affected by the MR we have no choice
|
1649 | 1650 | but to promote.
|
1650 | 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 | + |
|
1651 | 1663 | Note [The top-level Any principle]
|
1652 | 1664 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
1653 | -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
|
|
1654 | 1668 | |
1655 | 1669 | Most /top level/ bindings have a type signature, so none of this arises. But
|
1656 | 1670 | where a top-level binding lacks a signature, we don't want to infer a type like
|
... | ... | @@ -1659,11 +1673,18 @@ and then subsequently default alpha[0]:=Any. Exposing `Any` to the user is bad |
1659 | 1673 | bad bad. Better to report an error, which is what may well happen if we
|
1660 | 1674 | quantify over alpha instead.
|
1661 | 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 | + |
|
1662 | 1683 | For /nested/ bindings, a monomorphic type like `f :: alpha[0] -> Int` is fine,
|
1663 | 1684 | because we can see all the call sites of `f`, and they will probably fix
|
1664 | 1685 | `alpha`. In contrast, we can't see all of (or perhaps any of) the calls of
|
1665 | 1686 | top-level (exported) functions, reducing the worries about "spooky action at a
|
1666 | -distance".
|
|
1687 | +distance". This also moves in the direction of `MonoLocalBinds`, which we like.
|
|
1667 | 1688 | |
1668 | 1689 | Note [Do not quantify over constraints that determine a variable]
|
1669 | 1690 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
... | ... | @@ -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 | - | encl_eqs -> acc
|
|
1796 | - | quantify_equality eq_rel ty1 ty2 -> add_to_quant
|
|
1797 | - | otherwise -> add_to_no_quant
|
|
1798 | - -- encl_eqs: See Wrinkle (W1)
|
|
1799 | - -- ToDo: explain acc
|
|
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
|
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 | - |
|
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, ['']) |