summaryrefslogtreecommitdiff
path: root/compiler/typecheck
diff options
context:
space:
mode:
authorSimon Peyton Jones <simonpj@microsoft.com>2017-03-10 11:46:50 +0000
committerSimon Peyton Jones <simonpj@microsoft.com>2017-03-10 16:05:21 +0000
commit48d1866e9051e52b80c9c88547bd66d66483f1d5 (patch)
tree72047939e1bad05062f7518db97b6c999d32c948 /compiler/typecheck
parent7e96526ac2ef5987ecb03217d3d616b6281c1441 (diff)
downloadhaskell-48d1866e9051e52b80c9c88547bd66d66483f1d5.tar.gz
Improve error messages for skolems
In error messages like this • Couldn't match type ‘c’ with ‘f0 (a -> b)’ ‘c’ is a rigid type variable bound by the type signature for: f :: ((a -> b) -> b) -> forall c. c -> a we need to take case both to actually show that 'forall c', and to make sure that its name lines with the 'c' in the error message. This has been shaky for some time, and this commit puts it on solid ground. See TcRnTypes: Note [SigSkol SkolemInfo] The main changes are * SigSkol gets an extra field that records the way in which the type signature was skolemised. * The type in SigSkol is now the /un/-skolemised version * pprSkolemInfo uses the info to make the tidy type line up nicely Lots of error message wibbles!
Diffstat (limited to 'compiler/typecheck')
-rw-r--r--compiler/typecheck/Inst.hs52
-rw-r--r--compiler/typecheck/TcBinds.hs4
-rw-r--r--compiler/typecheck/TcErrors.hs19
-rw-r--r--compiler/typecheck/TcExpr.hs4
-rw-r--r--compiler/typecheck/TcMType.hs58
-rw-r--r--compiler/typecheck/TcRnDriver.hs2
-rw-r--r--compiler/typecheck/TcRnTypes.hs45
-rw-r--r--compiler/typecheck/TcSimplify.hs2
-rw-r--r--compiler/typecheck/TcUnify.hs17
9 files changed, 130 insertions, 73 deletions
diff --git a/compiler/typecheck/Inst.hs b/compiler/typecheck/Inst.hs
index 75b17ef039..eff8c5f51b 100644
--- a/compiler/typecheck/Inst.hs
+++ b/compiler/typecheck/Inst.hs
@@ -54,7 +54,7 @@ import Class( Class )
import MkId( mkDictFunId )
import Id
import Name
-import Var ( EvVar, mkTyVar, TyVarBndr(..) )
+import Var ( EvVar, mkTyVar, tyVarName, TyVarBndr(..) )
import DataCon
import TyCon
import VarEnv
@@ -129,31 +129,37 @@ ToDo: this eta-abstraction plays fast and loose with termination,
fix this
-}
-deeplySkolemise
- :: TcSigmaType
- -> TcM ( HsWrapper
- , [TyVar] -- all skolemised variables
- , [EvVar] -- all "given"s
- , TcRhoType)
+deeplySkolemise :: TcSigmaType
+ -> TcM ( HsWrapper
+ , [(Name,TyVar)] -- All skolemised variables
+ , [EvVar] -- All "given"s
+ , TcRhoType )
deeplySkolemise ty
- | Just (arg_tys, tvs, theta, ty') <- tcDeepSplitSigmaTy_maybe ty
- = do { ids1 <- newSysLocalIds (fsLit "dk") arg_tys
- ; (subst, tvs1) <- tcInstSkolTyVars tvs
- ; ev_vars1 <- newEvVars (substThetaUnchecked subst theta)
- ; (wrap, tvs2, ev_vars2, rho) <-
- deeplySkolemise (substTyAddInScope subst ty')
- ; return ( mkWpLams ids1
- <.> mkWpTyLams tvs1
- <.> mkWpLams ev_vars1
- <.> wrap
- <.> mkWpEvVarApps ids1
- , tvs1 ++ tvs2
- , ev_vars1 ++ ev_vars2
- , mkFunTys arg_tys rho ) }
+ = go init_subst ty
+ where
+ init_subst = mkEmptyTCvSubst (mkInScopeSet (tyCoVarsOfType ty))
+
+ go subst ty
+ | Just (arg_tys, tvs, theta, ty') <- tcDeepSplitSigmaTy_maybe ty
+ = do { let arg_tys' = substTys subst arg_tys
+ ; ids1 <- newSysLocalIds (fsLit "dk") arg_tys'
+ ; (subst', tvs1) <- tcInstSkolTyVarsX subst tvs
+ ; ev_vars1 <- newEvVars (substTheta subst' theta)
+ ; (wrap, tvs_prs2, ev_vars2, rho) <- go subst' ty'
+ ; let tv_prs1 = map tyVarName tvs `zip` tvs1
+ ; return ( mkWpLams ids1
+ <.> mkWpTyLams tvs1
+ <.> mkWpLams ev_vars1
+ <.> wrap
+ <.> mkWpEvVarApps ids1
+ , tv_prs1 ++ tvs_prs2
+ , ev_vars1 ++ ev_vars2
+ , mkFunTys arg_tys' rho ) }
- | otherwise
- = return (idHsWrapper, [], [], ty)
+ | otherwise
+ = return (idHsWrapper, [], [], substTy subst ty)
+ -- substTy is a quick no-op on an empty substitution
-- | Instantiate all outer type variables
-- and any context. Never looks through arrows.
diff --git a/compiler/typecheck/TcBinds.hs b/compiler/typecheck/TcBinds.hs
index 201da00504..942bf96988 100644
--- a/compiler/typecheck/TcBinds.hs
+++ b/compiler/typecheck/TcBinds.hs
@@ -687,13 +687,13 @@ tcPolyCheck prag_fn
, fun_matches = matches }))
= setSrcSpan sig_loc $
do { traceTc "tcPolyCheck" (ppr poly_id $$ ppr sig_loc)
- ; (tv_prs, theta, tau) <- tcInstType (tcInstSigTyVars sig_loc) poly_id
+ ; (tv_prs, theta, tau) <- tcInstType tcInstSkolTyVars poly_id
-- See Note [Instantiate sig with fresh variables]
; mono_name <- newNameAt (nameOccName name) nm_loc
; ev_vars <- newEvVars theta
; let mono_id = mkLocalId mono_name tau
- skol_info = SigSkol ctxt (mkPhiTy theta tau)
+ skol_info = SigSkol ctxt (idType poly_id) tv_prs
skol_tvs = map snd tv_prs
; (ev_binds, (co_fn, matches'))
diff --git a/compiler/typecheck/TcErrors.hs b/compiler/typecheck/TcErrors.hs
index d47ecc6315..4bcf67316b 100644
--- a/compiler/typecheck/TcErrors.hs
+++ b/compiler/typecheck/TcErrors.hs
@@ -2329,7 +2329,7 @@ ctxtFixes has_ambig_tvs pred implics
, isTyVarClassPred pred
, (skol:skols) <- usefulContext implics pred
, let what | null skols
- , SigSkol (PatSynCtxt {}) _ <- skol
+ , SigSkol (PatSynCtxt {}) _ _ <- skol
= text "\"required\""
| otherwise
= empty
@@ -2351,8 +2351,8 @@ discardProvCtxtGivens orig givens -- See Note [discardProvCtxtGivens]
| otherwise
= givens
where
- discard n (Implic { ic_info = SigSkol (PatSynCtxt n') _ }) = n == n'
- discard _ _ = False
+ discard n (Implic { ic_info = SigSkol (PatSynCtxt n') _ _ }) = n == n'
+ discard _ _ = False
usefulContext :: [Implication] -> PredType -> [SkolemInfo]
-- usefulContext picks out the implications whose context
@@ -2375,8 +2375,8 @@ usefulContext implics pred
| implausible_info (ic_info ic) = True
| otherwise = False
- implausible_info (SigSkol (InfSigCtxt {}) _) = True
- implausible_info _ = False
+ implausible_info (SigSkol (InfSigCtxt {}) _ _) = True
+ implausible_info _ = False
-- Do not suggest adding constraints to an *inferred* type signature
{- Note [Report candidate instances]
@@ -2600,13 +2600,10 @@ mkAmbigMsg prepend_msg ct
pprSkol :: [Implication] -> TcTyVar -> SDoc
pprSkol implics tv
= case skol_info of
- UnkSkol -> quotes (ppr tv) <+> text "is an unknown type variable"
- SigSkol ctxt ty -> ppr_rigid (pprSigSkolInfo ctxt
- (mkSpecForAllTys skol_tvs ty))
- _ -> ppr_rigid (pprSkolInfo skol_info)
+ UnkSkol -> quotes (ppr tv) <+> text "is an unknown type variable"
+ _ -> ppr_rigid (pprSkolInfo skol_info)
where
- Implic { ic_skols = skol_tvs, ic_info = skol_info }
- = getSkolemInfo implics tv
+ Implic { ic_info = skol_info } = getSkolemInfo implics tv
ppr_rigid pp_info
= hang (quotes (ppr tv) <+> text "is a rigid type variable bound by")
2 (sep [ pp_info
diff --git a/compiler/typecheck/TcExpr.hs b/compiler/typecheck/TcExpr.hs
index 6c9b5a2b1c..3bc6457e6e 100644
--- a/compiler/typecheck/TcExpr.hs
+++ b/compiler/typecheck/TcExpr.hs
@@ -1490,9 +1490,9 @@ in the other order, the extra signature in f2 is reqd.
tcExprSig :: LHsExpr Name -> TcIdSigInfo -> TcM (LHsExpr TcId, TcType)
tcExprSig expr (CompleteSig { sig_bndr = poly_id, sig_loc = loc })
= setSrcSpan loc $ -- Sets the location for the implication constraint
- do { (tv_prs, theta, tau) <- tcInstType (tcInstSigTyVars loc) poly_id
+ do { (tv_prs, theta, tau) <- tcInstType tcInstSkolTyVars poly_id
; given <- newEvVars theta
- ; let skol_info = SigSkol ExprSigCtxt (mkPhiTy theta tau)
+ ; let skol_info = SigSkol ExprSigCtxt (idType poly_id) tv_prs
skol_tvs = map snd tv_prs
; (ev_binds, expr') <- checkConstraints skol_info skol_tvs given $
tcExtendTyVarEnv2 tv_prs $
diff --git a/compiler/typecheck/TcMType.hs b/compiler/typecheck/TcMType.hs
index b0a9d4966d..2abc800a37 100644
--- a/compiler/typecheck/TcMType.hs
+++ b/compiler/typecheck/TcMType.hs
@@ -57,8 +57,8 @@ module TcMType (
newMetaSigTyVars, newMetaSigTyVarX,
newSigTyVar, newWildCardX,
tcInstType,
- tcInstSkolTyVars, tcInstSuperSkolTyVarsX,
- tcInstSigTyVars,
+ tcInstSkolTyVars,tcInstSkolTyVarsX,
+ tcInstSuperSkolTyVarsX,
tcSkolDFunType, tcSuperSkolTyVars,
instSkolTyCoVars, freshenTyVarBndrs, freshenCoVarBndrsX,
@@ -108,6 +108,7 @@ import VarSet
import TysWiredIn
import TysPrim
import VarEnv
+import NameEnv
import PrelNames
import Util
import Outputable
@@ -478,7 +479,10 @@ tcSuperSkolTyVar subst tv
-- skolems are non-overlappable; see Note [Overlap and deriving]
-- for an example where this matters.
tcInstSkolTyVars :: [TyVar] -> TcM (TCvSubst, [TcTyVar])
-tcInstSkolTyVars = tcInstSkolTyVars' False emptyTCvSubst
+tcInstSkolTyVars = tcInstSkolTyVarsX emptyTCvSubst
+
+tcInstSkolTyVarsX :: TCvSubst -> [TyVar] -> TcM (TCvSubst, [TcTyVar])
+tcInstSkolTyVarsX = tcInstSkolTyVars' False
tcInstSuperSkolTyVars :: [TyVar] -> TcM (TCvSubst, [TcTyVar])
tcInstSuperSkolTyVars = tcInstSuperSkolTyVarsX emptyTCvSubst
@@ -503,12 +507,6 @@ mkTcSkolTyVar lvl loc overlappable
details = SkolemTv (pushTcLevel lvl) overlappable
-- NB: skolems bump the level
-tcInstSigTyVars :: SrcSpan -> [TyVar]
- -> TcM (TCvSubst, [TcTyVar])
-tcInstSigTyVars loc tvs
- = do { lvl <- getTcLevel
- ; instSkolTyCoVars (mkTcSkolTyVar lvl loc False) tvs }
-
------------------
freshenTyVarBndrs :: [TyVar] -> TcRnIf gbl lcl (TCvSubst, [TyVar])
-- ^ Give fresh uniques to a bunch of TyVars, but they stay
@@ -1417,8 +1415,8 @@ zonkCtEvidence ctev@(CtDerived { ctev_pred = pred })
; return (ctev { ctev_pred = pred' }) }
zonkSkolemInfo :: SkolemInfo -> TcM SkolemInfo
-zonkSkolemInfo (SigSkol cx ty) = do { ty' <- zonkTcType ty
- ; return (SigSkol cx ty') }
+zonkSkolemInfo (SigSkol cx ty tv_prs) = do { ty' <- zonkTcType ty
+ ; return (SigSkol cx ty' tv_prs) }
zonkSkolemInfo (InferSkol ntys) = do { ntys' <- mapM do_one ntys
; return (InferSkol ntys') }
where
@@ -1606,11 +1604,39 @@ tidyEvVar env var = setVarType var (tidyType env (varType var))
----------------
tidySkolemInfo :: TidyEnv -> SkolemInfo -> SkolemInfo
-tidySkolemInfo env (DerivSkol ty) = DerivSkol (tidyType env ty)
-tidySkolemInfo env (SigSkol cx ty) = SigSkol cx (tidyType env ty)
-tidySkolemInfo env (InferSkol ids) = InferSkol (mapSnd (tidyType env) ids)
-tidySkolemInfo env (UnifyForAllSkol ty) = UnifyForAllSkol (tidyType env ty)
-tidySkolemInfo _ info = info
+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
+
+tidySigSkol :: TidyEnv -> UserTypeCtxt
+ -> TcType -> [(Name,TcTyVar)] -> SkolemInfo
+-- We need to take special care when tidying SigSkol
+-- See Note [SigSkol SkolemInfo] in TcRnTypes
+tidySigSkol env cx ty tv_prs
+ = SigSkol cx (tidy_ty env ty) tv_prs'
+ where
+ tv_prs' = mapSnd (tidyTyVarOcc env) tv_prs
+ inst_env = mkNameEnv tv_prs'
+
+ tidy_ty env (ForAllTy (TvBndr tv vis) ty)
+ = ForAllTy (TvBndr tv' vis) (tidy_ty env' ty)
+ where
+ (env', tv') = tidy_tv_bndr env tv
+
+ tidy_ty env (FunTy arg res)
+ = FunTy (tidyType env arg) (tidy_ty env res)
+
+ tidy_ty env ty = tidyType env ty
+
+ tidy_tv_bndr :: TidyEnv -> TyVar -> (TidyEnv, TyVar)
+ tidy_tv_bndr env@(occ_env, subst) tv
+ | Just tv' <- lookupNameEnv inst_env (tyVarName tv)
+ = ((occ_env, extendVarEnv subst tv tv'), tv')
+
+ | otherwise
+ = tidyTyCoVarBndr env tv
-------------------------------------------------------------------------
{-
diff --git a/compiler/typecheck/TcRnDriver.hs b/compiler/typecheck/TcRnDriver.hs
index b6af02b129..cdf327802f 100644
--- a/compiler/typecheck/TcRnDriver.hs
+++ b/compiler/typecheck/TcRnDriver.hs
@@ -1661,7 +1661,7 @@ check_main dflags tcg_env explicit_mod_hdr
; ioTyCon <- tcLookupTyCon ioTyConName
; res_ty <- newFlexiTyVarTy liftedTypeKind
; let io_ty = mkTyConApp ioTyCon [res_ty]
- skol_info = SigSkol (FunSigCtxt main_name False) io_ty
+ skol_info = SigSkol (FunSigCtxt main_name False) io_ty []
; (ev_binds, main_expr)
<- checkConstraints skol_info [] [] $
addErrCtxt mainCtxt $
diff --git a/compiler/typecheck/TcRnTypes.hs b/compiler/typecheck/TcRnTypes.hs
index 48c9c3577f..b6443340fd 100644
--- a/compiler/typecheck/TcRnTypes.hs
+++ b/compiler/typecheck/TcRnTypes.hs
@@ -2841,9 +2841,14 @@ pushErrCtxtSameOrigin err loc@(CtLoc { ctl_env = lcl })
-- a) type variables are skolemised
-- b) an implication constraint is generated
data SkolemInfo
- = SigSkol UserTypeCtxt -- A skolem that is created by instantiating
- TcType -- a programmer-supplied type signature
- -- Location of the binding site is on the TyVar
+ = SigSkol -- A skolem that is created by instantiating
+ -- a programmer-supplied type signature
+ -- Location of the binding site is on the TyVar
+ -- See Note [SigSkol SkolemInfo]
+ UserTypeCtxt -- What sort of signature
+ TcType -- Original type signature (before skolemisation)
+ [(Name,TcTyVar)] -- Maps the original name of the skolemised tyvar
+ -- to its instantiated version
| ClsSkol Class -- Bound at a class decl
@@ -2898,9 +2903,9 @@ termEvidenceAllowed _ = True
pprSkolInfo :: SkolemInfo -> SDoc
-- Complete the sentence "is a rigid type variable bound by..."
-pprSkolInfo (SigSkol ctxt ty) = pprSigSkolInfo ctxt ty
+pprSkolInfo (SigSkol cx ty _) = pprSigSkolInfo cx ty
pprSkolInfo (IPSkol ips) = text "the implicit-parameter binding" <> plural ips <+> text "for"
- <+> pprWithCommas ppr ips
+ <+> pprWithCommas ppr ips
pprSkolInfo (ClsSkol cls) = text "the class declaration for" <+> quotes (ppr cls)
pprSkolInfo (DerivSkol pred) = text "the deriving clause for" <+> quotes (ppr pred)
pprSkolInfo InstSkol = text "the instance declaration"
@@ -2923,6 +2928,7 @@ pprSkolInfo (UnifyForAllSkol ty) = text "the type" <+> ppr ty
pprSkolInfo UnkSkol = WARN( True, text "pprSkolInfo: UnkSkol" ) text "UnkSkol"
pprSigSkolInfo :: UserTypeCtxt -> TcType -> SDoc
+-- The type is already tidied
pprSigSkolInfo ctxt ty
= case ctxt of
FunSigCtxt f _ -> vcat [ text "the type signature for:"
@@ -2948,13 +2954,38 @@ pprPatSkolInfo (PatSynCon ps)
{- Note [Skolem info for pattern synonyms]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For pattern synonym SkolemInfo we have
- SigSkol (PatSynCtxt p) ty
+ SigSkol (PatSynCtxt p) ty _
but the type 'ty' is not very helpful. The full pattern-synonym type
-is has the provided and required pieces, which it is inconvenient to
+has the provided and required pieces, which it is inconvenient to
record and display here. So we simply don't display the type at all,
contenting outselves with just the name of the pattern synonym, which
is fine. We could do more, but it doesn't seem worth it.
+Note [SigSkol SkolemInfo]
+~~~~~~~~~~~~~~~~~~~~~~~~~
+Suppose we (deeply) skolemise a type
+ f :: forall a. a -> forall b. b -> a
+Then we'll instantiate [a :-> a', b :-> b'], and with the instantiated
+ a' -> b' -> a.
+But when, in an error message, we report that "b is a rigid type
+variable bound by the type signature for f", we want to show the foralls
+in the right place. So we proceed as follows:
+
+* In SigSkol we record
+ - the original signature forall a. a -> forall b. b -> a
+ - the instantiation mapping [a :-> a', b :-> b']
+
+* Then when tidying in TcMType.tidySkolemInfo, we first tidy a' to
+ whatever it tidies to, say a''; and then we walk over the type
+ replacing the binder a by the tidied version a'', to give
+ forall a''. a'' -> forall b''. b'' -> a''
+ We need to do this under function arrows, to match what deeplySkolemise
+ does.
+
+* Typically a'' will have a nice pretty name like "a", but the point is
+ that the foral-bound variables of the signature we report line up with
+ the instantiated skolems lying around in other types.
+
************************************************************************
* *
diff --git a/compiler/typecheck/TcSimplify.hs b/compiler/typecheck/TcSimplify.hs
index 4402d8f4de..e2598b5214 100644
--- a/compiler/typecheck/TcSimplify.hs
+++ b/compiler/typecheck/TcSimplify.hs
@@ -1391,7 +1391,7 @@ setImplicationStatus implic@(Implic { ic_binds = ev_binds_var
= Left ic
warnRedundantGivens :: SkolemInfo -> Bool
-warnRedundantGivens (SigSkol ctxt _)
+warnRedundantGivens (SigSkol ctxt _ _)
= case ctxt of
FunSigCtxt _ warn_redundant -> warn_redundant
ExprSigCtxt -> True
diff --git a/compiler/typecheck/TcUnify.hs b/compiler/typecheck/TcUnify.hs
index 75732c6f05..acfb0b7a0c 100644
--- a/compiler/typecheck/TcUnify.hs
+++ b/compiler/typecheck/TcUnify.hs
@@ -719,15 +719,13 @@ tc_sub_type_ds eq_orig inst_orig ctxt ty_actual ty_expected
| not (isPredTy act_arg)
, not (isPredTy exp_arg)
= -- See Note [Co/contra-variance of subsumption checking]
- do { res_wrap <- tc_sub_type_ds eq_orig inst_orig ctxt act_res exp_res
- ; arg_wrap
- <- tc_sub_tc_type eq_orig (GivenOrigin
- (SigSkol GenSigCtxt exp_arg))
- ctxt exp_arg act_arg
+ do { res_wrap <- tc_sub_type_ds eq_orig inst_orig ctxt act_res exp_res
+ ; arg_wrap <- tc_sub_tc_type eq_orig given_orig ctxt exp_arg act_arg
; return (mkWpFun arg_wrap res_wrap exp_arg exp_res doc) }
-- arg_wrap :: exp_arg ~> act_arg
-- res_wrap :: act-res ~> exp_res
where
+ given_orig = GivenOrigin (SigSkol GenSigCtxt exp_arg [])
doc = text "When checking that" <+> quotes (ppr ty_actual) <+>
text "is more polymorphic than" <+> quotes (ppr ty_expected)
@@ -1040,14 +1038,14 @@ tcSkolemise ctxt expected_ty thing_inside
-- We expect expected_ty to be a forall-type
-- If not, the call is a no-op
= do { traceTc "tcSkolemise" Outputable.empty
- ; (wrap, tvs', given, rho') <- deeplySkolemise expected_ty
+ ; (wrap, tv_prs, given, rho') <- deeplySkolemise expected_ty
; lvl <- getTcLevel
; when debugIsOn $
traceTc "tcSkolemise" $ vcat [
ppr lvl,
text "expected_ty" <+> ppr expected_ty,
- text "inst tyvars" <+> ppr tvs',
+ text "inst tyvars" <+> ppr tv_prs,
text "given" <+> ppr given,
text "inst type" <+> ppr rho' ]
@@ -1064,9 +1062,8 @@ tcSkolemise ctxt expected_ty thing_inside
-- TcTyVars, all this is handled automatically with no need for
-- extra faffing around
- -- Use the *instantiated* type in the SkolemInfo
- -- so that the names of displayed type variables line up
- ; let skol_info = SigSkol ctxt (mkFunTys (map varType given) rho')
+ ; let tvs' = map snd tv_prs
+ skol_info = SigSkol ctxt expected_ty tv_prs
; (ev_binds, result) <- checkConstraints skol_info tvs' given $
thing_inside tvs' rho'