diff options
| author | Matthew Pickering <matthewtpickering@gmail.com> | 2021-12-13 21:13:09 +0000 |
|---|---|---|
| committer | Matthew Pickering <matthewtpickering@gmail.com> | 2021-12-30 16:27:49 +0000 |
| commit | 3311f4b23060e421af9aa059d69330cb85b95eec (patch) | |
| tree | a6022c436e019e50f1daf29c1bb893f1bff602cf | |
| parent | ec557f4fb9643d6d688fb570da0c79c1d88ed7c2 (diff) | |
| download | haskell-3311f4b23060e421af9aa059d69330cb85b95eec.tar.gz | |
Mark provendance of skolem info
| -rw-r--r-- | compiler/GHC/Runtime/Eval.hs | 2 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Deriv/Infer.hs | 8 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Errors.hs | 134 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Gen/HsType.hs | 33 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Gen/Pat.hs | 12 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Gen/Rule.hs | 9 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Gen/Sig.hs | 2 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Gen/Splice.hs | 6 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Solver.hs | 14 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Solver/Canonical.hs | 10 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Solver/Monad.hs | 8 | ||||
| -rw-r--r-- | compiler/GHC/Tc/TyCl.hs | 56 | ||||
| -rw-r--r-- | compiler/GHC/Tc/TyCl/Class.hs | 3 | ||||
| -rw-r--r-- | compiler/GHC/Tc/TyCl/Instance.hs | 17 | ||||
| -rw-r--r-- | compiler/GHC/Tc/TyCl/PatSyn.hs | 4 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Types/Constraint.hs | 6 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Types/Origin.hs | 38 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Types/Origin.hs-boot | 1 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Utils/Backpack.hs | 4 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Utils/TcMType.hs | 38 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Utils/Unify.hs | 20 | ||||
| -rw-r--r-- | compiler/GHC/Tc/Utils/Zonk.hs | 4 | ||||
| -rw-r--r-- | compiler/GHC/Types/Unique/Map.hs | 4 |
23 files changed, 243 insertions, 190 deletions
diff --git a/compiler/GHC/Runtime/Eval.hs b/compiler/GHC/Runtime/Eval.hs index 40cd6703a6..69a820041c 100644 --- a/compiler/GHC/Runtime/Eval.hs +++ b/compiler/GHC/Runtime/Eval.hs @@ -1059,7 +1059,7 @@ getDictionaryBindings :: PredType -> TcM CtEvidence getDictionaryBindings theta = do dictName <- newName (mkDictOcc (mkVarOcc "magic")) let dict_var = mkVanillaGlobal dictName theta - loc <- getCtLocM (GivenOrigin unkSkol) Nothing + loc <- getCtLocM (GivenOrigin (getSkolemInfo unkSkol)) Nothing -- Generate a wanted here because at the end of constraint -- solving, most derived constraints get thrown away, which in certain diff --git a/compiler/GHC/Tc/Deriv/Infer.hs b/compiler/GHC/Tc/Deriv/Infer.hs index 9185aced1b..db54c9bab4 100644 --- a/compiler/GHC/Tc/Deriv/Infer.hs +++ b/compiler/GHC/Tc/Deriv/Infer.hs @@ -718,14 +718,14 @@ simplifyDeriv :: PredType -- ^ @C inst_ty@, head of the instance we are -> TcM ThetaType -- ^ Needed constraints (after simplification), -- i.e. @['PredType']@. simplifyDeriv pred tvs thetas - = do { (skol_subst, tvs_skols) <- tcInstSkolTyVars (DerivSkol pred) tvs -- Skolemize + = do { skol_info <- mkSkolemInfo (DerivSkol pred) + ; (skol_subst, tvs_skols) <- tcInstSkolTyVars skol_info tvs -- Skolemize -- The constraint solving machinery -- expects *TcTyVars* not TyVars. -- We use *non-overlappable* (vanilla) skolems -- See Note [Overlap and deriving] ; let skol_set = mkVarSet tvs_skols - skol_info = DerivSkol pred doc = text "deriving" <+> parens (ppr pred) mk_given_ev :: PredType -> TcM EvVar @@ -766,7 +766,7 @@ simplifyDeriv pred tvs thetas = do { ac_given_evs <- mapM mk_given_ev ac_givens ; (_, wanteds) <- captureConstraints $ - checkConstraints skol_info ac_skols ac_given_evs $ + checkConstraints (getSkolemInfo skol_info) ac_skols ac_given_evs $ -- The checkConstraints bumps the TcLevel, and -- wraps the wanted constraints in an implication, -- when (but only when) necessary @@ -841,7 +841,7 @@ simplifyDeriv pred tvs thetas -- forall tvs. min_theta => solved_wanteds ; min_theta_vars <- mapM newEvVar min_theta ; (leftover_implic, _) - <- buildImplicationFor tc_lvl skol_info tvs_skols + <- buildImplicationFor tc_lvl (getSkolemInfo skol_info) tvs_skols min_theta_vars solved_wanteds -- This call to simplifyTop is purely for error reporting -- See Note [Error reporting for deriving clauses] diff --git a/compiler/GHC/Tc/Errors.hs b/compiler/GHC/Tc/Errors.hs index 8d5433efa3..2f36562d29 100644 --- a/compiler/GHC/Tc/Errors.hs +++ b/compiler/GHC/Tc/Errors.hs @@ -5,6 +5,8 @@ {-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} {-# OPTIONS_GHC -Wno-incomplete-record-updates #-} +{-# LANGUAGE TupleSections #-} +{-# LANGUAGE ParallelListComp #-} module GHC.Tc.Errors( reportUnsolved, reportAllUnsolved, warnAllUnsolved, @@ -48,6 +50,7 @@ import GHC.Types.SrcLoc import GHC.Types.Basic import GHC.Types.Error import GHC.Types.Unique.Set ( nonDetEltsUniqSet ) +import qualified GHC.Types.Unique.Map as UM import GHC.Rename.Unbound ( unknownNameSuggestions, WhatLooking(..) ) import GHC.Unit.Module @@ -86,10 +89,12 @@ import Data.Foldable ( toList ) import Data.Functor ( (<&>) ) import Data.Function ( on ) import Data.List ( groupBy, partition, mapAccumL - , sortBy, tails, unfoldr ) + , sortBy, tails, unfoldr, sort ) import Data.Ord ( comparing ) -- import Data.Semigroup ( Semigroup ) import qualified Data.Semigroup as Semigroup +import Data.Bifunctor +import GHC.Utils.Trace {- @@ -450,7 +455,7 @@ reportImplic ctxt implic@(Implic { ic_skols = tvs -- (see Note [Skolems in an implication] in GHC.Tc.Types.Constraint) -- but tidying goes wrong on out-of-order constraints; -- so we sort them here before tidying - info' = tidySkolemInfo env1 info + info' = tidySkolemInfoAnon env1 info implic' = implic { ic_skols = tvs' , ic_given = map (tidyEvVar env1) given , ic_info = info' } @@ -475,7 +480,7 @@ reportImplic ctxt implic@(Implic { ic_skols = tvs IC_BadTelescope -> True _ -> False -warnRedundantConstraints :: ReportErrCtxt -> TcLclEnv -> SkolemInfo -> [EvVar] -> TcM () +warnRedundantConstraints :: ReportErrCtxt -> TcLclEnv -> SkolemInfoAnon -> [EvVar] -> TcM () -- See Note [Tracking redundant constraints] in GHC.Tc.Solver warnRedundantConstraints ctxt env info ev_vars | null redundant_evs @@ -511,7 +516,7 @@ warnRedundantConstraints ctxt env info ev_vars improving pred -- (transSuperClasses p) does not include p = any isImprovementPred (pred : transSuperClasses pred) -reportBadTelescope :: ReportErrCtxt -> TcLclEnv -> SkolemInfo -> [TcTyVar] -> TcM () +reportBadTelescope :: ReportErrCtxt -> TcLclEnv -> SkolemInfoAnon -> [TcTyVar] -> TcM () reportBadTelescope ctxt env (ForAllSkol telescope) skols = do { msg <- mkErrorReport ErrorWithoutFlag ctxt env (important doc) ; reportDiagnostic msg } @@ -1300,8 +1305,8 @@ mkHoleError lcl_name_cache tidy_simples ctxt hole@(Hole { hole_occ = occ else return (ctxt, empty) - ; zonked_skol_tvs <- mapM zonkTyVarSkolemInfo skol_tvs - ; let err = important (hole_msg zonked_skol_tvs) `mappend` + ; skol_msg <- pprSkols ctxt =<< mapM zonkTyVarSkolemInfo skol_tvs + ; let err = important (hole_msg skol_msg) `mappend` mk_relevant_bindings (binds_msg $$ constraints_msg) `mappend` valid_hole_fits sub_msg @@ -1317,16 +1322,16 @@ mkHoleError lcl_name_cache tidy_simples ctxt hole@(Hole { hole_occ = occ hole_kind = tcTypeKind hole_ty tyvars = tyCoVarsOfTypeList hole_ty - hole_msg zonked_skol_tvs = case sort of + hole_msg skol_msg = case sort of ExprHole _ -> vcat [ hang (text "Found hole:") 2 (pp_occ_with_type occ hole_ty) - , tyvars_msg zonked_skol_tvs, expr_hole_hint ] + , tyvars_msg skol_msg, expr_hole_hint ] TypeHole -> vcat [ hang (text "Found type wildcard" <+> quotes (ppr occ)) 2 (text "standing for" <+> quotes pp_hole_type_with_kind) - , tyvars_msg zonked_skol_tvs, type_hole_hint ] + , tyvars_msg skol_msg, type_hole_hint ] ConstraintHole -> vcat [ hang (text "Found extra-constraints wildcard standing for") 2 (quotes $ pprType hole_ty) -- always kind constraint - , tyvars_msg zonked_skol_tvs, type_hole_hint ] + , tyvars_msg skol_msg, type_hole_hint ] pp_hole_type_with_kind | isLiftedTypeKind hole_kind @@ -1339,9 +1344,9 @@ mkHoleError lcl_name_cache tidy_simples ctxt hole@(Hole { hole_occ = occ is_skol tv = isTcTyVar tv && isSkolemTyVar tv (skol_tvs, other_tvs) = partition is_skol tyvars - tyvars_msg zonked_skol_tvs = ppUnless (null tyvars) $ + tyvars_msg skol_msg = ppUnless (null tyvars) $ text "Where:" <+> (vcat (map loc_msg other_tvs) - $$ pprSkols ctxt zonked_skol_tvs) + $$ skol_msg) where -- Coercion variables can be free in the -- hole, via kind casts @@ -1650,29 +1655,26 @@ mkTyVarEqErr :: ReportErrCtxt -> Report -> Ct mkTyVarEqErr ctxt report ct tv1 ty2 = do { traceTc "mkTyVarEqErr" (ppr ct $$ ppr tv1 $$ ppr ty2) ; dflags <- getDynFlags - ; zonked_skol_tv <- zonkTyVarSkolemInfo tv1 - ; zonked_ty2 <- case tcGetCastedTyVar_maybe ty2 of - Just (tv, co) -> flip mkCastTy co . mkTyVarTy <$> zonkTyVarSkolemInfo tv - Nothing -> pure ty2 - ; return $ mkTyVarEqErr' dflags ctxt report ct zonked_skol_tv zonked_ty2 } + ; mkTyVarEqErr' dflags ctxt report ct tv1 ty2 } mkTyVarEqErr' :: DynFlags -> ReportErrCtxt -> Report -> Ct - -> TcTyVar -> TcType -> Report + -> TcTyVar -> TcType -> TcM Report mkTyVarEqErr' dflags ctxt report ct tv1 ty2 -- impredicativity is a simple error to understand; try it first | check_eq_result `cterHasProblem` cteImpredicative - = let msg = vcat [ (if isSkolemTyVar tv1 + = do + let msg = vcat [ (if isSkolemTyVar tv1 then text "Cannot equate type variable" else text "Cannot instantiate unification variable") <+> quotes (ppr tv1) , hang (text "with a" <+> what <+> text "involving polytypes:") 2 (ppr ty2) ] - in -- Unlike the other reports, this discards the old 'report_important' -- instead of augmenting it. This is because the details are not likely -- to be helpful since this is just an unimplemented feature. - mconcat [ headline_msg + extra_info <- extraTyVarEqInfo ctxt tv1 ty2 + return $ mconcat [ headline_msg , important msg - , if isSkolemTyVar tv1 then extraTyVarEqInfo ctxt tv1 ty2 else mempty + , if isSkolemTyVar tv1 then extra_info else mempty , report ] | isSkolemTyVar tv1 -- ty2 won't be a meta-tyvar; we would have @@ -1680,8 +1682,10 @@ mkTyVarEqErr' dflags ctxt report ct tv1 ty2 || isTyVarTyVar tv1 && not (isTyVarTy ty2) || ctEqRel ct == ReprEq -- The cases below don't really apply to ReprEq (except occurs check) - = mconcat [ headline_msg - , extraTyVarEqInfo ctxt tv1 ty2 + = do + extra_info <- extraTyVarEqInfo ctxt tv1 ty2 + return $ mconcat [ headline_msg + , extra_info , suggestAddSig ctxt ty1 ty2 , report ] @@ -1704,7 +1708,7 @@ mkTyVarEqErr' dflags ctxt report ct tv1 ty2 tyvar_binding tv = ppr tv <+> dcolon <+> ppr (tyVarKind tv) in - mconcat [headline_msg, extra2, extra3, report] + return $ mconcat [headline_msg, extra2, extra3, report] -- If the immediately-enclosing implication has 'tv' a skolem, and -- we know by now its an InferSkol kind of skolem, then presumably @@ -1713,10 +1717,15 @@ mkTyVarEqErr' dflags ctxt report ct tv1 ty2 | (implic:_) <- cec_encl ctxt , Implic { ic_skols = skols } <- implic , tv1 `elem` skols - = mconcat [ misMatchMsg ctxt ct ty1 ty2 - , extraTyVarEqInfo ctxt tv1 ty2 - , report - ] + = do + extra_info <- extraTyVarEqInfo ctxt tv1 ty2 + + return $ + + mconcat [ misMatchMsg ctxt ct ty1 ty2 + , extra_info + , report + ] -- Check for skolem escape | (implic:_) <- cec_encl ctxt -- Get the innermost context @@ -1741,7 +1750,7 @@ mkTyVarEqErr' dflags ctxt report ct tv1 ty2 , nest 2 $ text "at" <+> ppr (tcl_loc (ic_env implic)) ] ] in - mconcat [msg, tv_extra, report] + return $ mconcat [msg, tv_extra, report] -- Nastiest case: attempt to unify an untouchable variable -- So tv is a meta tyvar (or started that way before we @@ -1751,7 +1760,7 @@ mkTyVarEqErr' dflags ctxt report ct tv1 ty2 | (implic:_) <- cec_encl ctxt -- Get the innermost context , Implic { ic_given = given, ic_tclvl = lvl, ic_info = skol_info } <- implic = assertPpr (not (isTouchableMetaTyVar lvl tv1)) - (ppr tv1 $$ ppr lvl) $ -- See Note [Error messages for untouchables] + (ppr tv1 $$ ppr lvl) $ do -- See Note [Error messages for untouchables] let msg = misMatchMsg ctxt ct ty1 ty2 tclvl_extra = important $ nest 2 $ @@ -1760,13 +1769,12 @@ mkTyVarEqErr' dflags ctxt report ct tv1 ty2 , nest 2 $ text "bound by" <+> ppr skol_info , nest 2 $ text "at" <+> ppr (tcl_loc (ic_env implic)) ] - tv_extra = extraTyVarEqInfo ctxt tv1 ty2 add_sig = suggestAddSig ctxt ty1 ty2 - in - mconcat [msg, tclvl_extra, tv_extra, add_sig, report] + tv_extra <- extraTyVarEqInfo ctxt tv1 ty2 + return $ mconcat [msg, tclvl_extra, tv_extra, add_sig, report] | otherwise - = reportEqErr ctxt report ct (mkTyVarTy tv1) ty2 + = return $ reportEqErr ctxt report ct (mkTyVarTy tv1) ty2 -- This *can* happen (#6123) -- Consider an ambiguous top-level constraint (a ~ F a) -- Not an occurs check, because F is a type function. @@ -1918,23 +1926,23 @@ addition to superclasses (see Note [Remove redundant provided dicts] in GHC.Tc.TyCl.PatSyn). -} -extraTyVarEqInfo :: HasCallStack => ReportErrCtxt -> TcTyVar -> TcType -> Report +extraTyVarEqInfo :: HasCallStack => ReportErrCtxt -> TcTyVar -> TcType -> TcM Report -- Add on extra info about skolem constants -- NB: The types themselves are already tidied extraTyVarEqInfo ctxt tv1 ty2 - = important (extraTyVarInfo ctxt tv1 $$ ty_extra ty2) + = important <$> (($$) <$> extraTyVarInfo ctxt tv1 <*> ty_extra ty2) where ty_extra ty = case tcGetCastedTyVar_maybe ty of Just (tv, _) -> extraTyVarInfo ctxt tv - Nothing -> empty + Nothing -> return empty -extraTyVarInfo :: HasCallStack => ReportErrCtxt -> TcTyVar -> SDoc +extraTyVarInfo :: HasCallStack => ReportErrCtxt -> TcTyVar -> TcM SDoc extraTyVarInfo ctxt tv = assertPpr (isTyVar tv) (ppr tv) $ case tcTyVarDetails tv of SkolemTv {} -> pprSkols ctxt [tv] - RuntimeUnk {} -> quotes (ppr tv) <+> text "is an interactive-debugger skolem" - MetaTv {} -> empty + RuntimeUnk {} -> return $ quotes (ppr tv) <+> text "is an interactive-debugger skolem" + MetaTv {} -> return empty suggestAddSig :: ReportErrCtxt -> TcType -> TcType -> Report -- See Note [Suggest adding a type signature] @@ -2785,7 +2793,7 @@ discardProvCtxtGivens orig givens -- See Note [discardProvCtxtGivens] discard n (Implic { ic_info = SigSkol (PatSynCtxt n') _ _ }) = n == n' discard _ _ = False -usefulContext :: [Implication] -> PredType -> [SkolemInfo] +usefulContext :: [Implication] -> PredType -> [SkolemInfoAnon] -- usefulContext picks out the implications whose context -- the programmer might plausibly augment to solve 'pred' usefulContext implics pred @@ -3156,10 +3164,21 @@ mkAmbigMsg prepend_msg ct = text "The" <+> what <+> text "variable" <> plural tkvs <+> pprQuotedList tkvs <+> isOrAre tkvs <+> text "ambiguous" -pprSkols :: HasCallStack => ReportErrCtxt -> [TcTyVar] -> SDoc +pprSkols :: HasCallStack => ReportErrCtxt -> [TcTyVar] -> TcM SDoc pprSkols ctxt tvs - = vcat (map pp_one (getSkolemInfo (cec_encl ctxt) tvs)) + = do + pprTraceM "ty_vars" (ppr tvs) + pprTraceM "ty_vars" (ppr skolem_list) + zonked_ty_vars <- mapM (firstM zonkSkolemInfo) skolem_list + let tidy_ty_vars = map (bimap (getSkolemInfo . tidySkolemInfo (cec_tidy ctxt)) (map fst)) zonked_ty_vars + return $ vcat (map pp_one tidy_ty_vars) where + group_skolems :: UM.UniqMap SkolemInfo ([(TcTyVar, Int)]) + group_skolems = bagToList <$> UM.listToUniqMap_C unionBags [(skolemSkolInfo tv, unitBag (tv, n)) | tv <- tvs | n <- [0..]] + + skolem_list = sortBy (comparing (sort . map snd . snd . pprTraceIt "sort")) (UM.nonDetEltsUniqMap group_skolems) + + pp_one (UnkSkol cs, tvs) = vcat [ hang (pprQuotedList tvs) 2 (is_or_are tvs "a" "(rigid, skolem)") @@ -3189,33 +3208,6 @@ getAmbigTkvs ct ambig_tkvs = filter isAmbiguousTyVar tkvs dep_tkv_set = tyCoVarsOfTypes (map tyVarKind tkvs) -getSkolemInfo :: [Implication] -> [TcTyVar] - -> [(SkolemInfo, [TcTyVar])] -- #14628 --- Get the skolem info for some type variables --- from the implication constraints that bind them. --- --- In the returned (skolem, tvs) pairs, the 'tvs' part is non-empty -getSkolemInfo _ [] - = [] -getSkolemInfo _ tvs = - map (\tv -> (skolemSkolInfo tv, [tv])) tvs - {- -getSkolemInfo [] tvs - | all isRuntimeUnkSkol tvs = [(RuntimeUnkSkol, tvs)] -- #14628 - | otherwise = -- See https://gitlab.haskell.org/ghc/ghc/-/issues?label_name[]=No%20skolem%20info - pprTraceUserWarning msg [(UnkSkol,tvs)] - where - msg = text "No skolem info - we could not find the origin of the following variables" <+> ppr tvs - $$ text "This should not happen, please report it as a bug following the instructions at:" - $$ text "https://gitlab.haskell.org/ghc/ghc/wikis/report-a-bug" - -getSkolemInfo (implic:implics) tvs - | null tvs_here = getSkolemInfo implics tvs - | otherwise = (ic_info implic, tvs_here) : getSkolemInfo implics tvs_other - where - (tvs_here, tvs_other) = partition (`elem` ic_skols implic) tvs - -} - ----------------------- -- relevantBindings looks at the value environment and finds values whose -- types mention any of the offending type variables. It has to be diff --git a/compiler/GHC/Tc/Gen/HsType.hs b/compiler/GHC/Tc/Gen/HsType.hs index 6590b017d6..2ca15d2e5b 100644 --- a/compiler/GHC/Tc/Gen/HsType.hs +++ b/compiler/GHC/Tc/Gen/HsType.hs @@ -386,7 +386,9 @@ kcClassSigType :: [LocatedN Name] -> LHsSigType GhcRn -> TcM () kcClassSigType names sig_ty@(L _ (HsSig { sig_bndrs = hs_outer_bndrs, sig_body = hs_ty })) = addSigCtxt (funsSigCtxt names) sig_ty $ - do { _ <- bindOuterSigTKBndrs_Tv (SigTypeSkol (funsSigCtxt names)) hs_outer_bndrs $ + --MP: I suspect this is wrong, like the other kind checking cases. + do { skol_info <- mkSkolemInfo ((SigTypeSkol (funsSigCtxt names))) + ; _ <- bindOuterSigTKBndrs_Tv skol_info hs_outer_bndrs $ tcLHsType hs_ty liftedTypeKind ; return () } @@ -394,7 +396,8 @@ tcClassSigType :: [LocatedN Name] -> LHsSigType GhcRn -> TcM Type -- Does not do validity checking tcClassSigType names sig_ty = addSigCtxt sig_ctxt sig_ty $ - do { (implic, ty) <- tc_lhs_sig_type skol_info sig_ty (TheKind liftedTypeKind) + do { skol_info <- mkSkolemInfo skol_info_anon + ; (implic, ty) <- tc_lhs_sig_type skol_info sig_ty (TheKind liftedTypeKind) ; emitImplication implic ; return ty } -- Do not zonk-to-Type, nor perform a validity check @@ -415,7 +418,7 @@ tcClassSigType names sig_ty -- painfully delicate. where sig_ctxt = funsSigCtxt names - skol_info = SigTypeSkol sig_ctxt + skol_info_anon = SigTypeSkol sig_ctxt tcHsSigType :: UserTypeCtxt -> LHsSigType GhcRn -> TcM Type -- Does validity checking @@ -423,7 +426,7 @@ tcHsSigType :: UserTypeCtxt -> LHsSigType GhcRn -> TcM Type tcHsSigType ctxt sig_ty = addSigCtxt ctxt sig_ty $ do { traceTc "tcHsSigType {" (ppr sig_ty) - + ; skol_info <- mkSkolemInfo skol_info -- Generalise here: see Note [Kind generalisation] ; (implic, ty) <- tc_lhs_sig_type skol_info sig_ty (expectedKindInCtxt ctxt) @@ -592,6 +595,7 @@ tc_top_lhs_type tyki ctxt (L loc sig_ty@(HsSig { sig_bndrs = hs_outer_bndrs , sig_body = body })) = setSrcSpanA loc $ do { traceTc "tc_top_lhs_type {" (ppr sig_ty) + ; skol_info <- mkSkolemInfo skol_info_anon ; (tclvl, wanted, (outer_bndrs, ty)) <- pushLevelAndSolveEqualitiesX "tc_top_lhs_type" $ tcOuterTKBndrs skol_info hs_outer_bndrs $ @@ -609,7 +613,7 @@ tc_top_lhs_type tyki ctxt (L loc sig_ty@(HsSig { sig_bndrs = hs_outer_bndrs ; traceTc "tc_top_lhs_type }" (vcat [ppr sig_ty, ppr final_ty]) ; return final_ty } where - skol_info = SigTypeSkol ctxt + skol_info_anon = SigTypeSkol ctxt ----------------- tcHsDeriv :: LHsSigType GhcRn -> TcM ([TyVar], Class, [Type], [Kind]) @@ -2377,7 +2381,8 @@ kcCheckDeclHeader_cusk name flav -- CUSK case -- See note [Required, Specified, and Inferred for types] in GHC.Tc.TyCl = addTyConFlavCtxt name flav $ - do { (tclvl, wanted, (scoped_kvs, (tc_tvs, res_kind))) + do { skol_info <- mkSkolemInfo skol_info_anon + ; (tclvl, wanted, (scoped_kvs, (tc_tvs, res_kind))) <- pushLevelAndSolveEqualitiesX "kcCheckDeclHeader_cusk" $ bindImplicitTKBndrs_Q_Skol skol_info kv_ns $ bindExplicitTKBndrs_Q_Skol skol_info ctxt_kind hs_tvs $ @@ -2442,7 +2447,7 @@ kcCheckDeclHeader_cusk name flav ; return tycon } where - skol_info = TyConSkol callStack flav name + skol_info_anon = TyConSkol callStack flav name ctxt_kind | tcFlavourIsOpen flav = TheKind liftedTypeKind | otherwise = AnyKind @@ -2462,7 +2467,7 @@ kcInferDeclHeader name flav -- No standalane kind signature and no CUSK. -- See note [Required, Specified, and Inferred for types] in GHC.Tc.TyCl = addTyConFlavCtxt name flav $ - do { let skol_info = TyConSkol callStack flav name + do { skol_info <- mkSkolemInfo (TyConSkol callStack flav name) ; (scoped_kvs, (tc_tvs, res_kind)) -- Why bindImplicitTKBndrs_Q_Tv which uses newTyVarTyVar? -- See Note [Inferring kinds for type declarations] in GHC.Tc.TyCl @@ -2545,7 +2550,7 @@ kcCheckDeclHeader_sig kisig name flav -- Convert each ZippedBinder to TyConBinder for tyConBinders -- and to [(Name, TcTyVar)] for tcTyConScopedTyVars ; (vis_tcbs, concat -> explicit_tv_prs) <- mapAndUnzipM zipped_to_tcb zipped_binders - ; let skol_info = TyConSkol callStack flav name + ; skol_info <- mkSkolemInfo (TyConSkol callStack flav name) ; (tclvl, wanted, (implicit_tvs, (invis_binders, r_ki))) <- pushLevelAndSolveEqualitiesX "kcCheckDeclHeader_sig" $ -- #16687 @@ -3177,7 +3182,7 @@ tcExplicitTKBndrsX :: OutputableBndrFlag flag 'Renamed -- and emit an implication constraint with a ForAllSkol ic_info, -- so that it is subject to a telescope test. tcExplicitTKBndrsX skol_mode bndrs thing_inside - = do { ; let skol_info = ForAllSkol (fsep (map ppr bndrs)) + = do { skol_info <- mkSkolemInfo (ForAllSkol (fsep (map ppr bndrs))) ; (tclvl, wanted, (skol_tvs, res)) <- pushLevelAndCaptureConstraints $ bindExplicitTKBndrsX skol_info skol_mode bndrs $ @@ -3469,6 +3474,7 @@ bindTyClTyVars skol_info tycon_name thing_inside binders = tyConBinders tycon ; traceTc "bindTyClTyVars" (ppr tycon_name <+> ppr binders $$ ppr scoped_prs) ; let scoped_prs' = map skolemise_pair scoped_prs + ; traceTc "bindTyClTyVars" (ppr tycon_name <+> ppr binders $$ ppr scoped_prs') ; tcExtendNameTyVarEnv scoped_prs' $ thing_inside tycon binders res_kind } where @@ -3934,11 +3940,12 @@ tcHsPartialSigType ctxt sig_ty , (hs_ctxt, hs_tau) <- splitLHsQualTy body_ty = addSigCtxt ctxt sig_ty $ do { mode <- mkHoleMode TypeLevel HM_Sig + ; skol_info <- mkSkolemInfo (SigTypeSkol ctxt) ; (outer_bndrs, (wcs, wcx, theta, tau)) <- solveEqualities "tcHsPartialSigType" $ -- See Note [Failure in local type signatures] bindNamedWildCardBinders sig_wcs $ \ wcs -> - bindOuterSigTKBndrs_Tv_M (SigTypeSkol ctxt) mode hs_outer_bndrs $ + bindOuterSigTKBndrs_Tv_M skol_info mode hs_outer_bndrs $ do { -- Instantiate the type-class context; but if there -- is an extra-constraints wildcard, just discard it here (theta, wcx) <- tcPartialContext mode hs_ctxt @@ -4181,7 +4188,9 @@ tcHsPatSigType ctxt hole_mode new_implicit_tv name = do { kind <- newMetaKindVar ; tv <- case ctxt of - RuleSigCtxt rname _ -> newSkolemTyVar (RuleSkol rname) name kind + RuleSigCtxt rname _ -> do + skol_info <- mkSkolemInfo (RuleSkol rname) + newSkolemTyVar skol_info name kind _ -> newPatSigTyVar name kind -- See Note [Typechecking pattern signature binders] -- NB: tv's Name may be fresh (in the case of newPatSigTyVar) diff --git a/compiler/GHC/Tc/Gen/Pat.hs b/compiler/GHC/Tc/Gen/Pat.hs index f96baf671b..a12573ef65 100644 --- a/compiler/GHC/Tc/Gen/Pat.hs +++ b/compiler/GHC/Tc/Gen/Pat.hs @@ -915,7 +915,7 @@ tcDataConPat penv (L con_span con_name) data_con pat_ty_scaled ; let mc = case pe_ctxt penv of LamPat mc -> mc LetPat {} -> PatBindRhs - ; let skol_info = PatSkol (RealDataCon data_con) mc + ; skol_info <- mkSkolemInfo (PatSkol (RealDataCon data_con) mc) ; (tenv, ex_tvs') <- tcInstSuperSkolTyVarsX skol_info tenv1 ex_tvs -- Get location from monad, not from ex_tvs -- This freshens: See Note [Freshen existentials] @@ -962,7 +962,7 @@ tcDataConPat penv (L con_span con_name) data_con pat_ty_scaled ; given <- newEvVars theta' ; (ev_binds, (arg_pats', res)) - <- checkConstraints skol_info ex_tvs' given $ + <- checkConstraints (getSkolemInfo skol_info) ex_tvs' given $ tcConArgs (RealDataCon data_con) arg_tys_scaled tenv penv arg_pats thing_inside ; let res_pat = ConPat @@ -993,9 +993,9 @@ tcPatSynPat penv (L con_span con_name) pat_syn pat_ty arg_pats thing_inside ; let all_arg_tys = ty : prov_theta ++ (map scaledThing arg_tys) ; checkGADT (PatSynCon pat_syn) ex_tvs all_arg_tys penv - ; let skol_info = case pe_ctxt penv of - LamPat mc -> PatSkol (PatSynCon pat_syn) mc - LetPat {} -> unkSkol -- Doesn't matter + ; skol_info <- case pe_ctxt penv of + LamPat mc -> mkSkolemInfo (PatSkol (PatSynCon pat_syn) mc) + LetPat {} -> return unkSkol -- Doesn't matter ; (tenv, ex_tvs') <- tcInstSuperSkolTyVarsX skol_info subst ex_tvs -- This freshens: Note [Freshen existentials] @@ -1031,7 +1031,7 @@ tcPatSynPat penv (L con_span con_name) pat_syn pat_ty arg_pats thing_inside ; traceTc "checkConstraints {" Outputable.empty ; (ev_binds, (arg_pats', res)) - <- checkConstraints skol_info ex_tvs' prov_dicts' $ + <- checkConstraints (getSkolemInfo skol_info) ex_tvs' prov_dicts' $ tcConArgs (PatSynCon pat_syn) arg_tys_scaled tenv penv arg_pats thing_inside ; traceTc "checkConstraints }" (ppr ev_binds) diff --git a/compiler/GHC/Tc/Gen/Rule.hs b/compiler/GHC/Tc/Gen/Rule.hs index 9cbc8f721b..2b7f724965 100644 --- a/compiler/GHC/Tc/Gen/Rule.hs +++ b/compiler/GHC/Tc/Gen/Rule.hs @@ -119,7 +119,7 @@ tcRule (HsRule { rd_ext = ext , rd_rhs = rhs }) = addErrCtxt (ruleCtxt name) $ do { traceTc "---- Rule ------" (pprFullRuleName rname) - ; let skol_info = RuleSkol name + ; skol_info <- mkSkolemInfo (RuleSkol name) -- Note [Typechecking rules] ; (tc_lvl, stuff) <- pushTcLevelM $ generateRuleConstraints name ty_bndrs tm_bndrs lhs rhs @@ -164,9 +164,9 @@ tcRule (HsRule { rd_ext = ext -- For the LHS constraints we must solve the remaining constraints -- (a) so that we report insoluble ones -- (b) so that we bind any soluble ones - ; (lhs_implic, lhs_binds) <- buildImplicationFor tc_lvl skol_info qtkvs + ; (lhs_implic, lhs_binds) <- buildImplicationFor tc_lvl (getSkolemInfo skol_info) qtkvs lhs_evs residual_lhs_wanted - ; (rhs_implic, rhs_binds) <- buildImplicationFor tc_lvl skol_info qtkvs + ; (rhs_implic, rhs_binds) <- buildImplicationFor tc_lvl (getSkolemInfo skol_info) qtkvs lhs_evs rhs_wanted ; emitImplications (lhs_implic `unionBags` rhs_implic) @@ -207,7 +207,8 @@ generateRuleConstraints rule_name ty_bndrs tm_bndrs lhs rhs tcRuleBndrs :: FastString -> Maybe [LHsTyVarBndr () GhcRn] -> [LRuleBndr GhcRn] -> TcM ([TcTyVar], [Id]) tcRuleBndrs rule_name (Just bndrs) xs - = do { (tybndrs1,(tys2,tms)) <- bindExplicitTKBndrs_Skol (RuleSkol rule_name) bndrs $ + = do { skol_info <- mkSkolemInfo (RuleSkol rule_name) + ; (tybndrs1,(tys2,tms)) <- bindExplicitTKBndrs_Skol skol_info bndrs $ tcRuleTmBndrs rule_name xs ; let tys1 = binderVars tybndrs1 ; return (tys1 ++ tys2, tms) } diff --git a/compiler/GHC/Tc/Gen/Sig.hs b/compiler/GHC/Tc/Gen/Sig.hs index 01bf4ef4d2..3413d3e9b6 100644 --- a/compiler/GHC/Tc/Gen/Sig.hs +++ b/compiler/GHC/Tc/Gen/Sig.hs @@ -406,7 +406,7 @@ tcPatSynSig name sig_ty@(L _ (HsSig{sig_bndrs = hs_outer_bndrs, sig_body = hs_ty , (ex_hs_tvbndrs, hs_prov, hs_body_ty) <- splitLHsSigmaTyInvis hs_ty1 = do { traceTc "tcPatSynSig 1" (ppr sig_ty) - ; let skol_info = DataConSkol name + ; skol_info <- mkSkolemInfo (DataConSkol name) ; (tclvl, wanted, (outer_bndrs, (ex_bndrs, (req, prov, body_ty)))) <- pushLevelAndSolveEqualitiesX "tcPatSynSig" $ -- See Note [solveEqualities in tcPatSynSig] diff --git a/compiler/GHC/Tc/Gen/Splice.hs b/compiler/GHC/Tc/Gen/Splice.hs index 6a6f0b147a..ccb2a7f4ba 100644 --- a/compiler/GHC/Tc/Gen/Splice.hs +++ b/compiler/GHC/Tc/Gen/Splice.hs @@ -1697,16 +1697,16 @@ reifyInstances' th_nm th_tys rnImplicitTvOccs Nothing tv_rdrs $ \ tv_names -> do { (rn_ty, fvs) <- rnLHsType doc rdr_ty ; return ((tv_names, rn_ty), fvs) } - + ; skol_info <- mkSkolemInfo ReifySkol ; (tclvl, wanted, (tvs, ty)) <- pushLevelAndSolveEqualitiesX "reifyInstances" $ - bindImplicitTKBndrs_Skol ReifySkol tv_names $ + bindImplicitTKBndrs_Skol skol_info tv_names $ tcInferLHsType rn_ty ; tvs <- zonkAndScopedSort tvs -- Avoid error cascade if there are unsolved - ; reportUnsolvedEqualities ReifySkol tvs tclvl wanted + ; reportUnsolvedEqualities skol_info tvs tclvl wanted ; ty <- zonkTcTypeToType ty -- Substitute out the meta type variables diff --git a/compiler/GHC/Tc/Solver.hs b/compiler/GHC/Tc/Solver.hs index b3ded35ce2..d82111ba28 100644 --- a/compiler/GHC/Tc/Solver.hs +++ b/compiler/GHC/Tc/Solver.hs @@ -903,7 +903,7 @@ tcCheckGivens inerts given_ids = do (sat, new_inerts) <- runTcSInerts inerts $ do traceTcS "checkGivens {" (ppr inerts <+> ppr given_ids) lcl_env <- TcS.getLclEnv - let given_loc = mkGivenLoc topTcLevel unkSkol lcl_env + let given_loc = mkGivenLoc topTcLevel (getSkolemInfo unkSkol) lcl_env let given_cts = mkGivens given_loc (bagToList given_ids) -- See Note [Superclasses and satisfiability] solveSimpleGivens given_cts @@ -1053,7 +1053,7 @@ simplifyInfer rhs_tclvl infer_mode sigs name_taus wanteds ; dep_vars <- candidateQTyVarsOfTypes (psig_tv_tys ++ psig_theta ++ map snd name_taus) - ; let skol_info = InferSkol name_taus + ; skol_info <- mkSkolemInfo (InferSkol name_taus) ; qtkvs <- quantifyTyVars skol_info DefaultNonStandardTyVars dep_vars ; traceTc "simplifyInfer: empty WC" (ppr name_taus $$ ppr qtkvs) ; return (qtkvs, [], emptyTcEvBinds, False) } @@ -1112,8 +1112,8 @@ simplifyInfer rhs_tclvl infer_mode sigs name_taus wanteds ; bound_theta_vars <- mapM TcM.newEvVar bound_theta ; let full_theta = map idType bound_theta_vars - ; let skol_info = InferSkol [ (name, mkSigmaTy [] full_theta ty) - | (name, ty) <- name_taus ] + ; skol_info <- mkSkolemInfo (InferSkol [ (name, mkSigmaTy [] full_theta ty) + | (name, ty) <- name_taus ]) } @@ -1190,7 +1190,7 @@ findInferredDiff annotated_theta inferred_theta do { lcl_env <- TcM.getLclEnv ; given_ids <- mapM TcM.newEvVar annotated_theta ; wanteds <- newWanteds AnnOrigin inferred_theta - ; let given_loc = mkGivenLoc topTcLevel unkSkol lcl_env + ; let given_loc = mkGivenLoc topTcLevel (getSkolemInfo unkSkol) lcl_env given_cts = mkGivens given_loc given_ids ; residual <- runTcSDeriveds $ @@ -2140,7 +2140,7 @@ checkBadTelescope (Implic { ic_info = info | otherwise = go (later_skols `extendVarSet` one_skol) earlier_skols -warnRedundantGivens :: SkolemInfo -> Bool +warnRedundantGivens :: SkolemInfoAnon -> Bool warnRedundantGivens (SigSkol ctxt _ _) = case ctxt of FunSigCtxt _ rrc -> reportRedundantConstraints rrc @@ -2783,7 +2783,7 @@ disambigGroup (default_ty:default_tys) group@(the_tv, wanteds) | Just subst <- mb_subst = do { lcl_env <- TcS.getLclEnv ; tc_lvl <- TcS.getTcLevel - ; let loc = mkGivenLoc tc_lvl unkSkol lcl_env + ; let loc = mkGivenLoc tc_lvl (getSkolemInfo unkSkol) lcl_env -- Equality constraints are possible due to type defaulting plugins ; wanted_evs <- mapM (newWantedNC loc . substTy subst . ctPred) wanteds diff --git a/compiler/GHC/Tc/Solver/Canonical.hs b/compiler/GHC/Tc/Solver/Canonical.hs index c0e177b9aa..79a5abe19b 100644 --- a/compiler/GHC/Tc/Solver/Canonical.hs +++ b/compiler/GHC/Tc/Solver/Canonical.hs @@ -871,8 +871,8 @@ solveForAll :: CtEvidence -> [TyVar] -> TcThetaType -> PredType -> Bool solveForAll ev tvs theta pred pend_sc | CtWanted { ctev_dest = dest } <- ev = -- See Note [Solving a Wanted forall-constraint] - do { let skol_info = QuantCtxtSkol - empty_subst = mkEmptyTCvSubst $ mkInScopeSet $ + do { skol_info <- mkSkolemInfo QuantCtxtSkol + ; let empty_subst = mkEmptyTCvSubst $ mkInScopeSet $ tyCoVarsOfTypes (pred:theta) `delVarSetList` tvs ; (subst, skol_tvs) <- tcInstSkolTyVarsX skol_info empty_subst tvs ; given_ev_vars <- mapM newEvVar (substTheta subst theta) @@ -884,7 +884,7 @@ solveForAll ev tvs theta pred pend_sc ; return ( ctEvEvId wanted_ev , unitBag (mkNonCanonical wanted_ev)) } - ; ev_binds <- emitImplicationTcS lvl skol_info skol_tvs + ; ev_binds <- emitImplicationTcS lvl (getSkolemInfo skol_info) skol_tvs given_ev_vars wanteds ; setWantedEvTerm dest $ @@ -1348,7 +1348,7 @@ can_eq_nc_forall ev eq_rel s1 s2 else do { traceTcS "Creating implication for polytype equality" $ ppr ev ; let empty_subst1 = mkEmptyTCvSubst $ mkInScopeSet free_tvs - ; let skol_info = UnifyForAllSkol phi1 + ; skol_info <- mkSkolemInfo (UnifyForAllSkol phi1) ; (subst1, skol_tvs) <- tcInstSkolTyVarsX skol_info empty_subst1 $ binderVars bndrs1 @@ -1380,7 +1380,7 @@ can_eq_nc_forall ev eq_rel s1 s2 ; (lvl, (all_co, wanteds)) <- pushLevelNoWorkList (ppr skol_info) $ go skol_tvs empty_subst2 bndrs2 - ; emitTvImplicationTcS lvl skol_info skol_tvs wanteds + ; emitTvImplicationTcS lvl (getSkolemInfo skol_info) skol_tvs wanteds ; setWantedEq orig_dest all_co ; stopWith ev "Deferred polytype equality" } } diff --git a/compiler/GHC/Tc/Solver/Monad.hs b/compiler/GHC/Tc/Solver/Monad.hs index df493076e9..fbd799b42d 100644 --- a/compiler/GHC/Tc/Solver/Monad.hs +++ b/compiler/GHC/Tc/Solver/Monad.hs @@ -1212,6 +1212,7 @@ data TcSEnv --------------- newtype TcS a = TcS { unTcS :: TcSEnv -> TcM a } deriving (Functor) + -- | Smart constructor for 'TcS', as describe in Note [The one-shot state -- monad trick] in "GHC.Utils.Monad". mkTcS :: (TcSEnv -> TcM a) -> TcS a @@ -1225,6 +1226,9 @@ instance Monad TcS where m >>= k = mkTcS $ \ebs -> do unTcS m ebs >>= (\r -> unTcS (k r) ebs) +instance MonadIO TcS where + liftIO act = TcS $ \_env -> liftIO act + instance MonadFail TcS where fail err = mkTcS $ \_ -> fail err @@ -1497,7 +1501,7 @@ nestTcS (TcS thing_inside) ; return res } -emitImplicationTcS :: TcLevel -> SkolemInfo +emitImplicationTcS :: TcLevel -> SkolemInfoAnon -> [TcTyVar] -- Skolems -> [EvVar] -- Givens -> Cts -- Wanteds @@ -1518,7 +1522,7 @@ emitImplicationTcS new_tclvl skol_info skol_tvs givens wanteds ; emitImplication imp ; return (TcEvBinds (ic_binds imp)) } -emitTvImplicationTcS :: TcLevel -> SkolemInfo +emitTvImplicationTcS :: TcLevel -> SkolemInfoAnon -> [TcTyVar] -- Skolems -> Cts -- Wanteds -> TcS () diff --git a/compiler/GHC/Tc/TyCl.hs b/compiler/GHC/Tc/TyCl.hs index cf21ada2b6..201d22d04f 100644 --- a/compiler/GHC/Tc/TyCl.hs +++ b/compiler/GHC/Tc/TyCl.hs @@ -1577,11 +1577,15 @@ kcTyClDecl :: TyClDecl GhcRn -> TcTyCon -> TcM () -- result kind signature have already been dealt with -- by inferInitialKind, so we can ignore them here. +-- NB these equations just extend the type environment with carefully constructed +-- TcTyVars rather than create skolemised variables for the bound variables. +-- - inferInitialKinds makes the TcTyCon where the tyvars are TcTyVars +-- - In this function, those TcTyVars are unified with other kind variables during +-- - kind inference (see [How TcTyCons work]) + kcTyClDecl (DataDecl { tcdLName = (L _ _name), tcdDataDefn = defn }) tycon | HsDataDefn { dd_ctxt = ctxt, dd_cons = cons, dd_ND = new_or_data } <- defn - = --bindTyClTyVars (TyConSkol callStack DataTypeFlavour name) name $ \ _ binders _ -> - - tcExtendNameTyVarEnv (tcTyConScopedTyVars tycon) $ + = tcExtendNameTyVarEnv (tcTyConScopedTyVars tycon) $ -- NB: binding these tyvars isn't necessary for GADTs, but it does no -- harm. For GADTs, each data con brings its own tyvars into scope, -- and the ones from this bindTyClTyVars are either not mentioned or @@ -1684,7 +1688,9 @@ kcConDecl new_or_data = -- See Note [kcConDecls: kind-checking data type decls] addErrCtxt (dataConCtxt names) $ discardResult $ - bindOuterSigTKBndrs_Tv (DataConSkol (unLoc (head names))) outer_bndrs $ + mkSkolemInfo (DataConSkol (unLoc (head names))) >>= \skol_info -> + -- Not sure this is right, should just extend rather than skolemise but no test + bindOuterSigTKBndrs_Tv skol_info outer_bndrs $ -- Why "_Tv"? See Note [Using TyVarTvs for kind-checking GADTs] do { _ <- tcHsContext cxt ; traceTc "kcConDecl:GADT {" (ppr names $$ ppr res_ty) @@ -2426,6 +2432,7 @@ tcClassDecl1 :: HasCallStack => RolesInfo -> Name -> Maybe (LHsContext GhcRn) tcClassDecl1 roles_info class_name hs_ctxt meths fundeps sigs ats at_defs = fixM $ \ clas -> -- We need the knot because 'clas' is passed into tcClassATs + mkSkolemInfo skol_info_anon >>= \skol_info -> bindTyClTyVars skol_info class_name $ \ _ binders res_kind -> do { checkClassKindSig res_kind ; traceTc "tcClassDecl 1" (ppr class_name $$ ppr binders) @@ -2488,7 +2495,7 @@ tcClassDecl1 roles_info class_name hs_ctxt meths fundeps sigs ats at_defs ppr fds) ; return clas } where - skol_info = TyConSkol callStack ClassFlavour class_name + skol_info_anon = TyConSkol callStack ClassFlavour class_name tc_fundep :: GHC.Hs.FunDep GhcRn -> TcM ([Var],[Var]) tc_fundep (FunDep _ tvs1 tvs2) = do { tvs1' <- mapM (tcLookupTyVar . unLoc) tvs1 ; @@ -2712,7 +2719,8 @@ tcFamDecl1 parent (FamilyDecl { fdInfo = fam_info , fdResultSig = L _ sig , fdInjectivityAnn = inj }) | DataFamily <- fam_info - = bindTyClTyVars (TyConSkol callStack (DataFamilyFlavour Nothing) tc_name) tc_name $ \ _ binders res_kind -> do + = mkSkolemInfo (TyConSkol callStack (DataFamilyFlavour Nothing) tc_name) >>= \skol_info -> + bindTyClTyVars skol_info tc_name $ \ _ binders res_kind -> do { traceTc "tcFamDecl1 data family:" (ppr tc_name) ; checkFamFlag tc_name @@ -2738,7 +2746,9 @@ tcFamDecl1 parent (FamilyDecl { fdInfo = fam_info ; return tycon } | OpenTypeFamily <- fam_info - = bindTyClTyVars (TyConSkol callStack (OpenTypeFamilyFlavour Nothing) tc_name) tc_name $ \ _ binders res_kind -> do + = + mkSkolemInfo (TyConSkol callStack (OpenTypeFamilyFlavour Nothing) tc_name) >>= \skol_info -> + bindTyClTyVars skol_info tc_name $ \ _ binders res_kind -> do { traceTc "tcFamDecl1 open type family:" (ppr tc_name) ; checkFamFlag tc_name ; inj' <- tcInjectivity binders inj @@ -2853,7 +2863,7 @@ tcInjectivity tcbs (Just (L loc (InjectivityAnn _ _ lInjNames))) tcTySynRhs :: RolesInfo -> Name -> LHsType GhcRn -> TcM TyCon tcTySynRhs roles_info tc_name hs_ty - = bindTyClTyVars skol_info tc_name $ \ _ binders res_kind -> + = mkSkolemInfo skol_info_anon >>= \skol_info -> bindTyClTyVars skol_info tc_name $ \ _ binders res_kind -> do { env <- getLclEnv ; traceTc "tc-syn" (ppr tc_name $$ ppr (tcl_env env)) ; rhs_ty <- pushLevelAndSolveEqualities skol_info (binderVars binders) $ @@ -2875,7 +2885,7 @@ tcTySynRhs roles_info tc_name hs_ty ; let roles = roles_info tc_name ; return (buildSynTyCon tc_name binders res_kind roles rhs_ty) } where - skol_info = TyConSkol callStack TypeSynonymFlavour tc_name + skol_info_anon = TyConSkol callStack TypeSynonymFlavour tc_name tcDataDefn :: SDoc -> RolesInfo -> Name -> HsDataDefn GhcRn -> TcM (TyCon, [DerivInfo]) @@ -2887,7 +2897,9 @@ tcDataDefn err_ctxt roles_info tc_name -- via inferInitialKinds , dd_cons = cons , dd_derivs = derivs }) - = bindTyClTyVars skol_info tc_name $ \ tctc tycon_binders res_kind -> + = + mkSkolemInfo skol_info_anon >>= \skol_info -> + bindTyClTyVars skol_info tc_name $ \ tctc tycon_binders res_kind -> -- 'tctc' is a 'TcTyCon' and has the 'tcTyConScopedTyVars' that we need -- unlike the finalized 'tycon' defined above which is an 'AlgTyCon' -- @@ -2953,7 +2965,7 @@ tcDataDefn err_ctxt roles_info tc_name ; traceTc "tcDataDefn" (ppr tc_name $$ ppr tycon_binders $$ ppr extra_bndrs) ; return (tycon, [deriv_info]) } where - skol_info = TyConSkol callStack flav tc_name + skol_info_anon = TyConSkol callStack flav tc_name flav = newOrDataToFlavour new_or_data -- Abstract data types in hsig files can have arbitrary kinds, @@ -3001,7 +3013,8 @@ kcTyFamInstEqn tc_fam_tc ; checkTyFamInstEqn tc_fam_tc eqn_tc_name hs_pats ; discardResult $ - bindOuterFamEqnTKBndrs_Q_Tv FamInstSkol outer_bndrs $ + mkSkolemInfo FamInstSkol >>= \skol_info -> + bindOuterFamEqnTKBndrs_Q_Tv skol_info outer_bndrs $ do { (_fam_app, res_kind) <- tcFamTyPats tc_fam_tc hs_pats ; tcCheckLHsType hs_rhs_ty (TheKind res_kind) } -- Why "_Tv" here? Consider (#14066) @@ -3131,12 +3144,12 @@ tcTyFamInstEqnGuts fam_tc mb_clsinfo outer_hs_bndrs hs_pats hs_rhs_ty -- By now, for type families (but not data families) we should -- have checked that the number of patterns matches tyConArity - + ; skol_info <- mkSkolemInfo FamInstSkol -- This code is closely related to the code -- in GHC.Tc.Gen.HsType.kcCheckDeclHeader_cusk ; (tclvl, wanted, (outer_tvs, (lhs_ty, rhs_ty))) <- pushLevelAndSolveEqualitiesX "tcTyFamInstEqnGuts" $ - bindOuterFamEqnTKBndrs FamInstSkol outer_hs_bndrs $ + bindOuterFamEqnTKBndrs skol_info outer_hs_bndrs $ do { (lhs_ty, rhs_kind) <- tcFamTyPats fam_tc hs_pats -- Ensure that the instance is consistent with its -- parent class (#16008) @@ -3153,8 +3166,8 @@ tcTyFamInstEqnGuts fam_tc mb_clsinfo outer_hs_bndrs hs_pats hs_rhs_ty -- See Note [Generalising in tcTyFamInstEqnGuts] ; dvs <- candidateQTyVarsOfTypes (lhs_ty : mkTyVarTys outer_tvs) -- MP: @rae these qtvs end up in a no skolem info message for T14904a - ; qtvs <- quantifyTyVars FamInstSkol TryNotToDefaultNonStandardTyVars dvs - ; reportUnsolvedEqualities FamInstSkol qtvs tclvl wanted + ; qtvs <- quantifyTyVars skol_info TryNotToDefaultNonStandardTyVars dvs + ; reportUnsolvedEqualities skol_info qtvs tclvl wanted ; checkFamTelescope tclvl outer_hs_bndrs outer_tvs ; traceTc "tcTyFamInstEqnGuts 2" $ @@ -3194,8 +3207,8 @@ checkFamTelescope tclvl hs_outer_bndrs outer_tvs | HsOuterExplicit { hso_bndrs = bndrs } <- hs_outer_bndrs , (b_first : _) <- bndrs , let b_last = last bndrs - skol_info = ForAllSkol (fsep (map ppr bndrs)) - = setSrcSpan (combineSrcSpans (getLocA b_first) (getLocA b_last)) $ + = setSrcSpan (combineSrcSpans (getLocA b_first) (getLocA b_last)) $ do + skol_info <- mkSkolemInfo (ForAllSkol (fsep (map ppr bndrs))) emitResidualTvConstraint skol_info outer_tvs tclvl emptyWC | otherwise = return () @@ -3400,6 +3413,7 @@ tcConDecl new_or_data dd_info rep_tycon tc_bndrs res_kind tag_map -- , hsq_explicit = {f,b} } ; traceTc "tcConDecl 1" (vcat [ ppr name, ppr explicit_tkv_nms ]) + ; skol_info <- mkSkolemInfo (DataConSkol name) ; (tclvl, wanted, (exp_tvbndrs, (ctxt, arg_tys, field_lbls, stricts))) <- pushLevelAndSolveEqualitiesX "tcConDecl:H98" $ @@ -3473,8 +3487,6 @@ tcConDecl new_or_data dd_info rep_tycon tc_bndrs res_kind tag_map -- that way checkValidDataCon can complain if it's wrong. ; return [dc] } - where - skol_info = DataConSkol name tcConDecl new_or_data dd_info rep_tycon tc_bndrs _res_kind tag_map -- NB: don't use res_kind here, as it's ill-scoped. Instead, @@ -3486,7 +3498,7 @@ tcConDecl new_or_data dd_info rep_tycon tc_bndrs _res_kind tag_map = addErrCtxt (dataConCtxt names) $ do { traceTc "tcConDecl 1 gadt" (ppr names) ; let (L _ name : _) = names - + ; skol_info <- mkSkolemInfo skol_info_anon ; (tclvl, wanted, (outer_bndrs, (ctxt, arg_tys, res_ty, field_lbls, stricts))) <- pushLevelAndSolveEqualitiesX "tcConDecl:GADT" $ tcOuterTKBndrs skol_info outer_hs_bndrs $ @@ -3562,7 +3574,7 @@ tcConDecl new_or_data dd_info rep_tycon tc_bndrs _res_kind tag_map } ; mapM buildOneDataCon names } where - skol_info = DataConSkol (unLoc (head names)) + skol_info_anon = DataConSkol (unLoc (head names)) {- Note [GADT return types] ~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/compiler/GHC/Tc/TyCl/Class.hs b/compiler/GHC/Tc/TyCl/Class.hs index 675f81e45c..ba199abe4e 100644 --- a/compiler/GHC/Tc/TyCl/Class.hs +++ b/compiler/GHC/Tc/TyCl/Class.hs @@ -213,10 +213,11 @@ tcClassDecl2 class_scoped_tv_env -- dm1 = \d -> case ds d of (a,b,c) -> a -- And since ds is big, it doesn't get inlined, so we don't get good -- default methods. Better to make separate AbsBinds for each + + ; skol_info <- mkSkolemInfo (TyConSkol callStack ClassFlavour (getName class_name)) ; let (tyvars, _, _, op_items) = classBigSig clas prag_fn = mkPragEnv sigs default_binds sig_fn = mkHsSigFun sigs - skol_info = TyConSkol callStack ClassFlavour (getName class_name) (skol_subst, clas_tyvars) = tcSuperSkolTyVars skol_info tyvars pred = mkClassPred clas (mkTyVarTys clas_tyvars) scoped_tyvars = diff --git a/compiler/GHC/Tc/TyCl/Instance.hs b/compiler/GHC/Tc/TyCl/Instance.hs index ab40a331c4..0006f63ed7 100644 --- a/compiler/GHC/Tc/TyCl/Instance.hs +++ b/compiler/GHC/Tc/TyCl/Instance.hs @@ -492,8 +492,8 @@ tcClsInstDecl (L loc (ClsInstDecl { cid_poly_ty = hs_ty, cid_binds = binds do { dfun_ty <- tcHsClsInstType (InstDeclCtxt False) hs_ty ; let (tyvars, theta, clas, inst_tys) = tcSplitDFunTy dfun_ty -- NB: tcHsClsInstType does checkValidInstance - - ; (subst, skol_tvs) <- tcInstSkolTyVars InstSkol tyvars + ; skol_info <- mkSkolemInfo InstSkol + ; (subst, skol_tvs) <- tcInstSkolTyVars skol_info tyvars ; let tv_skol_prs = [ (tyVarName tv, skol_tv) | (tv, skol_tv) <- tyvars `zip` skol_tvs ] -- Map from the skolemized Names to the original Names. @@ -874,9 +874,10 @@ tcDataFamInstHeader tcDataFamInstHeader mb_clsinfo fam_tc outer_bndrs fixity hs_ctxt hs_pats m_ksig new_or_data = do { traceTc "tcDataFamInstHeader {" (ppr fam_tc <+> ppr hs_pats) + ; skol_info <- mkSkolemInfo FamInstSkol ; (tclvl, wanted, (scoped_tvs, (stupid_theta, lhs_ty, master_res_kind, instance_res_kind))) <- pushLevelAndSolveEqualitiesX "tcDataFamInstHeader" $ - bindOuterFamEqnTKBndrs FamInstSkol outer_bndrs $ + bindOuterFamEqnTKBndrs skol_info outer_bndrs $ do { stupid_theta <- tcHsContext hs_ctxt ; (lhs_ty, lhs_kind) <- tcFamTyPats fam_tc hs_pats ; (lhs_applied_ty, lhs_applied_kind) @@ -915,8 +916,8 @@ tcDataFamInstHeader mb_clsinfo fam_tc outer_bndrs fixity -- See GHC.Tc.TyCl Note [Generalising in tcFamTyPatsGuts] ; dvs <- candidateQTyVarsOfTypes (lhs_ty : mkTyVarTys scoped_tvs) - ; qtvs <- quantifyTyVars FamInstSkol TryNotToDefaultNonStandardTyVars dvs - ; reportUnsolvedEqualities FamInstSkol qtvs tclvl wanted + ; qtvs <- quantifyTyVars skol_info TryNotToDefaultNonStandardTyVars dvs + ; reportUnsolvedEqualities skol_info qtvs tclvl wanted -- Zonk the patterns etc into the Type world ; ze <- mkEmptyZonkEnv NoFlexi @@ -962,7 +963,8 @@ tcDataFamInstHeader mb_clsinfo fam_tc outer_bndrs fixity = do { sig_kind <- tcLHsKindSig data_ctxt hs_kind ; lvl <- getTcLevel ; let (tvs, inner_kind) = tcSplitForAllInvisTyVars sig_kind - ; (subst, _tvs') <- tcInstSkolTyVarsAt (SigTypeSkol data_ctxt) lvl False emptyTCvSubst tvs + ; skol_info <- mkSkolemInfo (SigTypeSkol data_ctxt) + ; (subst, _tvs') <- tcInstSkolTyVarsAt skol_info lvl False emptyTCvSubst tvs -- Perhaps surprisingly, we don't need the skolemised tvs themselves ; return (substTy subst inner_kind) } @@ -1211,7 +1213,8 @@ tcInstDecl2 (InstInfo { iSpec = ispec, iBinds = ibinds }) setSrcSpan loc $ addErrCtxt (instDeclCtxt2 (idType dfun_id)) $ do { -- Instantiate the instance decl with skolem constants - ; (inst_tyvars, dfun_theta, inst_head) <- tcSkolDFunType InstSkol dfun_id + ; skol_info <- mkSkolemInfo InstSkol + ; (inst_tyvars, dfun_theta, inst_head) <- tcSkolDFunType skol_info dfun_id ; dfun_ev_vars <- newEvVars dfun_theta -- We instantiate the dfun_id with superSkolems. -- See Note [Subtle interaction of recursion and overlap] diff --git a/compiler/GHC/Tc/TyCl/PatSyn.hs b/compiler/GHC/Tc/TyCl/PatSyn.hs index 552028b364..26ffe9116e 100644 --- a/compiler/GHC/Tc/TyCl/PatSyn.hs +++ b/compiler/GHC/Tc/TyCl/PatSyn.hs @@ -393,7 +393,7 @@ tcCheckPatSynDecl psb@PSB{ psb_id = lname@(L _ name), psb_args = details ; checkTc (all (isManyDataConTy . scaledMult) arg_tys) $ TcRnLinearPatSyn sig_body_ty - ; let skol_info = SigSkol (PatSynCtxt name) pat_ty [] + ; skol_info <- mkSkolemInfo (SigSkol (PatSynCtxt name) pat_ty []) -- The type here is a bit bogus, but we do not print -- the type for PatSynCtxt, so it doesn't matter -- See Note [Skolem info for pattern synonyms] in "GHC.Tc.Types.Origin" @@ -441,7 +441,7 @@ tcCheckPatSynDecl psb@PSB{ psb_id = lname@(L _ name), psb_args = details skol_arg_tys ; return (ex_tvs', prov_dicts, args') } - ; (implics, ev_binds) <- buildImplicationFor tclvl skol_info skol_univ_tvs + ; (implics, ev_binds) <- buildImplicationFor tclvl (getSkolemInfo skol_info) skol_univ_tvs req_dicts wanted -- Solve the constraints now, because we are about to make a PatSyn, diff --git a/compiler/GHC/Tc/Types/Constraint.hs b/compiler/GHC/Tc/Types/Constraint.hs index 103f0744b6..d2552d3d88 100644 --- a/compiler/GHC/Tc/Types/Constraint.hs +++ b/compiler/GHC/Tc/Types/Constraint.hs @@ -1306,7 +1306,7 @@ data Implication -- allocated /inside/ this implication ic_skols :: [TcTyVar], -- Introduced skolems - ic_info :: SkolemInfo, -- See Note [Skolems in an implication] + ic_info :: SkolemInfoAnon, -- See Note [Skolems in an implication] -- See Note [Shadowing in a constraint] ic_given :: [EvVar], -- Given evidence variables @@ -1454,7 +1454,7 @@ instance Outputable ImplicStatus where ppr (IC_Solved { ics_dead = dead }) = text "Solved" <+> (braces (text "Dead givens =" <+> ppr dead)) -checkTelescopeSkol :: SkolemInfo -> Bool +checkTelescopeSkol :: SkolemInfoAnon -> Bool -- See Note [Checking telescopes] checkTelescopeSkol (ForAllSkol {}) = True checkTelescopeSkol _ = False @@ -2179,7 +2179,7 @@ mkKindLoc s1 s2 loc = setCtLocOrigin (toKindLoc loc) toKindLoc :: CtLoc -> CtLoc toKindLoc loc = loc { ctl_t_or_k = Just KindLevel } -mkGivenLoc :: TcLevel -> SkolemInfo -> TcLclEnv -> CtLoc +mkGivenLoc :: TcLevel -> SkolemInfoAnon -> TcLclEnv -> CtLoc mkGivenLoc tclvl skol_info env = CtLoc { ctl_origin = GivenOrigin skol_info , ctl_env = setLclEnvTcLevel env tclvl diff --git a/compiler/GHC/Tc/Types/Origin.hs b/compiler/GHC/Tc/Types/Origin.hs index 0ea40b1ec5..2d9cbc83d9 100644 --- a/compiler/GHC/Tc/Types/Origin.hs +++ b/compiler/GHC/Tc/Types/Origin.hs @@ -13,7 +13,7 @@ module GHC.Tc.Types.Origin ( redundantConstraintsSpan, -- SkolemInfo - SkolemInfo(..), pprSigSkolInfo, pprSkolInfo, unkSkol, + SkolemInfo(..), SkolemInfoAnon(..), mkSkolemInfo, getSkolemInfo, pprSigSkolInfo, pprSkolInfo, unkSkol, -- CtOrigin CtOrigin(..), exprCtOrigin, lexprCtOrigin, matchesCtOrigin, grhssCtOrigin, @@ -59,6 +59,9 @@ import GHC.Utils.Outputable import GHC.Utils.Panic import GHC.Utils.Trace import GHC.Stack +import GHC.Utils.Monad +import GHC.Types.Unique +import GHC.Types.Unique.Supply {- ********************************************************************* * * @@ -194,10 +197,15 @@ isSigMaybe _ = Nothing ************************************************************************ -} +data SkolemInfo = SkolemInfo Unique SkolemInfoAnon + +instance Uniquable SkolemInfo where + getUnique (SkolemInfo u _) = u + -- SkolemInfo gives the origin of *given* constraints -- a) type variables are skolemised -- b) an implication constraint is generated -data SkolemInfo +data SkolemInfoAnon = SigSkol -- A skolem that is created by instantiating -- a programmer-supplied type signature -- Location of the binding site is on the TyVar @@ -255,17 +263,30 @@ data SkolemInfo | RuntimeUnkSkol -- Runtime skolem from the GHCi debugger #14628 - | UnkSkol CallStack -- Unhelpful info (until I improve it) + | UnkSkol CallStack + unkSkol :: HasCallStack => SkolemInfo -unkSkol = UnkSkol callStack +unkSkol = SkolemInfo (mkUniqueGrimily 0) (UnkSkol callStack) + +mkSkolemInfo :: MonadIO m => SkolemInfoAnon -> m SkolemInfo +mkSkolemInfo sk_anon = do + u <- liftIO $! uniqFromMask 's' + return (SkolemInfo u sk_anon) + +getSkolemInfo :: SkolemInfo -> SkolemInfoAnon +getSkolemInfo (SkolemInfo _ skol_anon) = skol_anon + instance Outputable SkolemInfo where + ppr (SkolemInfo _ sk_info ) = ppr sk_info + +instance Outputable SkolemInfoAnon where ppr = pprSkolInfo -pprSkolInfo :: SkolemInfo -> SDoc +pprSkolInfo :: SkolemInfoAnon -> SDoc -- Complete the sentence "is a rigid type variable bound by..." pprSkolInfo (SigSkol cx ty _) = pprSigSkolInfo cx ty pprSkolInfo (SigTypeSkol cx) = pprUserTypeCtxt cx @@ -293,7 +314,8 @@ pprSkolInfo RuntimeUnkSkol = text "Unknown type from GHCi runtime" -- unkSkol -- For type variables the others are dealt with by pprSkolTvBinding. -- For Insts, these cases should not happen -pprSkolInfo (UnkSkol cs) = warnPprTrace True (text "pprSkolInfo: unkSkol") $ text "UnkSkol" $$ prettyCallStackDoc cs +pprSkolInfo (UnkSkol cs) = warnPprTrace True (text "pprSkolInfo: unkSkol") $ text "UnkSkol" $$ prettyCallStackDoc cs + pprSigSkolInfo :: UserTypeCtxt -> TcType -> SDoc -- The type is already tidied @@ -366,7 +388,7 @@ in the right place. So we proceed as follows: data CtOrigin = -- | A given constraint from a user-written type signature. The -- 'SkolemInfo' inside gives more information. - GivenOrigin SkolemInfo + GivenOrigin SkolemInfoAnon -- The following are other origins for given constraints that cannot produce -- new skolems -- hence no SkolemInfo. @@ -397,7 +419,7 @@ data CtOrigin -- Note [Use only the best local instance], both in GHC.Tc.Solver.Interact. | OtherSCOrigin ScDepth -- ^ The number of superclass selections necessary to -- get this constraint - SkolemInfo -- ^ Where the sub-class constraint arose from + SkolemInfoAnon -- ^ Where the sub-class constraint arose from -- (used only for printing) -- All the others are for *wanted* constraints diff --git a/compiler/GHC/Tc/Types/Origin.hs-boot b/compiler/GHC/Tc/Types/Origin.hs-boot index 67d49c43c9..1110cc0967 100644 --- a/compiler/GHC/Tc/Types/Origin.hs-boot +++ b/compiler/GHC/Tc/Types/Origin.hs-boot @@ -2,6 +2,7 @@ module GHC.Tc.Types.Origin where import GHC.Stack ( HasCallStack ) +data SkolemInfoAnon data SkolemInfo unkSkol :: HasCallStack => SkolemInfo diff --git a/compiler/GHC/Tc/Utils/Backpack.hs b/compiler/GHC/Tc/Utils/Backpack.hs index 4b8d2f7d3a..20b81f8b3c 100644 --- a/compiler/GHC/Tc/Utils/Backpack.hs +++ b/compiler/GHC/Tc/Utils/Backpack.hs @@ -229,13 +229,13 @@ check_inst sig_inst = do mapM_ tcLookupImported_maybe (nameSetElemsStable (orphNamesOfClsInst sig_inst)) -- Based off of 'simplifyDeriv' let ty = idType (instanceDFunId sig_inst) - skol_info = InstSkol -- Based off of tcSplitDFunTy (tvs, theta, pred) = case tcSplitForAllInvisTyVars ty of { (tvs, rho) -> case splitFunTys rho of { (theta, pred) -> (tvs, theta, pred) }} origin = InstProvidedOrigin (tcg_semantic_mod tcg_env) sig_inst + skol_info <- mkSkolemInfo InstSkol (skol_subst, tvs_skols) <- tcInstSkolTyVars skol_info tvs -- Skolemize (tclvl,cts) <- pushTcLevelM $ do wanted <- newWanted origin @@ -253,7 +253,7 @@ check_inst sig_inst = do return $ wanted : givens unsolved <- simplifyWantedsTcM cts - (implic, _) <- buildImplicationFor tclvl skol_info tvs_skols [] unsolved + (implic, _) <- buildImplicationFor tclvl (getSkolemInfo skol_info) tvs_skols [] unsolved reportAllUnsolved (mkImplicWC implic) -- | For a module @modname@ of type 'HscSource', determine the list diff --git a/compiler/GHC/Tc/Utils/TcMType.hs b/compiler/GHC/Tc/Utils/TcMType.hs index 3cae5f2427..b6a750764a 100644 --- a/compiler/GHC/Tc/Utils/TcMType.hs +++ b/compiler/GHC/Tc/Utils/TcMType.hs @@ -68,7 +68,7 @@ module GHC.Tc.Utils.TcMType ( -------------------------------- -- Zonking and tidying zonkTidyTcType, zonkTidyTcTypes, zonkTidyOrigin, zonkTidyOrigins, - tidyEvVar, tidyCt, tidyHole, tidySkolemInfo, + tidyEvVar, tidyCt, tidyHole, tidySkolemInfo, tidySkolemInfoAnon, zonkTcTyVar, zonkTcTyVars, zonkTcTyVarToTyVar, zonkInvisTVBinder, zonkTyCoVarsAndFV, zonkTcTypeAndFV, zonkDTyCoVarSetAndFV, @@ -2332,7 +2332,7 @@ zonkImplication implic@(Implic { ic_skols = skols = do { skols' <- mapM zonkTyCoVarKind skols -- Need to zonk their kinds! -- as #7230 showed ; given' <- mapM zonkEvVar given - ; info' <- zonkSkolemInfo info + ; info' <- zonkSkolemInfoAnon info ; wanted' <- zonkWCRec wanted ; return (implic { ic_skols = skols' , ic_given = given' @@ -2415,13 +2415,16 @@ zonkCtEvidence ctev } zonkSkolemInfo :: SkolemInfo -> TcM SkolemInfo -zonkSkolemInfo (SigSkol cx ty tv_prs) = do { ty' <- zonkTcType ty +zonkSkolemInfo (SkolemInfo uid sk_anon) = SkolemInfo uid <$> zonkSkolemInfoAnon sk_anon + +zonkSkolemInfoAnon :: SkolemInfoAnon -> TcM SkolemInfoAnon +zonkSkolemInfoAnon (SigSkol cx ty tv_prs) = do { ty' <- zonkTcType ty ; return (SigSkol cx ty' tv_prs) } -zonkSkolemInfo (InferSkol ntys) = do { ntys' <- mapM do_one ntys +zonkSkolemInfoAnon (InferSkol ntys) = do { ntys' <- mapM do_one ntys ; return (InferSkol ntys') } where do_one (n, ty) = do { ty' <- zonkTcType ty; return (n, ty') } -zonkSkolemInfo skol_info = return skol_info +zonkSkolemInfoAnon skol_info = return skol_info zonkTyVarSkolemInfo :: TyVar -> TcM TyVar zonkTyVarSkolemInfo = @@ -2565,12 +2568,12 @@ zonkTidyTcTypes = zonkTidyTcTypes' [] zonkTidyOrigin :: TidyEnv -> CtOrigin -> TcM (TidyEnv, CtOrigin) zonkTidyOrigin env (GivenOrigin skol_info) - = do { skol_info1 <- zonkSkolemInfo skol_info - ; let skol_info2 = tidySkolemInfo env skol_info1 + = do { skol_info1 <- zonkSkolemInfoAnon skol_info + ; let skol_info2 = tidySkolemInfoAnon env skol_info1 ; return (env, GivenOrigin skol_info2) } zonkTidyOrigin env (OtherSCOrigin sc_depth skol_info) - = do { skol_info1 <- zonkSkolemInfo skol_info - ; let skol_info2 = tidySkolemInfo env skol_info1 + = do { skol_info1 <- zonkSkolemInfoAnon skol_info + ; let skol_info2 = tidySkolemInfoAnon env skol_info1 ; return (env, OtherSCOrigin sc_depth skol_info2) } zonkTidyOrigin env orig@(TypeEqOrigin { uo_actual = act , uo_expected = exp }) @@ -2624,16 +2627,19 @@ tidyHole env h@(Hole { hole_ty = ty }) = h { hole_ty = tidyType env ty } tidyEvVar :: TidyEnv -> EvVar -> EvVar tidyEvVar env var = updateIdTypeAndMult (tidyType env) var ----------------- tidySkolemInfo :: TidyEnv -> SkolemInfo -> SkolemInfo -tidySkolemInfo env (DerivSkol ty) = DerivSkol (tidyType env ty) -tidySkolemInfo env (SigSkol cx ty tv_prs) = tidySigSkol env cx ty tv_prs -tidySkolemInfo env (InferSkol ids) = InferSkol (mapSnd (tidyType env) ids) -tidySkolemInfo env (UnifyForAllSkol ty) = UnifyForAllSkol (tidyType env ty) -tidySkolemInfo _ info = info +tidySkolemInfo env (SkolemInfo u sk_anon) = SkolemInfo u (tidySkolemInfoAnon env sk_anon) + +---------------- +tidySkolemInfoAnon :: TidyEnv -> SkolemInfoAnon -> SkolemInfoAnon +tidySkolemInfoAnon env (DerivSkol ty) = DerivSkol (tidyType env ty) +tidySkolemInfoAnon env (SigSkol cx ty tv_prs) = tidySigSkol env cx ty tv_prs +tidySkolemInfoAnon env (InferSkol ids) = InferSkol (mapSnd (tidyType env) ids) +tidySkolemInfoAnon env (UnifyForAllSkol ty) = UnifyForAllSkol (tidyType env ty) +tidySkolemInfoAnon _ info = info tidySigSkol :: TidyEnv -> UserTypeCtxt - -> TcType -> [(Name,TcTyVar)] -> SkolemInfo + -> TcType -> [(Name,TcTyVar)] -> SkolemInfoAnon -- We need to take special care when tidying SigSkol -- See Note [SigSkol SkolemInfo] in "GHC.Tc.Types.Origin" tidySigSkol env cx ty tv_prs diff --git a/compiler/GHC/Tc/Utils/Unify.hs b/compiler/GHC/Tc/Utils/Unify.hs index 36d9de2839..d75fef9ccb 100644 --- a/compiler/GHC/Tc/Utils/Unify.hs +++ b/compiler/GHC/Tc/Utils/Unify.hs @@ -867,11 +867,11 @@ tcSkolemiseScoped ctxt expected_ty thing_inside = do { ; rec { (wrap, tv_prs, given, rho_ty) <- topSkolemise skol_info expected_ty ; let skol_tvs = map snd tv_prs - skol_info = SigSkol ctxt expected_ty tv_prs + ; skol_info <- mkSkolemInfo (SigSkol ctxt expected_ty tv_prs) } ; (ev_binds, res) - <- checkConstraints skol_info skol_tvs given $ + <- checkConstraints (getSkolemInfo skol_info) skol_tvs given $ tcExtendNameTyVarEnv tv_prs $ thing_inside rho_ty @@ -886,11 +886,11 @@ tcSkolemise ctxt expected_ty thing_inside ; rec { (wrap, tv_prs, given, rho_ty) <- topSkolemise skol_info expected_ty ; let skol_tvs = map snd tv_prs - skol_info = SigSkol ctxt expected_ty tv_prs + ; skol_info <- mkSkolemInfo (SigSkol ctxt expected_ty tv_prs) } ; (ev_binds, result) - <- checkConstraints skol_info skol_tvs given $ + <- checkConstraints (getSkolemInfo skol_info) skol_tvs given $ thing_inside rho_ty ; return (wrap <.> mkWpLet ev_binds, result) } @@ -907,7 +907,7 @@ tcSkolemiseET ctxt (Check ty) thing_inside = tcSkolemise ctxt ty $ \rho_ty -> thing_inside (mkCheckExpType rho_ty) -checkConstraints :: SkolemInfo +checkConstraints :: SkolemInfoAnon -> [TcTyVar] -- Skolems -> [EvVar] -- Given -> TcM result @@ -943,7 +943,7 @@ emitResidualTvConstraint :: SkolemInfo -> [TcTyVar] -> TcLevel -> WantedConstraints -> TcM () emitResidualTvConstraint skol_info skol_tvs tclvl wanted | not (isEmptyWC wanted) || - checkTelescopeSkol skol_info + checkTelescopeSkol (getSkolemInfo skol_info) = -- checkTelescopeSkol: in this case, /always/ emit this implication -- even if 'wanted' is empty. We need the implication so that we check -- for a bad telescope. See Note [Skolem escape and forall-types] in @@ -967,9 +967,9 @@ buildTvImplication skol_info skol_tvs tclvl wanted , ic_given_eqs = NoGivenEqs , ic_wanted = wanted , ic_binds = ev_binds - , ic_info = skol_info }) } + , ic_info = getSkolemInfo skol_info }) } -implicationNeeded :: SkolemInfo -> [TcTyVar] -> [EvVar] -> TcM Bool +implicationNeeded :: SkolemInfoAnon -> [TcTyVar] -> [EvVar] -> TcM Bool -- See Note [When to build an implication] implicationNeeded skol_info skol_tvs given | null skol_tvs @@ -989,7 +989,7 @@ implicationNeeded skol_info skol_tvs given | otherwise -- Non-empty skolems or givens = return True -- Definitely need an implication -alwaysBuildImplication :: SkolemInfo -> Bool +alwaysBuildImplication :: SkolemInfoAnon -> Bool -- See Note [When to build an implication] alwaysBuildImplication _ = False @@ -1006,7 +1006,7 @@ alwaysBuildImplication (FamInstSkol {}) = True alwaysBuildImplication _ = False -} -buildImplicationFor :: TcLevel -> SkolemInfo -> [TcTyVar] +buildImplicationFor :: TcLevel -> SkolemInfoAnon -> [TcTyVar] -> [EvVar] -> WantedConstraints -> TcM (Bag Implication, TcEvBinds) buildImplicationFor tclvl skol_info skol_tvs given wanted diff --git a/compiler/GHC/Tc/Utils/Zonk.hs b/compiler/GHC/Tc/Utils/Zonk.hs index 18c1e36545..f5a9d2f0e9 100644 --- a/compiler/GHC/Tc/Utils/Zonk.hs +++ b/compiler/GHC/Tc/Utils/Zonk.hs @@ -1799,9 +1799,7 @@ zonkTyVarOcc env@(ZonkEnv { ze_flexi = flexi lookup_in_tv_env -- Look up in the env just as we do for Ids = case lookupVarEnv tv_env tv of - Nothing -> do - tv' <- updateTyVarKindM (zonkTcTypeToTypeX env) tv - return $ mkTyVarTy tv' + Nothing -> mkTyVarTy <$> updateTyVarKindM (zonkTcTypeToTypeX env) tv Just tv' -> return (mkTyVarTy tv') zonk_meta ref Flexi diff --git a/compiler/GHC/Types/Unique/Map.hs b/compiler/GHC/Types/Unique/Map.hs index 41765e1c7c..dfbc13fe4f 100644 --- a/compiler/GHC/Types/Unique/Map.hs +++ b/compiler/GHC/Types/Unique/Map.hs @@ -41,6 +41,7 @@ module GHC.Types.Unique.Map ( lookupWithDefaultUniqMap, anyUniqMap, allUniqMap, + nonDetEltsUniqMap -- Non-deterministic functions omitted ) where @@ -204,3 +205,6 @@ anyUniqMap f (UniqMap m) = anyUFM (f . snd) m allUniqMap :: (a -> Bool) -> UniqMap k a -> Bool allUniqMap f (UniqMap m) = allUFM (f . snd) m + +nonDetEltsUniqMap :: UniqMap k a -> [(k, a)] +nonDetEltsUniqMap (UniqMap m) = nonDetEltsUFM m |
