Simon Peyton Jones pushed to branch wip/T26115 at Glasgow Haskell Compiler / GHC
Commits:
-
b2adb180
by Simon Peyton Jones at 2025-07-22T11:46:58+01:00
5 changed files:
- compiler/GHC/Tc/Deriv/Utils.hs
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Solver/Solve.hs
- compiler/GHC/Tc/Types/Origin.hs
- compiler/GHC/Tc/Utils/TcType.hs
Changes:
| ... | ... | @@ -107,12 +107,8 @@ askDerivUserTypeCtxt = asks (go . denv_ctxt) |
| 107 | 107 | go (InferContext Just{}) = InstDeclCtxt True
|
| 108 | 108 | go (InferContext Nothing) = DerivClauseCtxt
|
| 109 | 109 | |
| 110 | --- | @'mkDerivOrigin' wc@ returns 'StandAloneDerivOrigin' if @wc@ is 'True',
|
|
| 111 | --- and 'DerivClauseOrigin' if @wc@ is 'False'. Useful for error-reporting.
|
|
| 112 | 110 | mkDerivOrigin :: Bool -> CtOrigin
|
| 113 | -mkDerivOrigin standalone_wildcard
|
|
| 114 | - | standalone_wildcard = StandAloneDerivOrigin
|
|
| 115 | - | otherwise = DerivClauseOrigin
|
|
| 111 | +mkDerivOrigin standalone = DerivOrigin standalone
|
|
| 116 | 112 | |
| 117 | 113 | -- | Contains all of the information known about a derived instance when
|
| 118 | 114 | -- determining what its @EarlyDerivSpec@ should be.
|
| ... | ... | @@ -562,8 +558,10 @@ data PredSpec |
| 562 | 558 | { sps_pred :: TcPredType
|
| 563 | 559 | -- ^ The constraint to emit as a wanted
|
| 564 | 560 | -- Usually just a simple predicate like (Eq a) or (ki ~# Type),
|
| 565 | - -- but (hack) in the case of GHC.Tc.Deriv.Infer.inferConstraintsCoerceBased,
|
|
| 566 | - -- it can be a forall-constraint
|
|
| 561 | + -- but can be a forall-constraint:
|
|
| 562 | + -- * in the case of GHC.Tc.Deriv.Infer.inferConstraintsCoerceBased
|
|
| 563 | + -- * if a class has quantified-constraint superclasses,
|
|
| 564 | + -- via `mkDirectThetaSpec` in `inferConstraints`
|
|
| 567 | 565 | |
| 568 | 566 | , sps_origin :: CtOrigin
|
| 569 | 567 | -- ^ The origin of the constraint
|
| ... | ... | @@ -716,13 +714,16 @@ emitPredSpecConstraints _ (SimplePredSpec { sps_pred = wanted_pred |
| 716 | 714 | -- but here when we are trying to infer the context for an instance decl, we
|
| 717 | 715 | -- need that half-solved implication. See deriving/should_compile/T20815
|
| 718 | 716 | -- and Note [Inferred contexts from method constraints]
|
| 719 | - = do { let skol_info_anon
|
|
| 717 | + = do { let (_,_,head_ty) = tcSplitQuantPredTy wanted_pred -- Yuk
|
|
| 718 | + skol_info_anon
|
|
| 720 | 719 | = case orig of
|
| 721 | 720 | DerivOriginCoerce meth _ _ _ -> MethSkol (idName meth) False
|
| 721 | + DerivOrigin _ -> InstSkol (IsQC orig) (pSizeHead head_ty)
|
|
| 722 | 722 | _ -> pprPanic "emitPredSpecConstraints" (ppr orig $$ ppr wanted_pred)
|
| 723 | - -- We only get a polymorphic wanted_pred from DerivOriginCoerce
|
|
| 724 | - -- This is a bit messy, but arises from the fact that SimplePredSpe
|
|
| 725 | - -- is not really simple!
|
|
| 723 | + -- We only get a polymorphic wanted_pred from limited places
|
|
| 724 | + -- This is a bit messy, but arises from the fact
|
|
| 725 | + -- that SimplePredSpec is not really simple!
|
|
| 726 | + |
|
| 726 | 727 | ; skol_info <- mkSkolemInfo skol_info_anon
|
| 727 | 728 | ; (_wrapper, tv_prs, givens, wanted_rho) <- topSkolemise skol_info wanted_pred
|
| 728 | 729 | -- _wrapper: we ignore the evidence from all these constraints
|
| ... | ... | @@ -4197,8 +4197,7 @@ pprTcSolverReportMsg ctxt@(CEC {cec_encl = implics}) |
| 4197 | 4197 | = empty
|
| 4198 | 4198 | |
| 4199 | 4199 | drv_fixes = case orig of
|
| 4200 | - DerivClauseOrigin -> [drv_fix False]
|
|
| 4201 | - StandAloneDerivOrigin -> [drv_fix True]
|
|
| 4200 | + DerivOrigin standalone -> [drv_fix standalone]
|
|
| 4202 | 4201 | DerivOriginDC _ _ standalone -> [drv_fix standalone]
|
| 4203 | 4202 | DerivOriginCoerce _ _ _ standalone -> [drv_fix standalone]
|
| 4204 | 4203 | _ -> []
|
| ... | ... | @@ -1374,7 +1374,7 @@ solveWantedForAll qci tvs theta body_pred |
| 1374 | 1374 | ; (subst, skol_tvs) <- tcInstSkolTyVarsX skol_info empty_subst tvs
|
| 1375 | 1375 | ; let inst_pred = substTy subst body_pred
|
| 1376 | 1376 | inst_theta = substTheta subst theta
|
| 1377 | - skol_info_anon = InstSkol is_qc (get_size inst_pred) }
|
|
| 1377 | + skol_info_anon = InstSkol is_qc (pSizeHead inst_pred) }
|
|
| 1378 | 1378 | |
| 1379 | 1379 | ; given_ev_vars <- mapM newEvVar inst_theta
|
| 1380 | 1380 | ; (lvl, (w_id, wanteds))
|
| ... | ... | @@ -1388,9 +1388,9 @@ solveWantedForAll qci tvs theta body_pred |
| 1388 | 1388 | ; return ( wantedCtEvEvId wanted_ev
|
| 1389 | 1389 | , unitBag (mkNonCanonical $ CtWanted wanted_ev)) }
|
| 1390 | 1390 | |
| 1391 | - ; traceTcS "solveForAll {" (ppr skol_tvs $$ ppr given_ev_vars $$ ppr wanteds $$ ppr w_id)
|
|
| 1392 | 1391 | |
| 1393 | 1392 | -- Try to solve the constraint completely
|
| 1393 | + ; traceTcS "solveForAll {" (ppr skol_tvs $$ ppr given_ev_vars $$ ppr wanteds $$ ppr w_id)
|
|
| 1394 | 1394 | ; ev_binds_var <- TcS.newTcEvBinds
|
| 1395 | 1395 | ; solved <- trySolveImplication $
|
| 1396 | 1396 | (implicationPrototype loc_env)
|
| ... | ... | @@ -1402,7 +1402,8 @@ solveWantedForAll qci tvs theta body_pred |
| 1402 | 1402 | , ic_given = given_ev_vars
|
| 1403 | 1403 | , ic_wanted = emptyWC { wc_simple = wanteds } }
|
| 1404 | 1404 | ; traceTcS "solveForAll }" (ppr solved)
|
| 1405 | - ; evbs <- TcS.getTcEvBindsMap ev_binds_var
|
|
| 1405 | + |
|
| 1406 | + -- See if we succeeded in solving it completely
|
|
| 1406 | 1407 | ; if not solved
|
| 1407 | 1408 | then do { -- Not completely solved; abandon that attempt and add the
|
| 1408 | 1409 | -- original constraint to the inert set
|
| ... | ... | @@ -1410,7 +1411,8 @@ solveWantedForAll qci tvs theta body_pred |
| 1410 | 1411 | ; stopWith (CtWanted wtd) "Wanted forall-constraint:unsolved" }
|
| 1411 | 1412 | |
| 1412 | 1413 | else do { -- Completely solved; build an evidence term
|
| 1413 | - setWantedEvTerm dest EvCanonical $
|
|
| 1414 | + evbs <- TcS.getTcEvBindsMap ev_binds_var
|
|
| 1415 | + ; setWantedEvTerm dest EvCanonical $
|
|
| 1414 | 1416 | EvFun { et_tvs = skol_tvs, et_given = given_ev_vars
|
| 1415 | 1417 | , et_binds = evBindMapBinds evbs, et_body = w_id }
|
| 1416 | 1418 | ; stopWith (CtWanted wtd) "Wanted forall-constraint:solved" } }
|
| ... | ... | @@ -1421,11 +1423,6 @@ solveWantedForAll qci tvs theta body_pred |
| 1421 | 1423 | empty_subst = mkEmptySubst $ mkInScopeSet $
|
| 1422 | 1424 | tyCoVarsOfTypes (body_pred:theta) `delVarSetList` tvs
|
| 1423 | 1425 | |
| 1424 | - -- Getting the size of the head is a bit horrible
|
|
| 1425 | - -- because of the special treament for class predicates
|
|
| 1426 | - get_size pred = case classifyPredType pred of
|
|
| 1427 | - ClassPred cls tys -> pSizeClassPred cls tys
|
|
| 1428 | - _ -> pSizeType pred
|
|
| 1429 | 1426 | |
| 1430 | 1427 | {- Note [Solving a Wanted forall-constraint]
|
| 1431 | 1428 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... | ... | @@ -24,6 +24,7 @@ module GHC.Tc.Types.Origin ( |
| 24 | 24 | isWantedSuperclassOrigin,
|
| 25 | 25 | ClsInstOrQC(..), NakedScFlag(..), NonLinearPatternReason(..),
|
| 26 | 26 | HsImplicitLiftSplice(..),
|
| 27 | + StandaloneDeriv,
|
|
| 27 | 28 | |
| 28 | 29 | TypedThing(..), TyVarBndrs(..),
|
| 29 | 30 | |
| ... | ... | @@ -568,9 +569,9 @@ data CtOrigin |
| 568 | 569 | ClsInstOrQC -- Whether class instance or quantified constraint
|
| 569 | 570 | NakedScFlag
|
| 570 | 571 | |
| 571 | - | DerivClauseOrigin -- Typechecking a deriving clause (as opposed to
|
|
| 572 | - -- standalone deriving).
|
|
| 573 | - | DerivOriginDC DataCon Int Bool
|
|
| 572 | + | DerivOrigin StandaloneDeriv
|
|
| 573 | + -- Typechecking a `deriving` clause, or a standalone `deriving` declaration
|
|
| 574 | + | DerivOriginDC DataCon Int StandaloneDeriv
|
|
| 574 | 575 | -- Checking constraints arising from this data con and field index. The
|
| 575 | 576 | -- Bool argument in DerivOriginDC and DerivOriginCoerce is True if
|
| 576 | 577 | -- standalong deriving (with a wildcard constraint) is being used. This
|
| ... | ... | @@ -578,14 +579,10 @@ data CtOrigin |
| 578 | 579 | -- the argument is True, then don't recommend "use standalone deriving",
|
| 579 | 580 | -- but rather "fill in the wildcard constraint yourself").
|
| 580 | 581 | -- See Note [Inferring the instance context] in GHC.Tc.Deriv.Infer
|
| 581 | - | DerivOriginCoerce Id Type Type Bool
|
|
| 582 | - -- DerivOriginCoerce id ty1 ty2: Trying to coerce class method `id` from
|
|
| 583 | - -- `ty1` to `ty2`.
|
|
| 584 | - | StandAloneDerivOrigin -- Typechecking stand-alone deriving. Useful for
|
|
| 585 | - -- constraints coming from a wildcard constraint,
|
|
| 586 | - -- e.g., deriving instance _ => Eq (Foo a)
|
|
| 587 | - -- See Note [Inferring the instance context]
|
|
| 588 | - -- in GHC.Tc.Deriv.Infer
|
|
| 582 | + | DerivOriginCoerce Id Type Type StandaloneDeriv
|
|
| 583 | + -- DerivOriginCoerce id ty1 ty2: Trying to coerce class method `id` from
|
|
| 584 | + -- `ty1` to `ty2`.
|
|
| 585 | + |
|
| 589 | 586 | | DefaultOrigin -- Typechecking a default decl
|
| 590 | 587 | | DoOrigin -- Arising from a do expression
|
| 591 | 588 | | DoPatOrigin (LPat GhcRn) -- Arising from a failable pattern in
|
| ... | ... | @@ -659,6 +656,14 @@ data NonLinearPatternReason |
| 659 | 656 | | ViewPatternReason
|
| 660 | 657 | | OtherPatternReason
|
| 661 | 658 | |
| 659 | +type StandaloneDeriv = Bool
|
|
| 660 | + -- False <=> a `deriving` clause on a data/newtype declaration
|
|
| 661 | + -- e.g. data T a = MkT a deriving( Eq )
|
|
| 662 | + -- True <=> a standalone `deriving` clause with a wildcard constraint
|
|
| 663 | + -- e.g deriving instance _ => Eq (T a)
|
|
| 664 | + -- See Note [Inferring the instance context]
|
|
| 665 | + -- in GHC.Tc.Deriv.Infer
|
|
| 666 | + |
|
| 662 | 667 | -- | The number of superclass selections needed to get this Given.
|
| 663 | 668 | -- If @d :: C ty@ has @ScDepth=2@, then the evidence @d@ will look
|
| 664 | 669 | -- like @sc_sel (sc_sel dg)@, where @dg@ is a Given.
|
| ... | ... | @@ -929,8 +934,9 @@ pprCtO TupleOrigin = text "a tuple" |
| 929 | 934 | pprCtO NegateOrigin = text "a use of syntactic negation"
|
| 930 | 935 | pprCtO (ScOrigin IsClsInst _) = text "the superclasses of an instance declaration"
|
| 931 | 936 | pprCtO (ScOrigin (IsQC {}) _) = text "the head of a quantified constraint"
|
| 932 | -pprCtO DerivClauseOrigin = text "the 'deriving' clause of a data type declaration"
|
|
| 933 | -pprCtO StandAloneDerivOrigin = text "a 'deriving' declaration"
|
|
| 937 | +pprCtO (DerivOrigin standalone)
|
|
| 938 | + | standalone = text "a 'deriving' declaration"
|
|
| 939 | + | otherwise = text "the 'deriving' clause of a data type declaration"
|
|
| 934 | 940 | pprCtO DefaultOrigin = text "a 'default' declaration"
|
| 935 | 941 | pprCtO DoOrigin = text "a do statement"
|
| 936 | 942 | pprCtO MCompOrigin = text "a statement in a monad comprehension"
|
| ... | ... | @@ -130,7 +130,7 @@ module GHC.Tc.Utils.TcType ( |
| 130 | 130 | pSizeZero, pSizeOne,
|
| 131 | 131 | pSizeType, pSizeTypeX, pSizeTypes,
|
| 132 | 132 | pSizeClassPred, pSizeClassPredX,
|
| 133 | - pSizeTyConApp,
|
|
| 133 | + pSizeTyConApp, pSizeHead,
|
|
| 134 | 134 | noMoreTyVars, allDistinctTyVars,
|
| 135 | 135 | TypeSize, sizeType, sizeTypes, scopedSort,
|
| 136 | 136 | isTerminatingClass, isStuckTypeFamily,
|
| ... | ... | @@ -2364,6 +2364,13 @@ pSizeTyFamApp tc |
| 2364 | 2364 | | isStuckTypeFamily tc = pSizeZero
|
| 2365 | 2365 | | otherwise = PS_TyFam tc
|
| 2366 | 2366 | |
| 2367 | +pSizeHead :: PredType -> PatersonSize
|
|
| 2368 | +-- Getting the size of an instance head is a bit horrible
|
|
| 2369 | +-- because of the special treament for class predicates
|
|
| 2370 | +pSizeHead pred = case classifyPredType pred of
|
|
| 2371 | + ClassPred cls tys -> pSizeClassPred cls tys
|
|
| 2372 | + _ -> pSizeType pred
|
|
| 2373 | + |
|
| 2367 | 2374 | pSizeClassPred :: Class -> [Type] -> PatersonSize
|
| 2368 | 2375 | pSizeClassPred = pSizeClassPredX emptyVarSet
|
| 2369 | 2376 |