Simon Peyton Jones pushed to branch wip/T26315 at Glasgow Haskell Compiler / GHC
Commits:
-
c3a4a7df
by Simon Peyton Jones at 2025-09-02T15:32:22+01:00
14 changed files:
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Gen/Sig.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/Solver/Solve.hs
- compiler/GHC/Tc/Types/Origin.hs
- testsuite/tests/deriving/should_fail/T12768.stderr
- testsuite/tests/deriving/should_fail/T1496.stderr
- testsuite/tests/deriving/should_fail/T5498.stderr
- testsuite/tests/deriving/should_fail/T7148.stderr
- testsuite/tests/deriving/should_fail/T7148a.stderr
- testsuite/tests/quantified-constraints/T19690.stderr
- testsuite/tests/quantified-constraints/T21006.stderr
- testsuite/tests/typecheck/should_fail/T15801.stderr
- testsuite/tests/typecheck/should_fail/T22912.stderr
Changes:
| ... | ... | @@ -4197,17 +4197,6 @@ pprTcSolverReportMsg ctxt@(CEC {cec_encl = implics}) |
| 4197 | 4197 | 2 (text "Typeable" <+>
|
| 4198 | 4198 | parens (ppr ty <+> dcolon <+> ppr (typeKind ty)))
|
| 4199 | 4199 | |
| 4200 | - -- When we were originally trying to solve a quantified constraint like
|
|
| 4201 | - -- (forall a. Eq a => Eq (c a))
|
|
| 4202 | - -- add a note to say so, so the overall error looks like
|
|
| 4203 | - -- Cannot deduce Eq (c a)
|
|
| 4204 | - -- from (Eq a)
|
|
| 4205 | - -- when trying to solve (forall a. Eq a => Eq (c a))
|
|
| 4206 | - -- Without this, the error is very inscrutable
|
|
| 4207 | - | ScOrigin (IsQC pred orig) _ <- orig
|
|
| 4208 | - = hang (text "When trying to solve the quantified constraint")
|
|
| 4209 | - 2 (vcat [ ppr pred, pprCtOrigin orig ])
|
|
| 4210 | - |
|
| 4211 | 4200 | | otherwise
|
| 4212 | 4201 | = empty
|
| 4213 | 4202 | |
| ... | ... | @@ -4264,7 +4253,7 @@ pprTcSolverReportMsg (CEC {cec_encl = implics}) (OverlappingInstances item match |
| 4264 | 4253 | -- simply report back the whole given
|
| 4265 | 4254 | -- context. Accelerate Smart.hs showed this problem.
|
| 4266 | 4255 | sep [ text "There exists a (perhaps superclass) match:"
|
| 4267 | - , nest 2 (vcat (pp_givens useful_givens))]
|
|
| 4256 | + , nest 2 (vcat (pp_from_givens useful_givens))]
|
|
| 4268 | 4257 | |
| 4269 | 4258 | , ppWhen (null $ NE.tail matches) $
|
| 4270 | 4259 | parens (vcat [ ppUnless (null tyCoVars) $
|
| ... | ... | @@ -4395,6 +4384,7 @@ pprMismatchMsg ctxt |
| 4395 | 4384 | , mismatch_whenMatching = mb_match_txt
|
| 4396 | 4385 | , mismatch_mb_same_occ = same_occ_info })
|
| 4397 | 4386 | = vcat [ addArising (errorItemCtLoc item) msg
|
| 4387 | + , pprQCOriginExtra item
|
|
| 4398 | 4388 | , ea_extra
|
| 4399 | 4389 | , maybe empty (pprWhenMatching ctxt) mb_match_txt
|
| 4400 | 4390 | , maybe empty pprSameOccInfo same_occ_info ]
|
| ... | ... | @@ -4447,9 +4437,10 @@ pprMismatchMsg ctxt |
| 4447 | 4437 | , teq_mismatch_actual = act -- the mis-match
|
| 4448 | 4438 | , teq_mismatch_what = mb_thing
|
| 4449 | 4439 | , teq_mb_same_occ = mb_same_occ })
|
| 4450 | - = addArising ct_loc $
|
|
| 4451 | - pprWithInvisibleBitsWhen ppr_invis_bits msg
|
|
| 4452 | - $$ maybe empty pprSameOccInfo mb_same_occ
|
|
| 4440 | + = vcat [ addArising ct_loc $
|
|
| 4441 | + pprWithInvisibleBitsWhen ppr_invis_bits msg
|
|
| 4442 | + $$ maybe empty pprSameOccInfo mb_same_occ
|
|
| 4443 | + , pprQCOriginExtra item ]
|
|
| 4453 | 4444 | where
|
| 4454 | 4445 | |
| 4455 | 4446 | msg | Just (torc, rep) <- sORTKind_maybe exp
|
| ... | ... | @@ -4565,45 +4556,55 @@ pprMismatchMsg ctxt |
| 4565 | 4556 | starts_with_vowel [] = False
|
| 4566 | 4557 | |
| 4567 | 4558 | pprMismatchMsg ctxt (CouldNotDeduce useful_givens (item :| others) mb_extra)
|
| 4568 | - = main_msg $$
|
|
| 4569 | - case supplementary of
|
|
| 4570 | - Left infos
|
|
| 4571 | - -> vcat (map (pprExpectedActualInfo ctxt) infos)
|
|
| 4572 | - Right other_msg
|
|
| 4573 | - -> other_msg
|
|
| 4559 | + = vcat [ main_msg
|
|
| 4560 | + , pprQCOriginExtra item
|
|
| 4561 | + , ea_supplementary ]
|
|
| 4574 | 4562 | where
|
| 4575 | 4563 | main_msg
|
| 4576 | - | null useful_givens
|
|
| 4577 | - = addArising ct_loc (no_instance_msg <+> missing)
|
|
| 4578 | - | otherwise
|
|
| 4579 | - = vcat (addArising ct_loc (no_deduce_msg <+> missing)
|
|
| 4580 | - : pp_givens useful_givens)
|
|
| 4581 | - |
|
| 4582 | - supplementary = case mb_extra of
|
|
| 4583 | - Nothing
|
|
| 4584 | - -> Left []
|
|
| 4585 | - Just (CND_Extra level ty1 ty2)
|
|
| 4586 | - -> mk_supplementary_ea_msg ctxt level ty1 ty2 orig
|
|
| 4564 | + | null useful_givens = addArising ct_loc no_instance_msg
|
|
| 4565 | + | otherwise = vcat ( addArising ct_loc no_deduce_msg
|
|
| 4566 | + : pp_from_givens useful_givens)
|
|
| 4567 | + |
|
| 4568 | + ea_supplementary = case mb_extra of
|
|
| 4569 | + Nothing -> empty
|
|
| 4570 | + Just (CND_Extra level ty1 ty2) -> mk_supplementary_ea_msg ctxt level ty1 ty2 orig
|
|
| 4571 | + |
|
| 4587 | 4572 | ct_loc = errorItemCtLoc item
|
| 4588 | 4573 | orig = ctLocOrigin ct_loc
|
| 4589 | 4574 | wanteds = map errorItemPred (item:others)
|
| 4590 | 4575 | |
| 4591 | 4576 | no_instance_msg =
|
| 4592 | 4577 | case wanteds of
|
| 4593 | - [wanted] | Just (tc, _) <- splitTyConApp_maybe wanted
|
|
| 4594 | - -- Don't say "no instance" for a constraint such as "c" for a type variable c.
|
|
| 4595 | - , isClassTyCon tc -> text "No instance for"
|
|
| 4596 | - _ -> text "Could not solve:"
|
|
| 4578 | + [wanted] | -- Guard: don't say "no instance" for a constraint
|
|
| 4579 | + -- such as "c" for a type variable c.
|
|
| 4580 | + Just (tc, _) <- splitTyConApp_maybe wanted
|
|
| 4581 | + , isClassTyCon tc
|
|
| 4582 | + -> text "No instance for" <+> quotes (ppr wanted)
|
|
| 4583 | + _ -> text "Could not solve:" <+> pprTheta wanteds
|
|
| 4597 | 4584 | |
| 4598 | 4585 | no_deduce_msg =
|
| 4599 | 4586 | case wanteds of
|
| 4600 | - [_wanted] -> text "Could not deduce"
|
|
| 4601 | - _ -> text "Could not deduce:"
|
|
| 4602 | - |
|
| 4603 | - missing =
|
|
| 4604 | - case wanteds of
|
|
| 4605 | - [wanted] -> quotes (ppr wanted)
|
|
| 4606 | - _ -> pprTheta wanteds
|
|
| 4587 | + [wanted] -> text "Could not deduce" <+> quotes (ppr wanted)
|
|
| 4588 | + _ -> text "Could not deduce:" <+> pprTheta wanteds
|
|
| 4589 | + |
|
| 4590 | +pprQCOriginExtra :: ErrorItem -> SDoc
|
|
| 4591 | +-- When we were originally trying to solve a quantified constraint like
|
|
| 4592 | +-- (forall a. Eq a => Eq (c a))
|
|
| 4593 | +-- add a note to say so, so the overall error looks like
|
|
| 4594 | +-- Cannot deduce Eq (c a)
|
|
| 4595 | +-- from (Eq a)
|
|
| 4596 | +-- when trying to solve (forall a. Eq a => Eq (c a))
|
|
| 4597 | +-- Without this, the error is very inscrutable
|
|
| 4598 | +-- See (WFA3) in Note [Solving a Wanted forall-constraint],
|
|
| 4599 | +-- in GHC.Tc.Solver.Solve
|
|
| 4600 | +pprQCOriginExtra item
|
|
| 4601 | + | ScOrigin (IsQC pred orig) _ <- orig
|
|
| 4602 | + = hang (text "When trying to solve the quantified constraint")
|
|
| 4603 | + 2 (vcat [ ppr pred, pprCtOrigin orig ])
|
|
| 4604 | + | otherwise
|
|
| 4605 | + = empty
|
|
| 4606 | + where
|
|
| 4607 | + orig = ctLocOrigin (errorItemCtLoc item)
|
|
| 4607 | 4608 | |
| 4608 | 4609 | pprKindMismatchMsg :: TypedThing -> Type -> Type -> SDoc
|
| 4609 | 4610 | pprKindMismatchMsg thing exp act
|
| ... | ... | @@ -4908,10 +4909,7 @@ pprWhenMatching ctxt (WhenMatching cty1 cty2 sub_o mb_sub_t_or_k) = |
| 4908 | 4909 | where
|
| 4909 | 4910 | sub_t_or_k = mb_sub_t_or_k `orElse` TypeLevel
|
| 4910 | 4911 | sub_whats = text (levelString sub_t_or_k) <> char 's'
|
| 4911 | - supplementary =
|
|
| 4912 | - case mk_supplementary_ea_msg ctxt sub_t_or_k cty1 cty2 sub_o of
|
|
| 4913 | - Left infos -> vcat $ map (pprExpectedActualInfo ctxt) infos
|
|
| 4914 | - Right msg -> msg
|
|
| 4912 | + supplementary = mk_supplementary_ea_msg ctxt sub_t_or_k cty1 cty2 sub_o
|
|
| 4915 | 4913 | |
| 4916 | 4914 | pprTyVarInfo :: SolverReportErrCtxt -> TyVarInfo -> SDoc
|
| 4917 | 4915 | pprTyVarInfo ctxt (TyVarInfo { thisTyVar = tv1, otherTy = mb_tv2, thisTyVarIsUntouchable = mb_implic })
|
| ... | ... | @@ -5294,8 +5292,8 @@ usefulContext implics pred |
| 5294 | 5292 | implausible_info _ = False
|
| 5295 | 5293 | -- Do not suggest adding constraints to an *inferred* type signature
|
| 5296 | 5294 | |
| 5297 | -pp_givens :: [Implication] -> [SDoc]
|
|
| 5298 | -pp_givens givens
|
|
| 5295 | +pp_from_givens :: [Implication] -> [SDoc]
|
|
| 5296 | +pp_from_givens givens
|
|
| 5299 | 5297 | = case givens of
|
| 5300 | 5298 | [] -> []
|
| 5301 | 5299 | (g:gs) -> ppr_given (text "from the context:") g
|
| ... | ... | @@ -5465,13 +5463,15 @@ skolsSpan skol_tvs = foldr1WithDefault noSrcSpan combineSrcSpans (map getSrcSpan |
| 5465 | 5463 | **********************************************************************-}
|
| 5466 | 5464 | |
| 5467 | 5465 | mk_supplementary_ea_msg :: SolverReportErrCtxt -> TypeOrKind
|
| 5468 | - -> Type -> Type -> CtOrigin -> Either [ExpectedActualInfo] SDoc
|
|
| 5466 | + -> Type -> Type -> CtOrigin -> SDoc
|
|
| 5469 | 5467 | mk_supplementary_ea_msg ctxt level ty1 ty2 orig
|
| 5470 | 5468 | | TypeEqOrigin { uo_expected = exp, uo_actual = act } <- orig
|
| 5471 | 5469 | , not (ea_looks_same ty1 ty2 exp act)
|
| 5472 | - = mk_ea_msg ctxt Nothing level orig
|
|
| 5470 | + = case mk_ea_msg ctxt Nothing level orig of
|
|
| 5471 | + Left infos -> vcat $ map (pprExpectedActualInfo ctxt) infos
|
|
| 5472 | + Right msg -> msg
|
|
| 5473 | 5473 | | otherwise
|
| 5474 | - = Left []
|
|
| 5474 | + = empty
|
|
| 5475 | 5475 | |
| 5476 | 5476 | ea_looks_same :: Type -> Type -> Type -> Type -> Bool
|
| 5477 | 5477 | -- True if the faulting types (ty1, ty2) look the same as
|
| ... | ... | @@ -976,6 +976,8 @@ tcSpecPrag poly_id (SpecSigE nm rule_bndrs spec_e inl) |
| 976 | 976 | tcInferRho spec_e
|
| 977 | 977 | |
| 978 | 978 | -- (2) Solve the resulting wanteds
|
| 979 | + -- When doing so, switch on `tcsmFullySolveQCIs`; see (WFA4) in
|
|
| 980 | + -- Note [Solving a Wanted forall-constraint] in GHC.Tc.Solver.Solve
|
|
| 979 | 981 | ; ev_binds_var <- newTcEvBinds
|
| 980 | 982 | ; spec_e_wanted <- setTcLevel rhs_tclvl $
|
| 981 | 983 | runTcSWithEvBinds ev_binds_var $
|
| ... | ... | @@ -908,7 +908,7 @@ data TcSMode |
| 908 | 908 | , tcsmSkipOverlappable :: Bool
|
| 909 | 909 | -- ^ Do not select an OVERLAPPABLE instance
|
| 910 | 910 | , tcsmFullySolveQCIs :: Bool
|
| 911 | - -- ^ Fully solve all constraints, without using local Givens
|
|
| 911 | + -- ^ Fully solve quantified constraints
|
|
| 912 | 912 | }
|
| 913 | 913 | |
| 914 | 914 | vanillaTcSMode :: TcSMode
|
| ... | ... | @@ -1281,7 +1281,8 @@ tryShortCutTcS :: TcS Bool -> TcS Bool |
| 1281 | 1281 | -- Use only by the short-cut solver;
|
| 1282 | 1282 | -- see Note [Shortcut solving] in GHC.Tc.Solver.Dict
|
| 1283 | 1283 | tryShortCutTcS (TcS thing_inside)
|
| 1284 | - = TcS $ \ env@(TcSEnv { tcs_inerts = inerts_var
|
|
| 1284 | + = TcS $ \ env@(TcSEnv { tcs_mode = mode
|
|
| 1285 | + , tcs_inerts = inerts_var
|
|
| 1285 | 1286 | , tcs_ev_binds = old_ev_binds_var }) ->
|
| 1286 | 1287 | do { -- Initialise a fresh inert set, with no Givens and no Wanteds
|
| 1287 | 1288 | -- (i.e. empty `inert_cans`)
|
| ... | ... | @@ -1297,7 +1298,7 @@ tryShortCutTcS (TcS thing_inside) |
| 1297 | 1298 | |
| 1298 | 1299 | ; new_wl_var <- TcM.newTcRef emptyWorkList
|
| 1299 | 1300 | ; new_ev_binds_var <- TcM.cloneEvBindsVar old_ev_binds_var
|
| 1300 | - ; let nest_env = env { tcs_mode = vanillaTcSMode { tcsmSkipOverlappable = True }
|
|
| 1301 | + ; let nest_env = env { tcs_mode = mode { tcsmSkipOverlappable = True }
|
|
| 1301 | 1302 | , tcs_ev_binds = new_ev_binds_var
|
| 1302 | 1303 | , tcs_inerts = new_inert_var
|
| 1303 | 1304 | , tcs_worklist = new_wl_var }
|
| ... | ... | @@ -124,12 +124,10 @@ simplify_loop n limit definitely_redo_implications |
| 124 | 124 | -- Any insoluble constraints are in 'simples' and so get rewritten
|
| 125 | 125 | -- See Note [Rewrite insolubles] in GHC.Tc.Solver.InertSet
|
| 126 | 126 | |
| 127 | - ; mode <- getTcSMode
|
|
| 128 | - ; extras :: Bag (Either Ct Implication) <- mapMaybeBagM (solveWantedQCI mode) simples1
|
|
| 129 | - ; let simples' :: Cts -- simples1 are all Wanteds
|
|
| 130 | - implics_from_qcis :: Bag Implication
|
|
| 131 | - (simples', implics_from_qcis) = partitionBagWith id extras
|
|
| 127 | + -- Now try to solve any Wanted QCInsts in `simples1`
|
|
| 128 | + ; (simples', implics_from_qcis) <- solveWantedQCIs simples1
|
|
| 132 | 129 | |
| 130 | + -- Next, solve implications from wc_impl
|
|
| 133 | 131 | ; implics' <- if not definitely_redo_implications -- See Note [Superclass iteration]
|
| 134 | 132 | && unifs1 == 0 -- for this conditional
|
| 135 | 133 | then return implics
|
| ... | ... | @@ -1319,53 +1317,22 @@ and discharge df thus: |
| 1319 | 1317 | df = /\ab. \g1 g2. let <binds> in d
|
| 1320 | 1318 | where <binds> is filled in by solving the implication constraint.
|
| 1321 | 1319 | |
| 1322 | -What we actually do is:
|
|
| 1323 | -* In `solveForAll` we see if we have an identical quantified constraint
|
|
| 1324 | - to solve it (tryInertQCs), and then just add it to `inert_qcis :: [QCInst]`
|
|
| 1325 | -* In the main solver loop, `GHC.Tc.Solver.Solve.simplify_loop`,
|
|
| 1326 | - - We attempt to solve the `wc_simple` constraints with `solveSimpleWanteds`
|
|
| 1327 | - - we grab grab any unsolved Wanted quantified constraints
|
|
| 1328 | - from that attempt; they are all `QCInst` constraints.
|
|
| 1329 | - - we turn those Wanted `QCInst` constraints into `Implication`s,
|
|
| 1330 | - using `wantedQciToImplic`.
|
|
| 1331 | - - we add them to `wc_implic` and do `solveImplications`
|
|
| 1332 | - |
|
| 1333 | -We do not /actually/ emit an implication to solve later. Rather we
|
|
| 1334 | -try to solve it completely immediately using `trySolveImplication`
|
|
| 1335 | - - If successful, we can build evidence
|
|
| 1336 | - - If unsuccessful, we abandon the attempt and add the unsolved
|
|
| 1337 | - forall-constraint to the inert set.
|
|
| 1338 | - |
|
| 1339 | -There are several reasons for this "solve immediately" approach
|
|
| 1320 | +What we actually do is this:
|
|
| 1340 | 1321 | |
| 1341 | -* It saves quite a bit of plumbing, tracking the emitted implications for
|
|
| 1342 | - later solving; and the evidence would have to contain as-yet-incomplte
|
|
| 1343 | - bindings which complicates tracking of unused Givens.
|
|
| 1344 | - |
|
| 1345 | -* We get better error messages, about failing to solve, say
|
|
| 1346 | - (forall a. a->a) ~ (forall b. b->Int)
|
|
| 1347 | - |
|
| 1348 | -* Consider
|
|
| 1349 | - f :: forall f a. (Ix a, forall x. Eq x => Eq (f x)) => a -> f a
|
|
| 1350 | - {-# SPECIALISE f :: forall f. (forall x. Eq x => Eq (f x)) => Int -> f Int #-}
|
|
| 1351 | - This SPECIALISE is treated like an expression with a type signature, so
|
|
| 1352 | - we instantiate the constraints, simplify them and re-generalise. From the
|
|
| 1353 | - instantiation we get [W] d :: (forall x. Eq a => Eq (f x))
|
|
| 1354 | - and we want to generalise over that. We do not want to attempt to solve it
|
|
| 1355 | - and then get stuck, and emit an error message. If we can't solve it, better
|
|
| 1356 | - to leave it alone.
|
|
| 1322 | +* In `solveForAll` we see if we have an identical quantified constraint
|
|
| 1323 | + to solve it (using tryInertQCs), and then just add it to `inert_qcis :: [QCInst]`
|
|
| 1324 | + See Note [Solving Wanted QCs from Given QCs]
|
|
| 1357 | 1325 | |
| 1358 | - We still need to simplify quantified constraints that can be
|
|
| 1359 | - /fully solved/ from instances, otherwise we would never be able to
|
|
| 1360 | - specialise them away. Example: {-# SPECIALISE f @[] @a #-}.
|
|
| 1326 | +* In the main solver loop, `GHC.Tc.Solver.Solve.simplify_loop`:
|
|
| 1361 | 1327 | |
| 1362 | - This last point is a big one: it was the immediate driver for moving to
|
|
| 1363 | - the "solve immediately" approach.
|
|
| 1328 | + - We attempt to solve the `wc_simple` constraints with `solveSimpleWanteds`
|
|
| 1329 | + Unsolved quantified constraints just accumulate in the `inert_qcis` field
|
|
| 1330 | + of the `InertSet`.
|
|
| 1364 | 1331 | |
| 1365 | -You might worry about the wasted work from failed attempts to fully-solve, but
|
|
| 1366 | -it is seldom repeated (because the constraint solver seldom iterates much).
|
|
| 1332 | + - Then we use `solveWantedQCIs` to solve any quantified constraints. It usually
|
|
| 1333 | + turns the `QCInst` into an `Implication`; but not invariably (WFA4)
|
|
| 1367 | 1334 | |
| 1368 | -There are some tricky corners though:
|
|
| 1335 | +Wrinkles:
|
|
| 1369 | 1336 | |
| 1370 | 1337 | (WFA1) We can take a more straightforward path when there is a matching Given, e.g.
|
| 1371 | 1338 | [W] dg :: forall c d. (Eq c, Ord d) => C x c d
|
| ... | ... | @@ -1386,16 +1353,38 @@ There are some tricky corners though: |
| 1386 | 1353 | * Give the Givens a CtOrigin of (GivenOrigin (InstSkol IsQC head_size))
|
| 1387 | 1354 | * Give the Wanted a CtOrigin of (ScOrigin IsQC NakedSc)
|
| 1388 | 1355 | |
| 1389 | - Both of these things are done in solveForAll. Now the mechanism described
|
|
| 1356 | + Both of these things are done in `solveWantedQCI`. Now the mechanism described
|
|
| 1390 | 1357 | in Note [Solving superclass constraints] in GHC.Tc.TyCl.Instance takes over.
|
| 1391 | 1358 | |
| 1392 | -(WFA3) When inferring an appropriate context for a `deriving` instance, we
|
|
| 1393 | - really /do/ want to generate an implication, and perhaps leave it half-solved,
|
|
| 1394 | - from where we might then gather unsolved class constraints (via
|
|
| 1395 | - `approximateWC` called in GHC.Tc.Deriv.Infer.simplifyDeriv). Rather than
|
|
| 1396 | - have a complicated special solver mode, we simply generate an /implication/
|
|
| 1397 | - in the first place, in GHC.Tc.Deriv.Utils.emitPredSpecConstraints.
|
|
| 1398 | - See Note [Inferred contexts from method constraints] in GHC.Tc.Deriv.Infer
|
|
| 1359 | +(WFA3) Error messages. Suppose we are trying to solve the quantified constraint
|
|
| 1360 | + forall a. Eq a => Eq (c a)
|
|
| 1361 | + We don't just want to say "No instance for Eq (c a)". It /really/ helps to
|
|
| 1362 | + say what quantified constraint we were trying to solve.
|
|
| 1363 | + |
|
| 1364 | + So the `IsQC` origin carries that info, and `GHC.Tc.Errors.Ppr.pprQCOriginExtra`
|
|
| 1365 | + prints the extra info.
|
|
| 1366 | + |
|
| 1367 | +(WFA4) Consider
|
|
| 1368 | + f :: forall f a. (Ix a, forall x. Eq x => Eq (f x)) => a -> f a
|
|
| 1369 | + {-# SPECIALISE f :: forall f. (forall x. Eq x => Eq (f x)) => Int -> f Int #-}
|
|
| 1370 | + This SPECIALISE is treated like an expression with a type signature, so
|
|
| 1371 | + we instantiate the constraints, simplify them and re-generalise. From the
|
|
| 1372 | + instantiation we get [W] d :: (forall x. Eq a => Eq (f x))
|
|
| 1373 | + and we want to generalise over that. We do not want to attempt to solve it
|
|
| 1374 | + and then get stuck, and emit an error message. If we can't solve it, it is
|
|
| 1375 | + much, much better to leave it alone.
|
|
| 1376 | + |
|
| 1377 | + We still need to simplify quantified constraints that can be /fully solved/
|
|
| 1378 | + from instances, otherwise we would never be able to specialise them
|
|
| 1379 | + away. Example: {-# SPECIALISE f @[] @a #-}. So:
|
|
| 1380 | + |
|
| 1381 | + * The constraint solver has a mode flag `tcsmFullySolveQCIs` that says
|
|
| 1382 | + "fully solve quantified constraint, or leave them alone
|
|
| 1383 | + * When simplifying constraints in a SPECIALISE pragma, we switch on this
|
|
| 1384 | + flag (see `GHC.Tc.Gen.Sig.tcSpecPrag`, the `SpecSigE` case).
|
|
| 1385 | + |
|
| 1386 | + You might worry about the wasted work from failed attempts to fully-solve, but
|
|
| 1387 | + it is seldom repeated (because the constraint solver seldom iterates much).
|
|
| 1399 | 1388 | |
| 1400 | 1389 | Note [Solving Wanted QCs from Given QCs]
|
| 1401 | 1390 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... | ... | @@ -1505,13 +1494,23 @@ try_inert_qcs (QCI { qci_ev = ev_w }) inerts = |
| 1505 | 1494 | | otherwise
|
| 1506 | 1495 | = Nothing
|
| 1507 | 1496 | |
| 1497 | +solveWantedQCIs :: Cts -> TcS (Cts, Bag Implication)
|
|
| 1498 | +solveWantedQCIs wanteds
|
|
| 1499 | + = do { mode <- getTcSMode
|
|
| 1500 | + ; bag_of_eithers <- mapBagM (solveWantedQCI mode) wanteds
|
|
| 1501 | + -- bag_of_eithers :: Bag (Either Ct Implication)
|
|
| 1502 | + ; return (partitionBagWith id bag_of_eithers) }
|
|
| 1503 | + |
|
| 1508 | 1504 | solveWantedQCI :: TcSMode
|
| 1509 | 1505 | -> Ct -- Definitely a Wanted
|
| 1510 | - -> TcS (Maybe (Either Ct Implication))
|
|
| 1511 | --- Try to solve a quantified constraint.
|
|
| 1512 | --- In TcSChortCut mode, insist on solving it fully or not at all
|
|
| 1506 | + -> TcS (Either Ct Implication)
|
|
| 1507 | +-- Try to solve a quantified constraint, `ct`
|
|
| 1513 | 1508 | -- Returns
|
| 1514 | --- No-op on all Cts other than CQuantCan
|
|
| 1509 | +-- (Left ct) if `ct` is not a quantified constraint
|
|
| 1510 | +-- (Right implic) if we can solve a quantified constraint `ct` by creating
|
|
| 1511 | +-- an implication and fully or partly solving it
|
|
| 1512 | +-- (Left ct) for a quantified constraint that can't be /fully solved/,
|
|
| 1513 | +-- but mode is tcsmFullySolveQCIs
|
|
| 1515 | 1514 | -- See Note [Solving a Wanted forall-constraint]
|
| 1516 | 1515 | solveWantedQCI mode ct@(CQuantCan (QCI { qci_ev = ev, qci_tvs = tvs
|
| 1517 | 1516 | , qci_theta = theta, qci_body = body_pred }))
|
| ... | ... | @@ -1543,7 +1542,8 @@ solveWantedQCI mode ct@(CQuantCan (QCI { qci_ev = ev, qci_tvs = tvs |
| 1543 | 1542 | ; newWantedNC loc' rewriters inst_pred }
|
| 1544 | 1543 | |
| 1545 | 1544 | ; ev_binds_var <- TcS.newTcEvBinds
|
| 1546 | - ; let imp = (implicationPrototype (ctLocEnv loc))
|
|
| 1545 | + ; let imp :: Implication
|
|
| 1546 | + imp = (implicationPrototype (ctLocEnv loc))
|
|
| 1547 | 1547 | { ic_tclvl = lvl
|
| 1548 | 1548 | , ic_skols = skol_tvs
|
| 1549 | 1549 | , ic_given = given_ev_vars
|
| ... | ... | @@ -1558,7 +1558,7 @@ solveWantedQCI mode ct@(CQuantCan (QCI { qci_ev = ev, qci_tvs = tvs |
| 1558 | 1558 | , not (isSolvedStatus (ic_status imp'))
|
| 1559 | 1559 | -> -- Not fully solved, but mode says that we must fully
|
| 1560 | 1560 | -- solve quantified constraints; so abandon the attempt
|
| 1561 | - return (Just (Left ct))
|
|
| 1561 | + return (Left ct)
|
|
| 1562 | 1562 | |
| 1563 | 1563 | | otherwise
|
| 1564 | 1564 | -> -- Record evidence and return residual implication
|
| ... | ... | @@ -1570,14 +1570,14 @@ solveWantedQCI mode ct@(CQuantCan (QCI { qci_ev = ev, qci_tvs = tvs |
| 1570 | 1570 | , et_binds = TcEvBinds ev_binds_var
|
| 1571 | 1571 | , et_body = wantedCtEvEvId wanted_ev }
|
| 1572 | 1572 | |
| 1573 | - ; return (Just (Right imp')) }
|
|
| 1573 | + ; return (Right imp') }
|
|
| 1574 | 1574 | }
|
| 1575 | 1575 | |
| 1576 | 1576 | | otherwise -- A Given QCInst
|
| 1577 | 1577 | = pprPanic "wantedQciToImplic: found a Given QCI" (ppr ct)
|
| 1578 | 1578 | |
| 1579 | 1579 | -- No-op on all Ct's other than CQuantCan
|
| 1580 | -solveWantedQCI _ ct = return (Just (Left ct))
|
|
| 1580 | +solveWantedQCI _ ct = return (Left ct)
|
|
| 1581 | 1581 | |
| 1582 | 1582 | |
| 1583 | 1583 | {-
|
| ... | ... | @@ -910,8 +910,9 @@ ppr_ct_origin herald (ScOrigin IsClsInst nkd) |
| 910 | 910 | |
| 911 | 911 | ppr_ct_origin _herald (ScOrigin (IsQC pred orig) nkd)
|
| 912 | 912 | = vcat [ whenPprDebug (text "IsQC" <> braces (text "sc-origin:" <> ppr nkd) <+> ppr pred)
|
| 913 | - , ppr_ct_origin (text "arising (via a quantified constraint) from") orig ]
|
|
| 914 | - -- Just print `orig`, with a new herald
|
|
| 913 | + , hang (text "arising (via a quantified constraint) from") 2 (pprCtO orig) ]
|
|
| 914 | + -- Print `orig` briefly with pprCtO. We'll print it more voluminously later.
|
|
| 915 | + -- See GHC.Tc.Errors.Ppr.pprQCOriginExtra
|
|
| 915 | 916 | |
| 916 | 917 | ppr_ct_origin herald (NonLinearPatternOrigin reason pat)
|
| 917 | 918 | = hang (herald <+> text "a non-linear pattern" <+> quotes (ppr pat))
|
| 1 | 1 | T12768.hs:9:33: error: [GHC-39999]
|
| 2 | 2 | • Could not deduce ‘D [a]’
|
| 3 | - arising from the coercion of the method ‘op’
|
|
| 4 | - from type ‘D [a] => [a] -> [a]’ to type ‘D (N a) => N a -> N a’
|
|
| 3 | + arising (via a quantified constraint) from a derived method
|
|
| 5 | 4 | from the context: C a
|
| 6 | 5 | bound by the deriving clause for ‘C (N a)’ at T12768.hs:9:33
|
| 7 | - or from: D (N a)
|
|
| 8 | - bound by the method declaration for op at T12768.hs:9:33
|
|
| 9 | - Possible fix:
|
|
| 10 | - use a standalone 'deriving instance' declaration,
|
|
| 11 | - so you can specify the instance context yourself
|
|
| 6 | + or from: D (N a) bound by a quantified constraint at T12768.hs:9:33
|
|
| 7 | + When trying to solve the quantified constraint
|
|
| 8 | + D (N a) => (Coercible ([a] -> [a]) (N a -> N a), D [a])
|
|
| 9 | + arising from the coercion of the method ‘op’
|
|
| 10 | + from type ‘D [a] => [a] -> [a]’ to type ‘D (N a) => N a -> N a’
|
|
| 12 | 11 | • When deriving the instance for (C (N a))
|
| 13 | 12 |
| 1 | 1 | T1496.hs:10:32: error: [GHC-18872]
|
| 2 | 2 | • Couldn't match representation of type: c Int
|
| 3 | 3 | with that of: c Moo
|
| 4 | + arising (via a quantified constraint) from a derived method
|
|
| 5 | + When trying to solve the quantified constraint
|
|
| 6 | + forall (c :: * -> *). Coercible (c Int -> c Int) (c Int -> c Moo)
|
|
| 4 | 7 | arising from the coercion of the method ‘isInt’
|
| 5 | 8 | from type ‘forall (c :: * -> *). c Int -> c Int’
|
| 6 | 9 | to type ‘forall (c :: * -> *). c Int -> c Moo’
|
| 1 | 1 | T5498.hs:30:39: error: [GHC-18872]
|
| 2 | 2 | • Couldn't match representation of type: c a
|
| 3 | 3 | with that of: c (Down a)
|
| 4 | + arising (via a quantified constraint) from a derived method
|
|
| 5 | + When trying to solve the quantified constraint
|
|
| 6 | + forall (c :: * -> *).
|
|
| 7 | + Coercible (c a -> c Int) (c (Down a) -> c Int)
|
|
| 4 | 8 | arising from the coercion of the method ‘intIso’
|
| 5 | 9 | from type ‘forall (c :: * -> *). c a -> c Int’
|
| 6 | 10 | to type ‘forall (c :: * -> *). c (Down a) -> c Int’
|
| 1 | 1 | T7148.hs:27:40: error: [GHC-25897]
|
| 2 | 2 | • Couldn't match type ‘b’ with ‘Tagged a b’
|
| 3 | - arising from the coercion of the method ‘iso1’
|
|
| 4 | - from type ‘forall b1. SameType () b1 -> SameType b b1’
|
|
| 5 | - to type ‘forall b1. SameType () b1 -> SameType (Tagged a b) b1’
|
|
| 3 | + arising (via a quantified constraint) from a derived method
|
|
| 4 | + When trying to solve the quantified constraint
|
|
| 5 | + forall b1.
|
|
| 6 | + Coercible
|
|
| 7 | + (SameType b1 () -> SameType b1 b)
|
|
| 8 | + (SameType b1 () -> SameType b1 (Tagged a b))
|
|
| 9 | + arising from the coercion of the method ‘iso2’
|
|
| 10 | + from type ‘forall b1. SameType b1 () -> SameType b1 b’
|
|
| 11 | + to type ‘forall b1. SameType b1 () -> SameType b1 (Tagged a b)’
|
|
| 6 | 12 | ‘b’ is a rigid type variable bound by
|
| 7 | 13 | the deriving clause for ‘IsoUnit (Tagged a b)’
|
| 8 | 14 | at T7148.hs:27:40-46
|
| ... | ... | @@ -10,9 +16,15 @@ T7148.hs:27:40: error: [GHC-25897] |
| 10 | 16 | |
| 11 | 17 | T7148.hs:27:40: error: [GHC-25897]
|
| 12 | 18 | • Couldn't match type ‘b’ with ‘Tagged a b’
|
| 13 | - arising from the coercion of the method ‘iso2’
|
|
| 14 | - from type ‘forall b1. SameType b1 () -> SameType b1 b’
|
|
| 15 | - to type ‘forall b1. SameType b1 () -> SameType b1 (Tagged a b)’
|
|
| 19 | + arising (via a quantified constraint) from a derived method
|
|
| 20 | + When trying to solve the quantified constraint
|
|
| 21 | + forall b1.
|
|
| 22 | + Coercible
|
|
| 23 | + (SameType () b1 -> SameType b b1)
|
|
| 24 | + (SameType () b1 -> SameType (Tagged a b) b1)
|
|
| 25 | + arising from the coercion of the method ‘iso1’
|
|
| 26 | + from type ‘forall b1. SameType () b1 -> SameType b b1’
|
|
| 27 | + to type ‘forall b1. SameType () b1 -> SameType (Tagged a b) b1’
|
|
| 16 | 28 | ‘b’ is a rigid type variable bound by
|
| 17 | 29 | the deriving clause for ‘IsoUnit (Tagged a b)’
|
| 18 | 30 | at T7148.hs:27:40-46
|
| 1 | 1 | T7148a.hs:19:50: error: [GHC-10283]
|
| 2 | 2 | • Couldn't match representation of type ‘b’
|
| 3 | 3 | with that of ‘Result a b’
|
| 4 | + arising (via a quantified constraint) from a derived method
|
|
| 5 | + When trying to solve the quantified constraint
|
|
| 6 | + forall b.
|
|
| 7 | + Coercible
|
|
| 8 | + (Proxy b -> a -> Result a b) (Proxy b -> IS_NO_LONGER a -> b)
|
|
| 4 | 9 | arising from the coercion of the method ‘coerce’
|
| 5 | 10 | from type ‘forall b. Proxy b -> a -> Result a b’
|
| 6 | 11 | to type ‘forall b.
|
| 7 | 12 | Proxy b -> IS_NO_LONGER a -> Result (IS_NO_LONGER a) b’
|
| 8 | 13 | ‘b’ is a rigid type variable bound by
|
| 9 | - the method declaration for coerce
|
|
| 14 | + a quantified constraint
|
|
| 10 | 15 | at T7148a.hs:19:50-56
|
| 11 | 16 | • When deriving the instance for (Convert (IS_NO_LONGER a))
|
| 12 | 17 |
| 1 | 1 | T19690.hs:12:16: error: [GHC-05617]
|
| 2 | - • Could not solve: ‘Hold c => c’ arising from a use of ‘go’
|
|
| 2 | + • Could not deduce ‘c’
|
|
| 3 | + arising (via a quantified constraint) from a use of ‘go’
|
|
| 4 | + from the context: Hold c
|
|
| 5 | + bound by a quantified constraint at T19690.hs:12:16-17
|
|
| 6 | + When trying to solve the quantified constraint
|
|
| 7 | + Hold c => c
|
|
| 8 | + arising from a use of ‘go’
|
|
| 3 | 9 | • In the expression: go
|
| 4 | 10 | In an equation for ‘anythingDict’:
|
| 5 | 11 | anythingDict
|
| ... | ... | @@ -7,4 +13,6 @@ T19690.hs:12:16: error: [GHC-05617] |
| 7 | 13 | where
|
| 8 | 14 | go :: (Hold c => c) => Dict c
|
| 9 | 15 | go = Dict
|
| 16 | + • Relevant bindings include
|
|
| 17 | + anythingDict :: Dict c (bound at T19690.hs:12:1)
|
|
| 10 | 18 |
| 1 | 1 | T21006.hs:14:10: error: [GHC-05617]
|
| 2 | - • Could not solve: ‘forall b (c :: Constraint).
|
|
| 3 | - (Determines b, Determines c) =>
|
|
| 4 | - c’
|
|
| 2 | + • Could not deduce ‘c’
|
|
| 3 | + arising (via a quantified constraint) from
|
|
| 4 | + the superclasses of an instance declaration
|
|
| 5 | + from the context: (Determines b, Determines c)
|
|
| 6 | + bound by a quantified constraint at T21006.hs:14:10-15
|
|
| 7 | + When trying to solve the quantified constraint
|
|
| 8 | + forall b (c :: Constraint). (Determines b, Determines c) => c
|
|
| 5 | 9 | arising from the superclasses of an instance declaration
|
| 6 | 10 | • In the instance declaration for ‘OpCode’
|
| 7 | 11 |
| 1 | -T15801.hs:52:10: error: [GHC-05617]
|
|
| 2 | - • Could not solve: ‘forall (op_a :: Op (*)) (b :: Op (*)).
|
|
| 3 | - op_a -#- b’
|
|
| 1 | +T15801.hs:52:10: error: [GHC-18872]
|
|
| 2 | + • Couldn't match representation of type: UnOp op_a -> UnOp b
|
|
| 3 | + with that of: op_a --> b
|
|
| 4 | + arising (via a quantified constraint) from the superclasses of an instance declaration
|
|
| 5 | + When trying to solve the quantified constraint
|
|
| 6 | + forall (op_a :: Op (*)) (b :: Op (*)). op_a -#- b
|
|
| 4 | 7 | arising from the superclasses of an instance declaration
|
| 5 | 8 | • In the instance declaration for ‘OpRíki (Op (*))’
|
| 6 | 9 |
| 1 | -T22912.hs:17:16: error: [GHC-05617]
|
|
| 2 | - • Could not solve: ‘Exactly (Implies c) => Implies c’
|
|
| 1 | +T22912.hs:17:16: error: [GHC-39999]
|
|
| 2 | + • Could not deduce ‘Implies c’
|
|
| 3 | + arising (via a quantified constraint) from a use of ‘go’
|
|
| 4 | + from the context: Exactly (Implies c)
|
|
| 5 | + bound by a quantified constraint at T22912.hs:17:16-17
|
|
| 6 | + When trying to solve the quantified constraint
|
|
| 7 | + Exactly (Implies c) => Implies c
|
|
| 3 | 8 | arising from a use of ‘go’
|
| 9 | + Possible fix:
|
|
| 10 | + add (Implies c) to the context of
|
|
| 11 | + the type signature for:
|
|
| 12 | + anythingDict :: forall (c :: Constraint). Dict c
|
|
| 4 | 13 | • In the expression: go
|
| 5 | 14 | In an equation for ‘anythingDict’:
|
| 6 | 15 | anythingDict
|