summaryrefslogtreecommitdiff
path: root/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'compiler')
-rw-r--r--compiler/basicTypes/OccName.hs7
-rw-r--r--compiler/basicTypes/VarEnv.hs9
-rw-r--r--compiler/typecheck/TcHsType.hs2
-rw-r--r--compiler/typecheck/TcMType.hs123
-rw-r--r--compiler/typecheck/TcPatSyn.hs4
-rw-r--r--compiler/types/TyCoPpr.hs7
-rw-r--r--compiler/types/TyCoTidy.hs6
-rw-r--r--compiler/utils/Outputable.hs11
8 files changed, 119 insertions, 50 deletions
diff --git a/compiler/basicTypes/OccName.hs b/compiler/basicTypes/OccName.hs
index 7584fa4380..ac2ad47100 100644
--- a/compiler/basicTypes/OccName.hs
+++ b/compiler/basicTypes/OccName.hs
@@ -94,7 +94,7 @@ module OccName (
-- * Tidying up
TidyOccEnv, emptyTidyOccEnv, initTidyOccEnv,
- tidyOccName, avoidClashesOccEnv,
+ tidyOccName, avoidClashesOccEnv, delTidyOccEnvList,
-- FsEnv
FastStringEnv, emptyFsEnv, lookupFsEnv, extendFsEnv, mkFsEnv
@@ -818,7 +818,7 @@ Every id contributes a type variable to the type signature, and all of them are
(id,id,id) :: (a2 -> a2, a1 -> a1, a -> a)
-which is a bit unfortunate, as it unfairly renames only one of them. What we
+which is a bit unfortunate, as it unfairly renames only two of them. What we
would like to see is
(id,id,id) :: (a3 -> a3, a2 -> a2, a1 -> a1)
@@ -846,6 +846,9 @@ initTidyOccEnv = foldl' add emptyUFM
where
add env (OccName _ fs) = addToUFM env fs 1
+delTidyOccEnvList :: TidyOccEnv -> [FastString] -> TidyOccEnv
+delTidyOccEnvList = delListFromUFM
+
-- see Note [Tidying multiple names at once]
avoidClashesOccEnv :: TidyOccEnv -> [OccName] -> TidyOccEnv
avoidClashesOccEnv env occs = go env emptyUFM occs
diff --git a/compiler/basicTypes/VarEnv.hs b/compiler/basicTypes/VarEnv.hs
index 4c23b1f141..dfdc933b67 100644
--- a/compiler/basicTypes/VarEnv.hs
+++ b/compiler/basicTypes/VarEnv.hs
@@ -71,13 +71,14 @@ module VarEnv (
-- * TidyEnv and its operation
TidyEnv,
- emptyTidyEnv, mkEmptyTidyEnv
+ emptyTidyEnv, mkEmptyTidyEnv, delTidyEnvList
) where
import GhcPrelude
import qualified Data.IntMap.Strict as IntMap -- TODO: Move this to UniqFM
import OccName
+import Name
import Var
import VarSet
import UniqSet
@@ -424,6 +425,12 @@ emptyTidyEnv = (emptyTidyOccEnv, emptyVarEnv)
mkEmptyTidyEnv :: TidyOccEnv -> TidyEnv
mkEmptyTidyEnv occ_env = (occ_env, emptyVarEnv)
+delTidyEnvList :: TidyEnv -> [Var] -> TidyEnv
+delTidyEnvList (occ_env, var_env) vs = (occ_env', var_env')
+ where
+ occ_env' = occ_env `delTidyOccEnvList` map (occNameFS . getOccName) vs
+ var_env' = var_env `delVarEnvList` vs
+
{-
************************************************************************
* *
diff --git a/compiler/typecheck/TcHsType.hs b/compiler/typecheck/TcHsType.hs
index 17c027318b..61980788cd 100644
--- a/compiler/typecheck/TcHsType.hs
+++ b/compiler/typecheck/TcHsType.hs
@@ -1770,7 +1770,7 @@ the surrounding context, we must obey the following dictum:
Every metavariable in a type must either be
(A) generalized, or
(B) promoted, or See Note [Promotion in signatures]
- (C) zapped to Any See Note [Naughty quantification candidates] in TcMType
+ (C) a cause to error See Note [Naughty quantification candidates] in TcMType
The kindGeneralize functions do not require pre-zonking; they zonk as they
go.
diff --git a/compiler/typecheck/TcMType.hs b/compiler/typecheck/TcMType.hs
index 0ac553c0ea..c86e3162c0 100644
--- a/compiler/typecheck/TcMType.hs
+++ b/compiler/typecheck/TcMType.hs
@@ -1137,8 +1137,7 @@ So alpha is entirely unconstrained.
What then should we do with alpha? During generalization, every
metavariable is either (A) promoted, (B) generalized, or (C) zapped
-(according again to Note [Recipe for checking a signature] in
-TcHsType).
+(according to Note [Recipe for checking a signature] in TcHsType).
* We can't generalise it.
* We can't promote it, because its kind prevents that
@@ -1146,11 +1145,14 @@ TcHsType).
go into the typing environment (as the type of some let-bound
variable, say), and then chaos erupts when we try to instantiate.
-So, we zap it, eagerly, to Any. We don't have to do this eager zapping
-in terms (say, in `length []`) because terms are never re-examined before
-the final zonk (which zaps any lingering metavariables to Any).
+Previously, we zapped it to Any. This worked, but it had the unfortunate
+effect of causing Any sometimes to appear in error messages. If this
+kind of signature happens, the user probably has made a mistake -- no
+one really wants Any in their types. So we now error. This must be
+a hard error (failure in the monad) to avoid other messages from mentioning
+Any.
-We do this eager zapping in candidateQTyVars, which always precedes
+We do this eager erroring in candidateQTyVars, which always precedes
generalisation, because at that moment we have a clear picture of what
skolems are in scope within the type itself (e.g. that 'forall arg').
@@ -1235,13 +1237,14 @@ partitionCandidates dvs@(DV { dv_kvs = kvs, dv_tvs = tvs }) pred
-- See Note [Dependent type variables]
candidateQTyVarsOfType :: TcType -- not necessarily zonked
-> TcM CandidatesQTvs
-candidateQTyVarsOfType ty = collect_cand_qtvs False emptyVarSet mempty ty
+candidateQTyVarsOfType ty = collect_cand_qtvs ty False emptyVarSet mempty ty
-- | Like 'candidateQTyVarsOfType', but over a list of types
-- The variables to quantify must have a TcLevel strictly greater than
-- the ambient level. (See Wrinkle in Note [Naughty quantification candidates])
candidateQTyVarsOfTypes :: [Type] -> TcM CandidatesQTvs
-candidateQTyVarsOfTypes tys = foldlM (collect_cand_qtvs False emptyVarSet) mempty tys
+candidateQTyVarsOfTypes tys = foldlM (\acc ty -> collect_cand_qtvs ty False emptyVarSet acc ty)
+ mempty tys
-- | Like 'candidateQTyVarsOfType', but consider every free variable
-- to be dependent. This is appropriate when generalizing a *kind*,
@@ -1249,11 +1252,12 @@ candidateQTyVarsOfTypes tys = foldlM (collect_cand_qtvs False emptyVarSet) mempt
-- to Type.)
candidateQTyVarsOfKind :: TcKind -- Not necessarily zonked
-> TcM CandidatesQTvs
-candidateQTyVarsOfKind ty = collect_cand_qtvs True emptyVarSet mempty ty
+candidateQTyVarsOfKind ty = collect_cand_qtvs ty True emptyVarSet mempty ty
candidateQTyVarsOfKinds :: [TcKind] -- Not necessarily zonked
-> TcM CandidatesQTvs
-candidateQTyVarsOfKinds tys = foldM (collect_cand_qtvs True emptyVarSet) mempty tys
+candidateQTyVarsOfKinds tys = foldM (\acc ty -> collect_cand_qtvs ty True emptyVarSet acc ty)
+ mempty tys
delCandidates :: CandidatesQTvs -> [Var] -> CandidatesQTvs
delCandidates (DV { dv_kvs = kvs, dv_tvs = tvs, dv_cvs = cvs }) vars
@@ -1262,7 +1266,8 @@ delCandidates (DV { dv_kvs = kvs, dv_tvs = tvs, dv_cvs = cvs }) vars
, dv_cvs = cvs `delVarSetList` vars }
collect_cand_qtvs
- :: Bool -- True <=> consider every fv in Type to be dependent
+ :: TcType -- original type that we started recurring into; for errors
+ -> Bool -- True <=> consider every fv in Type to be dependent
-> VarSet -- Bound variables (locals only)
-> CandidatesQTvs -- Accumulating parameter
-> Type -- Not necessarily zonked
@@ -1272,14 +1277,14 @@ collect_cand_qtvs
-- * Looks through meta-tyvars as it goes;
-- no need to zonk in advance
--
--- * Needs to be monadic anyway, because it does the zap-naughty
--- stuff; see Note [Naughty quantification candidates]
+-- * Needs to be monadic anyway, because it handles naughty
+-- quantification; see Note [Naughty quantification candidates]
--
-- * Returns fully-zonked CandidateQTvs, including their kinds
-- so that subsequent dependency analysis (to build a well
-- scoped telescope) works correctly
-collect_cand_qtvs is_dep bound dvs ty
+collect_cand_qtvs orig_ty is_dep bound dvs ty
= go dvs ty
where
is_bound tv = tv `elemVarSet` bound
@@ -1292,8 +1297,8 @@ collect_cand_qtvs is_dep bound dvs ty
go dv (FunTy _ arg res) = foldlM go dv [arg, res]
go dv (LitTy {}) = return dv
go dv (CastTy ty co) = do dv1 <- go dv ty
- collect_cand_qtvs_co bound dv1 co
- go dv (CoercionTy co) = collect_cand_qtvs_co bound dv co
+ collect_cand_qtvs_co orig_ty bound dv1 co
+ go dv (CoercionTy co) = collect_cand_qtvs_co orig_ty bound dv co
go dv (TyVarTy tv)
| is_bound tv = return dv
@@ -1303,8 +1308,8 @@ collect_cand_qtvs is_dep bound dvs ty
Nothing -> go_tv dv tv }
go dv (ForAllTy (Bndr tv _) ty)
- = do { dv1 <- collect_cand_qtvs True bound dv (tyVarKind tv)
- ; collect_cand_qtvs is_dep (bound `extendVarSet` tv) dv1 ty }
+ = do { dv1 <- collect_cand_qtvs orig_ty True bound dv (tyVarKind tv)
+ ; collect_cand_qtvs orig_ty is_dep (bound `extendVarSet` tv) dv1 ty }
-----------------
go_tv dv@(DV { dv_kvs = kvs, dv_tvs = tvs }) tv
@@ -1322,25 +1327,23 @@ collect_cand_qtvs is_dep bound dvs ty
-- (#15795) and to make the naughty check
-- (which comes next) works correctly
+ ; let tv_kind_vars = tyCoVarsOfType tv_kind
; cur_lvl <- getTcLevel
; if | tcTyVarLevel tv <= cur_lvl
-> return dv -- this variable is from an outer context; skip
-- See Note [Use level numbers ofor quantification]
- | intersectsVarSet bound (tyCoVarsOfType tv_kind)
+ | intersectsVarSet bound tv_kind_vars
-- the tyvar must not be from an outer context, but we have
-- already checked for this.
-- See Note [Naughty quantification candidates]
- -> do { traceTc "Zapping naughty quantifier" $
+ -> do { traceTc "Naughty quantifier" $
vcat [ ppr tv <+> dcolon <+> ppr tv_kind
, text "bound:" <+> pprTyVars (nonDetEltsUniqSet bound)
- , text "fvs:" <+> pprTyVars (nonDetEltsUniqSet $
- tyCoVarsOfType tv_kind) ]
+ , text "fvs:" <+> pprTyVars (nonDetEltsUniqSet tv_kind_vars) ]
- ; writeMetaTyVar tv (anyTypeOfKind tv_kind)
-
- -- See Note [Recurring into kinds for candidateQTyVars]
- ; collect_cand_qtvs True bound dv tv_kind }
+ ; let escapees = intersectVarSet bound tv_kind_vars
+ ; naughtyQuantification orig_ty tv escapees }
| otherwise
-> do { let tv' = tv `setTyVarKind` tv_kind
@@ -1349,15 +1352,16 @@ collect_cand_qtvs is_dep bound dvs ty
-- See Note [Order of accumulation]
-- See Note [Recurring into kinds for candidateQTyVars]
- ; collect_cand_qtvs True bound dv' tv_kind } }
+ ; collect_cand_qtvs orig_ty True bound dv' tv_kind } }
-collect_cand_qtvs_co :: VarSet -- bound variables
+collect_cand_qtvs_co :: TcType -- original type at top of recursion; for errors
+ -> VarSet -- bound variables
-> CandidatesQTvs -> Coercion
-> TcM CandidatesQTvs
-collect_cand_qtvs_co bound = go_co
+collect_cand_qtvs_co orig_ty bound = go_co
where
- go_co dv (Refl ty) = collect_cand_qtvs True bound dv ty
- go_co dv (GRefl _ ty mco) = do dv1 <- collect_cand_qtvs True bound dv ty
+ go_co dv (Refl ty) = collect_cand_qtvs orig_ty True bound dv ty
+ go_co dv (GRefl _ ty mco) = do dv1 <- collect_cand_qtvs orig_ty True bound dv ty
go_mco dv1 mco
go_co dv (TyConAppCo _ _ cos) = foldlM go_co dv cos
go_co dv (AppCo co1 co2) = foldlM go_co dv [co1, co2]
@@ -1365,8 +1369,8 @@ collect_cand_qtvs_co bound = go_co
go_co dv (AxiomInstCo _ _ cos) = foldlM go_co dv cos
go_co dv (AxiomRuleCo _ cos) = foldlM go_co dv cos
go_co dv (UnivCo prov _ t1 t2) = do dv1 <- go_prov dv prov
- dv2 <- collect_cand_qtvs True bound dv1 t1
- collect_cand_qtvs True bound dv2 t2
+ dv2 <- collect_cand_qtvs orig_ty True bound dv1 t1
+ collect_cand_qtvs orig_ty True bound dv2 t2
go_co dv (SymCo co) = go_co dv co
go_co dv (TransCo co1 co2) = foldlM go_co dv [co1, co2]
go_co dv (NthCo _ _ co) = go_co dv co
@@ -1385,7 +1389,7 @@ collect_cand_qtvs_co bound = go_co
go_co dv (ForAllCo tcv kind_co co)
= do { dv1 <- go_co dv kind_co
- ; collect_cand_qtvs_co (bound `extendVarSet` tcv) dv1 co }
+ ; collect_cand_qtvs_co orig_ty (bound `extendVarSet` tcv) dv1 co }
go_mco dv MRefl = return dv
go_mco dv (MCo co) = go_co dv co
@@ -1401,7 +1405,7 @@ collect_cand_qtvs_co bound = go_co
| cv `elemVarSet` cvs = return dv
-- See Note [Recurring into kinds for candidateQTyVars]
- | otherwise = collect_cand_qtvs True bound
+ | otherwise = collect_cand_qtvs orig_ty True bound
(dv { dv_cvs = cvs `extendVarSet` cv })
(idType cv)
@@ -2305,8 +2309,8 @@ tidySigSkol env cx ty tv_prs
%************************************************************************
%* *
Levity polymorphism checks
-* *
-************************************************************************
+* *
+*************************************************************************
See Note [Levity polymorphism checking] in DsMonad
@@ -2351,3 +2355,48 @@ formatLevPolyErr ty
where
(tidy_env, tidy_ty) = tidyOpenType emptyTidyEnv ty
tidy_ki = tidyType tidy_env (tcTypeKind ty)
+
+{-
+%************************************************************************
+%* *
+ Error messages
+* *
+*************************************************************************
+
+-}
+
+-- See Note [Naughty quantification candidates]
+naughtyQuantification :: TcType -- original type user wanted to quantify
+ -> TcTyVar -- naughty var
+ -> TyVarSet -- skolems that would escape
+ -> TcM a
+naughtyQuantification orig_ty tv escapees
+ = do { orig_ty1 <- zonkTcType orig_ty -- in case it's not zonked
+
+ ; escapees' <- mapM zonkTcTyVarToTyVar $
+ nonDetEltsUniqSet escapees
+ -- we'll just be printing, so no harmful non-determinism
+
+ ; let fvs = tyCoVarsOfTypeWellScoped orig_ty1
+ env0 = tidyFreeTyCoVars emptyTidyEnv fvs
+ env = env0 `delTidyEnvList` escapees'
+ -- this avoids gratuitous renaming of the escaped
+ -- variables; very confusing to users!
+
+ orig_ty' = tidyType env orig_ty1
+ ppr_tidied = pprTyVars . map (tidyTyCoVarOcc env)
+ doc = pprWithExplicitKindsWhen True $
+ vcat [ sep [ text "Cannot generalise type; skolem" <> plural escapees'
+ , quotes $ ppr_tidied escapees'
+ , text "would escape" <+> itsOrTheir escapees' <+> text "scope"
+ ]
+ , sep [ text "if I tried to quantify"
+ , ppr_tidied [tv]
+ , text "in this type:"
+ ]
+ , nest 2 (pprTidiedType orig_ty')
+ , text "(Indeed, I sometimes struggle even printing this correctly,"
+ , text " due to its ill-scoped nature.)"
+ ]
+
+ ; failWithTcM (env, doc) }
diff --git a/compiler/typecheck/TcPatSyn.hs b/compiler/typecheck/TcPatSyn.hs
index 1a6d3ea1e4..6984d06907 100644
--- a/compiler/typecheck/TcPatSyn.hs
+++ b/compiler/typecheck/TcPatSyn.hs
@@ -237,7 +237,7 @@ dependentArgErr (arg, bad_cos)
~~-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
data AST a = Sym [a]
- class Prj s where { prj :: [a] -> Maybe (s a)
+ class Prj s where { prj :: [a] -> Maybe (s a) }
pattern P x <= Sym (prj -> Just x)
Here we get a matcher with this type
@@ -261,7 +261,7 @@ mentions the existentials. We can conveniently do that by making the
forall ex_tvs. arg_ty
After that, Note [Naughty quantification candidates] in TcMType takes
-over, and zonks any such naughty variables to Any.
+over and errors.
Note [Remove redundant provided dicts]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/compiler/types/TyCoPpr.hs b/compiler/types/TyCoPpr.hs
index fbbdfbd7a0..f7a768210b 100644
--- a/compiler/types/TyCoPpr.hs
+++ b/compiler/types/TyCoPpr.hs
@@ -5,7 +5,7 @@ module TyCoPpr
PprPrec(..), topPrec, sigPrec, opPrec, funPrec, appPrec, maybeParen,
-- * Pretty-printing types
- pprType, pprParendType, pprPrecType, pprPrecTypeX,
+ pprType, pprParendType, pprTidiedType, pprPrecType, pprPrecTypeX,
pprTypeApp, pprTCvBndr, pprTCvBndrs,
pprSigmaType,
pprTheta, pprParendTheta, pprForAll, pprUserForAll,
@@ -81,10 +81,13 @@ See Note [Precedence in types] in BasicTypes.
-- See Note [Pretty printing via Iface syntax] in PprTyThing
--------------------------------------------------------
-pprType, pprParendType :: Type -> SDoc
+pprType, pprParendType, pprTidiedType :: Type -> SDoc
pprType = pprPrecType topPrec
pprParendType = pprPrecType appPrec
+-- already pre-tidied
+pprTidiedType = pprIfaceType . toIfaceTypeX emptyVarSet
+
pprPrecType :: PprPrec -> Type -> SDoc
pprPrecType = pprPrecTypeX emptyTidyEnv
diff --git a/compiler/types/TyCoTidy.hs b/compiler/types/TyCoTidy.hs
index f4639ca303..b6f87c2230 100644
--- a/compiler/types/TyCoTidy.hs
+++ b/compiler/types/TyCoTidy.hs
@@ -95,8 +95,8 @@ tidyTyCoVarBinders tidy_env tvbs
tidyFreeTyCoVars :: TidyEnv -> [TyCoVar] -> TidyEnv
-- ^ Add the free 'TyVar's to the env in tidy form,
-- so that we can tidy the type they are free in
-tidyFreeTyCoVars (full_occ_env, var_env) tyvars
- = fst (tidyOpenTyCoVars (full_occ_env, var_env) tyvars)
+tidyFreeTyCoVars tidy_env tyvars
+ = fst (tidyOpenTyCoVars tidy_env tyvars)
---------------
tidyOpenTyCoVars :: TidyEnv -> [TyCoVar] -> (TidyEnv, [TyCoVar])
@@ -232,5 +232,3 @@ tidyCo env@(_, subst) co
tidyCos :: TidyEnv -> [Coercion] -> [Coercion]
tidyCos env = map (tidyCo env)
-
-
diff --git a/compiler/utils/Outputable.hs b/compiler/utils/Outputable.hs
index d81c754866..02805c6c7c 100644
--- a/compiler/utils/Outputable.hs
+++ b/compiler/utils/Outputable.hs
@@ -34,7 +34,7 @@ module Outputable (
sep, cat,
fsep, fcat,
hang, hangNotEmpty, punctuate, ppWhen, ppUnless,
- speakNth, speakN, speakNOf, plural, isOrAre, doOrDoes,
+ speakNth, speakN, speakNOf, plural, isOrAre, doOrDoes, itsOrTheir,
unicodeSyntax,
coloured, keyword,
@@ -1160,6 +1160,15 @@ doOrDoes :: [a] -> SDoc
doOrDoes [_] = text "does"
doOrDoes _ = text "do"
+-- | Determines the form of possessive appropriate for the length of a list:
+--
+-- > itsOrTheir [x] = text "its"
+-- > itsOrTheir [x,y] = text "their"
+-- > itsOrTheir [] = text "their" -- probably avoid this
+itsOrTheir :: [a] -> SDoc
+itsOrTheir [_] = text "its"
+itsOrTheir _ = text "their"
+
{-
************************************************************************
* *