Simon Peyton Jones pushed to branch wip/T26115 at Glasgow Haskell Compiler / GHC
Commits:
-
4b02492f
by Simon Peyton Jones at 2025-06-26T23:40:31+01:00
7 changed files:
- compiler/GHC/Tc/Solver/Dict.hs
- compiler/GHC/Tc/Solver/Equality.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/Solver/Solve.hs
- compiler/GHC/Tc/Types/Evidence.hs
- compiler/GHC/Tc/Utils/Monad.hs
- compiler/GHC/Tc/Utils/TcMType.hs
Changes:
... | ... | @@ -1086,14 +1086,25 @@ matchLocalInst :: TcPredType -> CtLoc -> TcS ClsInstResult |
1086 | 1086 | -- Look up the predicate in Given quantified constraints,
|
1087 | 1087 | -- which are effectively just local instance declarations.
|
1088 | 1088 | matchLocalInst body_pred loc
|
1089 | - = do { inerts@(IS { inert_cans = ics }) <- getInertSet
|
|
1089 | + = do { -- In TcSShortCut mode we do not look at Givens;
|
|
1090 | + -- c.f. tryInertDicts
|
|
1091 | + mode <- getTcSMode
|
|
1092 | + ; case mode of
|
|
1093 | + { TcSShortCut -> do { traceTcS "matchLocalInst:TcSShortCut" (ppr body_pred)
|
|
1094 | + ; return NoInstance }
|
|
1095 | + ; _other ->
|
|
1096 | + |
|
1097 | + do { -- Look in the inert set for a matching Given quantified constraint
|
|
1098 | + inerts@(IS { inert_cans = ics }) <- getInertSet
|
|
1090 | 1099 | ; case match_local_inst inerts (inert_insts ics) of
|
1091 | 1100 | { ([], []) -> do { traceTcS "No local instance for" (ppr body_pred)
|
1092 | 1101 | ; return NoInstance }
|
1093 | 1102 | ; (matches, unifs) ->
|
1094 | - do { matches <- mapM mk_instDFun matches
|
|
1095 | - ; unifs <- mapM mk_instDFun unifs
|
|
1103 | + |
|
1104 | + do { -- Find the best match
|
|
1096 | 1105 | -- See Note [Use only the best matching quantified constraint]
|
1106 | + matches <- mapM mk_instDFun matches
|
|
1107 | + ; unifs <- mapM mk_instDFun unifs
|
|
1097 | 1108 | ; case dominatingMatch matches of
|
1098 | 1109 | { Just (dfun_id, tys, theta)
|
1099 | 1110 | | all ((theta `impliedBySCs`) . thdOf3) unifs
|
... | ... | @@ -1115,7 +1126,7 @@ matchLocalInst body_pred loc |
1115 | 1126 | , text "matches:" <+> ppr matches
|
1116 | 1127 | , text "unifs:" <+> ppr unifs
|
1117 | 1128 | , text "best_match:" <+> ppr mb_best ]
|
1118 | - ; return NotSure }}}}}
|
|
1129 | + ; return NotSure }}}}}}}
|
|
1119 | 1130 | where
|
1120 | 1131 | body_pred_tv_set = tyCoVarsOfType body_pred
|
1121 | 1132 |
... | ... | @@ -545,7 +545,9 @@ can_eq_nc_forall ev eq_rel s1 s2 |
545 | 545 | , ic_wanted = emptyWC { wc_simple = wanteds } }
|
546 | 546 | |
547 | 547 | ; if solved
|
548 | - then do { setWantedEq orig_dest all_co
|
|
548 | + then do { zonked_all_co <- zonkCo all_co
|
|
549 | + -- ToDo: explain this zonk
|
|
550 | + ; setWantedEq orig_dest zonked_all_co
|
|
549 | 551 | ; stopWith ev "Polytype equality: solved" }
|
550 | 552 | else canEqSoftFailure IrredShapeReason ev s1 s2 } }
|
551 | 553 | |
... | ... | @@ -572,7 +574,8 @@ can_eq_nc_forall ev eq_rel s1 s2 |
572 | 574 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
573 | 575 | To solve an equality between foralls
|
574 | 576 | [W] (forall a. t1) ~ (forall b. t2)
|
575 | -the basic plan is simple: just create the implication constraint
|
|
577 | +the basic plan is simple: use `trySolveImplication` to solve the
|
|
578 | +implication constraint
|
|
576 | 579 | [W] forall a. { t1 ~ (t2[a/b]) }
|
577 | 580 | |
578 | 581 | The evidence we produce is a ForAllCo; see the typing rule for
|
... | ... | @@ -1435,7 +1435,7 @@ getTcEvBindsVar = TcS (return . tcs_ev_binds) |
1435 | 1435 | getTcLevel :: TcS TcLevel
|
1436 | 1436 | getTcLevel = wrapTcS TcM.getTcLevel
|
1437 | 1437 | |
1438 | -getTcEvTyCoVars :: EvBindsVar -> TcS TyCoVarSet
|
|
1438 | +getTcEvTyCoVars :: EvBindsVar -> TcS [TcCoercion]
|
|
1439 | 1439 | getTcEvTyCoVars ev_binds_var
|
1440 | 1440 | = wrapTcS $ TcM.getTcEvTyCoVars ev_binds_var
|
1441 | 1441 | |
... | ... | @@ -1989,19 +1989,15 @@ setEvBind ev_bind |
1989 | 1989 | ; wrapTcS $ TcM.addTcEvBind evb ev_bind }
|
1990 | 1990 | |
1991 | 1991 | -- | Mark variables as used filling a coercion hole
|
1992 | -useVars :: CoVarSet -> TcS ()
|
|
1993 | -useVars co_vars
|
|
1992 | +addUsedCoercion :: TcCoercion -> TcS ()
|
|
1993 | +addUsedCoercion co
|
|
1994 | 1994 | = do { ev_binds_var <- getTcEvBindsVar
|
1995 | - ; let ref = ebv_tcvs ev_binds_var
|
|
1996 | - ; wrapTcS $
|
|
1997 | - do { tcvs <- TcM.readTcRef ref
|
|
1998 | - ; let tcvs' = tcvs `unionVarSet` co_vars
|
|
1999 | - ; TcM.writeTcRef ref tcvs' } }
|
|
1995 | + ; wrapTcS (TcM.updTcRef (ebv_tcvs ev_binds_var) (co :)) }
|
|
2000 | 1996 | |
2001 | 1997 | -- | Equalities only
|
2002 | -setWantedEq :: HasDebugCallStack => TcEvDest -> Coercion -> TcS ()
|
|
1998 | +setWantedEq :: HasDebugCallStack => TcEvDest -> TcCoercion -> TcS ()
|
|
2003 | 1999 | setWantedEq (HoleDest hole) co
|
2004 | - = do { useVars (coVarsOfCo co)
|
|
2000 | + = do { addUsedCoercion co
|
|
2005 | 2001 | ; fillCoercionHole hole co }
|
2006 | 2002 | setWantedEq (EvVarDest ev) _ = pprPanic "setWantedEq: EvVarDest" (ppr ev)
|
2007 | 2003 | |
... | ... | @@ -2009,7 +2005,7 @@ setWantedEq (EvVarDest ev) _ = pprPanic "setWantedEq: EvVarDest" (ppr ev) |
2009 | 2005 | setWantedEvTerm :: TcEvDest -> CanonicalEvidence -> EvTerm -> TcS ()
|
2010 | 2006 | setWantedEvTerm (HoleDest hole) _canonical tm
|
2011 | 2007 | | Just co <- evTermCoercion_maybe tm
|
2012 | - = do { useVars (coVarsOfCo co)
|
|
2008 | + = do { addUsedCoercion co
|
|
2013 | 2009 | ; fillCoercionHole hole co }
|
2014 | 2010 | | otherwise
|
2015 | 2011 | = -- See Note [Yukky eq_sel for a HoleDest]
|
... | ... | @@ -35,6 +35,7 @@ import GHC.Tc.Solver.Monad as TcS |
35 | 35 | import GHC.Core.Predicate
|
36 | 36 | import GHC.Core.Reduction
|
37 | 37 | import GHC.Core.Coercion
|
38 | +import GHC.Core.TyCo.FVs( coVarsOfCos )
|
|
38 | 39 | import GHC.Core.Class( classHasSCs )
|
39 | 40 | |
40 | 41 | import GHC.Types.Id( idType )
|
... | ... | @@ -546,7 +547,7 @@ neededEvVars implic@(Implic { ic_info = info |
546 | 547 | , ic_need_implic = old_need_implic -- See (TRC1)
|
547 | 548 | })
|
548 | 549 | = do { ev_binds <- TcS.getTcEvBindsMap ev_binds_var
|
549 | - ; tcvs <- TcS.getTcEvTyCoVars ev_binds_var
|
|
550 | + ; used_cos <- TcS.getTcEvTyCoVars ev_binds_var
|
|
550 | 551 | |
551 | 552 | ; let -- Find the variables needed by `implics`
|
552 | 553 | new_need_implic@(ENS { ens_dms = dm_seeds, ens_fvs = other_seeds })
|
... | ... | @@ -556,7 +557,8 @@ neededEvVars implic@(Implic { ic_info = info |
556 | 557 | -- Get the variables needed by the solved bindings
|
557 | 558 | -- (It's OK to use a non-deterministic fold here
|
558 | 559 | -- because add_wanted is commutative.)
|
559 | - seeds_w = nonDetStrictFoldEvBindMap add_wanted tcvs ev_binds
|
|
560 | + used_covars = coVarsOfCos used_cos
|
|
561 | + seeds_w = nonDetStrictFoldEvBindMap add_wanted used_covars ev_binds
|
|
560 | 562 | |
561 | 563 | need_ignoring_dms = findNeededGivenEvVars ev_binds (other_seeds `unionVarSet` seeds_w)
|
562 | 564 | need_from_dms = findNeededGivenEvVars ev_binds dm_seeds
|
... | ... | @@ -577,7 +579,7 @@ neededEvVars implic@(Implic { ic_info = info |
577 | 579 | ; traceTcS "neededEvVars" $
|
578 | 580 | vcat [ text "old_need_implic:" <+> ppr old_need_implic
|
579 | 581 | , text "new_need_implic:" <+> ppr new_need_implic
|
580 | - , text "tcvs:" <+> ppr tcvs
|
|
582 | + , text "used_covars:" <+> ppr used_covars
|
|
581 | 583 | , text "need_ignoring_dms:" <+> ppr need_ignoring_dms
|
582 | 584 | , text "need_from_dms:" <+> ppr need_from_dms
|
583 | 585 | , text "need:" <+> ppr need
|
... | ... | @@ -360,11 +360,13 @@ data EvBindsVar |
360 | 360 | -- (dictionaries etc)
|
361 | 361 | -- Some Given, some Wanted
|
362 | 362 | |
363 | - ebv_tcvs :: IORef CoVarSet
|
|
364 | - -- The free Given coercion vars needed by Wanted coercions that
|
|
365 | - -- are solved by filling in their HoleDest in-place. Since they
|
|
366 | - -- don't appear in ebv_binds, we keep track of their free
|
|
367 | - -- variables so that we can report unused given constraints
|
|
363 | + ebv_tcvs :: IORef [TcCoercion]
|
|
364 | + -- When we solve a Wanted by filling in a CoercionHole, it is as
|
|
365 | + -- if we were adding an evidence binding
|
|
366 | + -- co_hole := coercion
|
|
367 | + -- We keep all these RHS coercions in a list, alongside `ebv_binds`,
|
|
368 | + -- so that we can report unused given constraints,
|
|
369 | + -- in GHC.Tc.Solver.neededEvVars
|
|
368 | 370 | -- See Note [Tracking redundant constraints] in GHC.Tc.Solver
|
369 | 371 | }
|
370 | 372 | |
... | ... | @@ -372,7 +374,7 @@ data EvBindsVar |
372 | 374 | |
373 | 375 | -- See above for comments on ebv_uniq, ebv_tcvs
|
374 | 376 | ebv_uniq :: Unique,
|
375 | - ebv_tcvs :: IORef CoVarSet
|
|
377 | + ebv_tcvs :: IORef [TcCoercion]
|
|
376 | 378 | }
|
377 | 379 | |
378 | 380 | instance Data.Data TcEvBinds where
|
... | ... | @@ -1762,7 +1762,7 @@ addTopEvBinds new_ev_binds thing_inside |
1762 | 1762 | |
1763 | 1763 | newTcEvBinds :: TcM EvBindsVar
|
1764 | 1764 | newTcEvBinds = do { binds_ref <- newTcRef emptyEvBindMap
|
1765 | - ; tcvs_ref <- newTcRef emptyVarSet
|
|
1765 | + ; tcvs_ref <- newTcRef []
|
|
1766 | 1766 | ; uniq <- newUnique
|
1767 | 1767 | ; traceTc "newTcEvBinds" (text "unique =" <+> ppr uniq)
|
1768 | 1768 | ; return (EvBindsVar { ebv_binds = binds_ref
|
... | ... | @@ -1774,7 +1774,7 @@ newTcEvBinds = do { binds_ref <- newTcRef emptyEvBindMap |
1774 | 1774 | -- must be made monadically
|
1775 | 1775 | newNoTcEvBinds :: TcM EvBindsVar
|
1776 | 1776 | newNoTcEvBinds
|
1777 | - = do { tcvs_ref <- newTcRef emptyVarSet
|
|
1777 | + = do { tcvs_ref <- newTcRef []
|
|
1778 | 1778 | ; uniq <- newUnique
|
1779 | 1779 | ; traceTc "newNoTcEvBinds" (text "unique =" <+> ppr uniq)
|
1780 | 1780 | ; return (CoEvBindsVar { ebv_tcvs = tcvs_ref
|
... | ... | @@ -1785,14 +1785,14 @@ cloneEvBindsVar :: EvBindsVar -> TcM EvBindsVar |
1785 | 1785 | -- solving don't pollute the original
|
1786 | 1786 | cloneEvBindsVar ebv@(EvBindsVar {})
|
1787 | 1787 | = do { binds_ref <- newTcRef emptyEvBindMap
|
1788 | - ; tcvs_ref <- newTcRef emptyVarSet
|
|
1788 | + ; tcvs_ref <- newTcRef []
|
|
1789 | 1789 | ; return (ebv { ebv_binds = binds_ref
|
1790 | 1790 | , ebv_tcvs = tcvs_ref }) }
|
1791 | 1791 | cloneEvBindsVar ebv@(CoEvBindsVar {})
|
1792 | - = do { tcvs_ref <- newTcRef emptyVarSet
|
|
1792 | + = do { tcvs_ref <- newTcRef []
|
|
1793 | 1793 | ; return (ebv { ebv_tcvs = tcvs_ref }) }
|
1794 | 1794 | |
1795 | -getTcEvTyCoVars :: EvBindsVar -> TcM TyCoVarSet
|
|
1795 | +getTcEvTyCoVars :: EvBindsVar -> TcM [TcCoercion]
|
|
1796 | 1796 | getTcEvTyCoVars ev_binds_var
|
1797 | 1797 | = readTcRef (ebv_tcvs ev_binds_var)
|
1798 | 1798 | |
... | ... | @@ -1817,15 +1817,15 @@ updTcEvBinds (EvBindsVar { ebv_binds = old_ebv_ref, ebv_tcvs = old_tcv_ref }) |
1817 | 1817 | = do { new_ebvs <- readTcRef new_ebv_ref
|
1818 | 1818 | ; updTcRef old_ebv_ref (`unionEvBindMap` new_ebvs)
|
1819 | 1819 | ; new_tcvs <- readTcRef new_tcv_ref
|
1820 | - ; updTcRef old_tcv_ref (`unionVarSet` new_tcvs) }
|
|
1820 | + ; updTcRef old_tcv_ref (new_tcvs ++) }
|
|
1821 | 1821 | updTcEvBinds (EvBindsVar { ebv_tcvs = old_tcv_ref })
|
1822 | 1822 | (CoEvBindsVar { ebv_tcvs = new_tcv_ref })
|
1823 | 1823 | = do { new_tcvs <- readTcRef new_tcv_ref
|
1824 | - ; updTcRef old_tcv_ref (`unionVarSet` new_tcvs) }
|
|
1824 | + ; updTcRef old_tcv_ref (new_tcvs ++) }
|
|
1825 | 1825 | updTcEvBinds (CoEvBindsVar { ebv_tcvs = old_tcv_ref })
|
1826 | 1826 | (CoEvBindsVar { ebv_tcvs = new_tcv_ref })
|
1827 | 1827 | = do { new_tcvs <- readTcRef new_tcv_ref
|
1828 | - ; updTcRef old_tcv_ref (`unionVarSet` new_tcvs) }
|
|
1828 | + ; updTcRef old_tcv_ref (new_tcvs ++) }
|
|
1829 | 1829 | updTcEvBinds old_var new_var
|
1830 | 1830 | = pprPanic "updTcEvBinds" (ppr old_var $$ ppr new_var)
|
1831 | 1831 | -- Terms inside types, no good
|
... | ... | @@ -2444,14 +2444,11 @@ checkTypeHasFixedRuntimeRep prov ty = |
2444 | 2444 | unless (typeHasFixedRuntimeRep ty)
|
2445 | 2445 | (addDetailedDiagnostic $ TcRnTypeDoesNotHaveFixedRuntimeRep ty prov)
|
2446 | 2446 | |
2447 | -{-
|
|
2448 | -%************************************************************************
|
|
2449 | -%* *
|
|
2447 | +{- **********************************************************************
|
|
2448 | +* *
|
|
2450 | 2449 | Error messages
|
2451 | 2450 | * *
|
2452 | -*************************************************************************
|
|
2453 | - |
|
2454 | --}
|
|
2451 | +********************************************************************** -}
|
|
2455 | 2452 | |
2456 | 2453 | -- See Note [Naughty quantification candidates]
|
2457 | 2454 | naughtyQuantification :: TcType -- original type user wanted to quantify
|