3 reportUnsolved, reportUnsolvedImplication, reportUnsolvedDeriv,
4 reportUnsolvedWantedEvVars, warnDefaulting,
5 unifyCtxt, typeExtraInfoMsg,
6 kindErrorTcS, misMatchErrorTcS, flattenForAllErrorTcS,
7 occursCheckErrorTcS, solverDepthErrorTcS
10 #include "HsVersions.h"
23 import HsExpr ( pprMatchContext )
29 import ListSetOps( equivClasses )
35 import StaticFlags( opt_PprStyle_Debug )
36 import Data.List( partition )
37 import Control.Monad( unless )
40 %************************************************************************
42 \section{Errors and contexts}
44 %************************************************************************
46 ToDo: for these error messages, should we note the location as coming
47 from the insts, or just whatever seems to be around in the monad just
51 reportUnsolved :: (CanonicalCts, Bag Implication) -> TcM ()
52 reportUnsolved (unsolved_flats, unsolved_implics)
56 = do { env0 <- tcInitTidyEnv
57 ; let tidy_env = tidyFreeTyVars env0 (tyVarsOfWanteds unsolved)
58 tidy_unsolved = tidyWanteds tidy_env unsolved
59 err_ctxt = CEC { cec_encl = []
61 , cec_tidy = tidy_env }
62 ; traceTc "reportUnsolved" (ppr unsolved)
63 ; reportTidyWanteds err_ctxt tidy_unsolved }
65 unsolved = mkWantedConstraints unsolved_flats unsolved_implics
67 reportUnsolvedWantedEvVars :: Bag WantedEvVar -> TcM ()
68 reportUnsolvedWantedEvVars wanteds
72 = do { env0 <- tcInitTidyEnv
73 ; let tidy_env = tidyFreeTyVars env0 (tyVarsOfWantedEvVars wanteds)
74 tidy_unsolved = tidyWantedEvVars tidy_env wanteds
75 err_ctxt = CEC { cec_encl = []
77 , cec_tidy = tidy_env }
78 ; groupErrs (reportFlat err_ctxt) (bagToList tidy_unsolved) }
80 reportUnsolvedDeriv :: [PredType] -> WantedLoc -> TcM ()
81 reportUnsolvedDeriv unsolved loc
86 do { env0 <- tcInitTidyEnv
87 ; let tidy_env = tidyFreeTyVars env0 (tyVarsOfTheta unsolved)
88 tidy_unsolved = map (tidyPred tidy_env) unsolved
89 err_ctxt = CEC { cec_encl = []
91 , cec_tidy = tidy_env }
92 ; reportFlat err_ctxt tidy_unsolved (ctLocOrigin loc) }
94 alt_fix = vcat [ptext (sLit "Alternatively, use a standalone 'deriving instance' declaration,"),
95 nest 2 $ ptext (sLit "so you can specify the instance context yourself")]
97 reportUnsolvedImplication :: Implication -> TcM ()
98 reportUnsolvedImplication implic
99 = do { env0 <- tcInitTidyEnv
100 ; let tidy_env = tidyFreeTyVars env0 (tyVarsOfImplication implic)
101 tidy_implic = tidyImplication tidy_env implic
102 new_tidy_env = foldNameEnv add tidy_env (ic_env implic)
103 err_ctxt = CEC { cec_encl = [tidy_implic]
105 , cec_tidy = new_tidy_env }
106 ; reportTidyWanteds err_ctxt (ic_wanted tidy_implic) }
108 -- Extend the tidy env with a mapping from tyvars to the
109 -- names the user originally used. At the moment we do this
110 -- from the type env, but it might be better to record the
111 -- scoped type variable in the Implication. Urgh.
112 add (ATyVar name ty) (occ_env, var_env)
113 | Just tv <- tcGetTyVar_maybe ty
114 , not (getUnique name `elemVarEnvByKey` var_env)
115 = case tidyOccName occ_env (nameOccName name) of
116 (occ_env', occ') -> (occ_env', extendVarEnv var_env tv tv')
118 tv' = setTyVarName tv name'
119 name' = tidyNameOcc name occ'
120 add _ tidy_env = tidy_env
123 = CEC { cec_encl :: [Implication] -- Enclosing implications
125 , cec_tidy :: TidyEnv
126 , cec_extra :: SDoc -- Add this to each error message
129 reportTidyImplic :: ReportErrCtxt -> Implication -> TcM ()
130 reportTidyImplic ctxt implic
131 = reportTidyWanteds ctxt' (ic_wanted implic)
133 ctxt' = ctxt { cec_encl = implic : cec_encl ctxt }
135 reportTidyWanteds :: ReportErrCtxt -> WantedConstraints -> TcM ()
136 reportTidyWanteds ctxt unsolved
137 = do { let (flats, implics) = splitWanteds unsolved
138 (ambigs, others) = partition is_ambiguous (bagToList flats)
139 ; groupErrs (reportFlat ctxt) others
140 ; mapBagM_ (reportTidyImplic ctxt) implics
141 ; ifErrsM (return ()) $
142 -- Only report ambiguity if no other errors happened
143 -- See Note [Avoiding spurious errors]
144 reportAmbigErrs ctxt skols ambigs }
146 skols = foldr (unionVarSet . ic_skols) emptyVarSet (cec_encl ctxt)
148 -- Treat it as "ambiguous" if
149 -- (a) it is a class constraint
150 -- (b) it constrains only type variables
151 -- (else we'd prefer to report it as "no instance for...")
152 -- (c) it mentions type variables that are not skolems
153 is_ambiguous d = isTyVarClassPred pred
154 && not (tyVarsOfPred pred `subVarSet` skols)
156 pred = wantedEvVarPred d
158 reportFlat :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()
159 reportFlat ctxt flats origin
160 = do { unless (null dicts) $ reportDictErrs ctxt dicts origin
161 ; unless (null eqs) $ reportEqErrs ctxt eqs origin
162 ; unless (null ips) $ reportIPErrs ctxt ips origin
163 ; ASSERT( null others ) return () }
165 (dicts, non_dicts) = partition isClassPred flats
166 (eqs, non_eqs) = partition isEqPred non_dicts
167 (ips, others) = partition isIPPred non_eqs
169 --------------------------------------------
171 --------------------------------------------
173 groupErrs :: ([PredType] -> CtOrigin -> TcM ()) -- Deal with one group
174 -> [WantedEvVar] -- Unsolved wanteds
176 -- Group together insts with the same origin
177 -- We want to report them together in error messages
181 groupErrs report_err (wanted : wanteds)
182 = do { setCtLoc the_loc $
183 report_err the_vars (ctLocOrigin the_loc)
184 ; groupErrs report_err others }
186 the_loc = wantedEvVarLoc wanted
187 the_key = mk_key the_loc
188 the_vars = map wantedEvVarPred (wanted:friends)
189 (friends, others) = partition is_friend wanteds
190 is_friend friend = mk_key (wantedEvVarLoc friend) == the_key
192 mk_key :: WantedLoc -> (SrcSpan, String)
193 mk_key loc = (ctLocSpan loc, showSDoc (ppr (ctLocOrigin loc)))
194 -- It may seem crude to compare the error messages,
195 -- but it makes sure that we combine just what the user sees,
196 -- and it avoids need equality on InstLocs.
198 -- Add the "arising from..." part to a message about bunch of dicts
199 addArising :: CtOrigin -> SDoc -> SDoc
200 addArising orig msg = msg $$ nest 2 (pprArising orig)
202 pprWithArising :: [WantedEvVar] -> (WantedLoc, SDoc)
203 -- Print something like
204 -- (Eq a) arising from a use of x at y
205 -- (Show a) arising froma use of p at q
206 -- Also return a location for the erroe message
208 = panic "pprWithArising"
209 pprWithArising [WantedEvVar ev loc]
210 = (loc, pprEvVarTheta [ev] <+> pprArising (ctLocOrigin loc))
211 pprWithArising ev_vars
212 = (first_loc, vcat (map ppr_one ev_vars))
214 first_loc = wantedEvVarLoc (head ev_vars)
215 ppr_one (WantedEvVar v loc)
216 = parens (pprPred (evVarPred v)) <+> pprArisingAt loc
218 addErrorReport :: ReportErrCtxt -> SDoc -> TcM ()
219 addErrorReport ctxt msg = addErrTcM (cec_tidy ctxt, msg $$ cec_extra ctxt)
221 pprErrCtxtLoc :: ReportErrCtxt -> SDoc
223 = case map (ctLocOrigin . ic_loc) (cec_encl ctxt) of
224 [] -> ptext (sLit "the top level") -- Should not happen
225 (orig:origs) -> ppr_skol orig $$
226 vcat [ ptext (sLit "or") <+> ppr_skol orig | orig <- origs ]
228 ppr_skol (PatSkol dc _) = ptext (sLit "the data constructor") <+> quotes (ppr dc)
229 ppr_skol skol_info = pprSkolInfo skol_info
231 couldNotDeduce :: [EvVar] -> [PredType] -> SDoc
232 couldNotDeduce givens wanteds
233 = sep [ ptext (sLit "Could not deduce") <+> pprTheta wanteds
234 , nest 2 $ ptext (sLit "from the context")
235 <+> pprEvVarTheta givens]
237 getUserGivens :: ReportErrCtxt -> Maybe [EvVar]
238 -- Just gs => Say "could not deduce ... from gs"
239 -- Nothing => No interesting givens, say something else
240 getUserGivens (CEC {cec_encl = ctxt})
241 | null user_givens = Nothing
242 | otherwise = Just user_givens
244 givens = foldl (\gs ic -> ic_given ic ++ gs) [] ctxt
245 user_givens | opt_PprStyle_Debug = givens
246 | otherwise = filterOut isSelfDict givens
247 -- In user mode, don't show the "self-dict" given
248 -- which is only added to do co-inductive solving
249 -- Rather an awkward hack, but there we are
250 -- This is the only use of isSelfDict, so it's not in an inner loop
254 %************************************************************************
256 Implicit parameter errors
258 %************************************************************************
261 reportIPErrs :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()
262 reportIPErrs ctxt ips orig
263 = addErrorReport ctxt $ addArising orig msg
265 msg | Just givens <- getUserGivens ctxt
266 = couldNotDeduce givens ips
268 = sep [ ptext (sLit "Unbound implicit parameter") <> plural ips
269 , nest 2 (pprTheta ips) ]
273 %************************************************************************
277 %************************************************************************
280 reportEqErrs :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()
281 reportEqErrs ctxt eqs orig
282 = mapM_ report_one eqs
285 report_one (EqPred ty1 ty2)
286 = getWantedEqExtra emptyTvSubst env0 orig ty1 ty2 $ \ env1 extra ->
287 let ctxt' = ctxt { cec_tidy = env1
288 , cec_extra = cec_extra ctxt $$ extra }
289 in reportEqErr ctxt' ty1 ty2
291 = pprPanic "reportEqErrs" (ppr pred)
293 reportEqErr :: ReportErrCtxt -> TcType -> TcType -> TcM ()
294 reportEqErr ctxt ty1 ty2
295 | Just tv1 <- tcGetTyVar_maybe ty1 = reportTyVarEqErr ctxt tv1 ty2
296 | Just tv2 <- tcGetTyVar_maybe ty2 = reportTyVarEqErr ctxt tv2 ty1
297 | otherwise -- Neither side is a type variable
298 -- Since the unsolved constraint is canonical,
299 -- it must therefore be of form (F tys ~ ty)
300 = addErrorReport ctxt (msg $$ mkTyFunInfoMsg ty1 ty2)
302 msg = case getUserGivens ctxt of
303 Just givens -> couldNotDeduce givens [EqPred ty1 ty2]
304 Nothing -> misMatchMsg ty1 ty2
306 reportTyVarEqErr :: ReportErrCtxt -> TcTyVar -> TcType -> TcM ()
307 reportTyVarEqErr ctxt tv1 ty2
309 , Just tv2 <- tcGetTyVar_maybe ty2
311 = -- sk ~ alpha: swap
312 reportTyVarEqErr ctxt tv2 ty1
315 = -- sk ~ ty, where ty isn't a meta-tyvar: mis-match
316 addErrTcM (misMatchMsgWithExtras (cec_tidy ctxt) ty1 ty2)
318 -- So tv is a meta tyvar, and presumably it is
319 -- an *untouchable* meta tyvar, else it'd have been unified
320 | not (k2 `isSubKind` k1) -- Kind error
321 = addErrorReport ctxt $ (kindErrorMsg (mkTyVarTy tv1) ty2)
323 -- Check for skolem escape
324 | (implic:_) <- cec_encl ctxt -- Get the innermost context
325 , let esc_skols = varSetElems (tyVarsOfType ty2 `intersectVarSet` ic_skols implic)
326 implic_loc = ic_loc implic
327 , not (null esc_skols)
328 = setCtLoc implic_loc $ -- Override the error message location from the
329 -- place the equality arose to the implication site
330 do { (env1, env_sigs) <- findGlobals ctxt (unitVarSet tv1)
331 ; let msg = misMatchMsg ty1 ty2
332 esc_doc | isSingleton esc_skols
333 = ptext (sLit "because this skolem type variable would escape:")
335 = ptext (sLit "because these skolem type variables would escape:")
336 extra1 = vcat [ nest 2 $ esc_doc <+> pprQuotedList esc_skols
337 , sep [ (if isSingleton esc_skols
338 then ptext (sLit "This skolem is")
339 else ptext (sLit "These skolems are"))
340 <+> ptext (sLit "bound by")
341 , nest 2 $ pprSkolInfo (ctLocOrigin implic_loc) ] ]
342 ; addErrTcM (env1, msg $$ extra1 $$ mkEnvSigMsg (ppr tv1) env_sigs) }
344 -- Nastiest case: attempt to unify an untouchable variable
345 | (implic:_) <- cec_encl ctxt -- Get the innermost context
346 , let implic_loc = ic_loc implic
347 given = ic_given implic
348 = setCtLoc (ic_loc implic) $
349 do { let (env1, msg) = misMatchMsgWithExtras (cec_tidy ctxt) ty1 ty2
350 extra = vcat [ ptext (sLit "because") <+> ppr tv1 <+> ptext (sLit "is untouchable")
351 , ptext (sLit "inside the constraints") <+> pprEvVarTheta given
352 , nest 2 (ptext (sLit "bound at")
353 <+> pprSkolInfo (ctLocOrigin implic_loc)) ]
354 ; addErrTcM (env1, msg $$ extra) }
356 | otherwise -- I'm not sure how this can happen!
357 = addErrTcM (misMatchMsgWithExtras (cec_tidy ctxt) ty1 ty2)
359 is_meta1 = isMetaTyVar tv1
364 mkTyFunInfoMsg :: TcType -> TcType -> SDoc
365 -- See Note [Non-injective type functions]
366 mkTyFunInfoMsg ty1 ty2
367 | Just (tc1,_) <- tcSplitTyConApp_maybe ty1
368 , Just (tc2,_) <- tcSplitTyConApp_maybe ty2
369 , tc1 == tc2, isSynFamilyTyCon tc1
370 = ptext (sLit "NB:") <+> quotes (ppr tc1)
371 <+> ptext (sLit "is a type function") <> (pp_inj tc1)
374 pp_inj tc | isInjectiveTyCon tc = empty
375 | otherwise = ptext (sLit (", and may not be injective"))
377 misMatchMsgWithExtras :: TidyEnv -> TcType -> TcType -> (TidyEnv, SDoc)
378 -- This version is used by TcSimplify too, which doesn't track the
379 -- expected/acutal thing, so we just have ty1 ty2 here
380 -- NB: The types are already tidied
381 misMatchMsgWithExtras env ty1 ty2
382 = (env2, sep [ misMatchMsg ty1 ty2, nest 2 (extra1 $$ extra2) ])
384 (env1, extra1) = typeExtraInfoMsg env ty1
385 (env2, extra2) = typeExtraInfoMsg env1 ty2
387 misMatchMsg :: TcType -> TcType -> SDoc -- Types are already tidy
388 misMatchMsg ty1 ty2 = sep [ ptext (sLit "Couldn't match type") <+> quotes (ppr ty1)
389 , nest 15 $ ptext (sLit "with") <+> quotes (ppr ty2)]
391 kindErrorMsg :: TcType -> TcType -> SDoc -- Types are already tidy
393 = vcat [ ptext (sLit "Kind incompatibility when matching types:")
394 , nest 2 (vcat [ ppr ty1 <+> dcolon <+> ppr k1
395 , ppr ty2 <+> dcolon <+> ppr k2 ]) ]
400 typeExtraInfoMsg :: TidyEnv -> Type -> (TidyEnv, SDoc)
401 -- Shows a bit of extra info about skolem constants
402 typeExtraInfoMsg env ty
403 | Just tv <- tcGetTyVar_maybe ty
405 , isSkolemTyVar tv || isSigTyVar tv
407 , let (env1, tv1) = tidySkolemTyVar env tv
408 = (env1, pprSkolTvBinding tv1)
410 typeExtraInfoMsg env _ty = (env, empty) -- Normal case
413 unifyCtxt :: EqOrigin -> TidyEnv -> TcM (TidyEnv, SDoc)
414 unifyCtxt (UnifyOrigin { uo_actual = act_ty, uo_expected = exp_ty }) tidy_env
415 = do { act_ty' <- zonkTcType act_ty
416 ; exp_ty' <- zonkTcType exp_ty
417 ; let (env1, exp_ty'') = tidyOpenType tidy_env exp_ty'
418 (env2, act_ty'') = tidyOpenType env1 act_ty'
419 ; return (env2, mkExpectedActualMsg act_ty'' exp_ty'') }
421 mkExpectedActualMsg :: Type -> Type -> SDoc
422 mkExpectedActualMsg act_ty exp_ty
423 = vcat [ text "Expected type" <> colon <+> ppr exp_ty
424 , text " Actual type" <> colon <+> ppr act_ty ]
427 Note [Non-injective type functions]
428 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
429 It's very confusing to get a message like
430 Couldn't match expected type `Depend s'
431 against inferred type `Depend s1'
432 so mkTyFunInfoMsg adds:
433 NB: `Depend' is type function, and hence may not be injective
435 Warn of loopy local equalities that were dropped.
438 %************************************************************************
442 %************************************************************************
445 reportDictErrs :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()
446 reportDictErrs ctxt wanteds orig
447 = do { inst_envs <- tcGetInstEnvs
448 ; let (others, overlaps) = partitionWith (check_overlap inst_envs) wanteds
449 ; unless (null others) $
450 addErrorReport ctxt (mk_no_inst_err others)
451 ; mapM_ (addErrorReport ctxt) overlaps }
453 check_overlap :: (InstEnv,InstEnv) -> PredType -> Either PredType SDoc
454 -- Right msg => overlap message
455 -- Left inst => no instance
456 check_overlap inst_envs pred@(ClassP clas tys)
457 = case lookupInstEnv inst_envs clas tys of
458 ([], _) -> Left pred -- No match
459 -- The case of exactly one match and no unifiers means a
460 -- successful lookup. That can't happen here, because dicts
461 -- only end up here if they didn't match in Inst.lookupInst
463 | debugIsOn -> pprPanic "check_overlap" (ppr pred)
464 res -> Right (mk_overlap_msg pred res)
465 check_overlap _ _ = panic "check_overlap"
467 mk_overlap_msg pred (matches, unifiers)
468 = ASSERT( not (null matches) )
469 vcat [ addArising orig (ptext (sLit "Overlapping instances for")
471 , sep [ptext (sLit "Matching instances") <> colon,
472 nest 2 (vcat [pprInstances ispecs, pprInstances unifiers])]
473 , if not (isSingleton matches)
474 then -- Two or more matches
476 else -- One match, plus some unifiers
477 ASSERT( not (null unifiers) )
478 parens (vcat [ptext (sLit "The choice depends on the instantiation of") <+>
479 quotes (pprWithCommas ppr (varSetElems (tyVarsOfPred pred))),
480 ptext (sLit "To pick the first instance above, use -XIncoherentInstances"),
481 ptext (sLit "when compiling the other instance declarations")])]
483 ispecs = [ispec | (ispec, _) <- matches]
485 mk_no_inst_err :: [PredType] -> SDoc
486 mk_no_inst_err wanteds
487 | Just givens <- getUserGivens ctxt
488 = vcat [ addArising orig $ couldNotDeduce givens wanteds
489 , show_fixes (fix1 : fixes2) ]
491 | otherwise -- Top level
492 = vcat [ addArising orig $
493 ptext (sLit "No instance") <> plural wanteds
494 <+> ptext (sLit "for") <+> pprTheta wanteds
495 , show_fixes fixes2 ]
498 fix1 = sep [ ptext (sLit "add") <+> pprTheta wanteds
499 <+> ptext (sLit "to the context of")
500 , nest 2 $ pprErrCtxtLoc ctxt ]
502 fixes2 | null instance_dicts = []
503 | otherwise = [sep [ptext (sLit "add an instance declaration for"),
504 pprTheta instance_dicts]]
505 instance_dicts = filterOut isTyVarClassPred wanteds
506 -- Insts for which it is worth suggesting an adding an
507 -- instance declaration. Exclude tyvar dicts.
509 show_fixes :: [SDoc] -> SDoc
510 show_fixes [] = empty
511 show_fixes (f:fs) = sep [ptext (sLit "Possible fix:"),
512 nest 2 (vcat (f : map (ptext (sLit "or") <+>) fs))]
514 reportAmbigErrs :: ReportErrCtxt -> TcTyVarSet -> [WantedEvVar] -> TcM ()
515 reportAmbigErrs ctxt skols ambigs
516 -- Divide into groups that share a common set of ambiguous tyvars
517 = mapM_ report (equivClasses cmp ambigs_w_tvs)
519 ambigs_w_tvs = [ (d, varSetElems (tyVarsOfWantedEvVar d `minusVarSet` skols))
521 cmp (_,tvs1) (_,tvs2) = tvs1 `compare` tvs2
523 report :: [(WantedEvVar, [TcTyVar])] -> TcM ()
526 do { let main_msg = sep [ text "Ambiguous type variable" <> plural tvs
527 <+> pprQuotedList tvs
528 <+> text "in the constraint" <> plural pairs <> colon
529 , nest 2 pp_wanteds ]
530 ; (tidy_env, mono_msg) <- mkMonomorphismMsg ctxt tvs
531 ; addErrTcM (tidy_env, main_msg $$ mono_msg) }
534 (loc, pp_wanteds) = pprWithArising (map fst pairs)
536 mkMonomorphismMsg :: ReportErrCtxt -> [TcTyVar] -> TcM (TidyEnv, SDoc)
537 -- There's an error with these Insts; if they have free type variables
538 -- it's probably caused by the monomorphism restriction.
539 -- Try to identify the offending variable
540 -- ASSUMPTION: the Insts are fully zonked
541 mkMonomorphismMsg ctxt inst_tvs
542 = do { dflags <- getDOpts
543 ; (tidy_env, docs) <- findGlobals ctxt (mkVarSet inst_tvs)
544 ; return (tidy_env, mk_msg dflags docs) }
546 mk_msg _ _ | any isRuntimeUnk inst_tvs
547 = vcat [ptext (sLit "Cannot resolve unknown runtime types:") <+>
548 (pprWithCommas ppr inst_tvs),
549 ptext (sLit "Use :print or :force to determine these types")]
550 mk_msg _ [] = ptext (sLit "Probable fix: add a type signature that fixes these type variable(s)")
551 -- This happens in things like
552 -- f x = show (read "foo")
553 -- where monomorphism doesn't play any role
555 = vcat [ptext (sLit "Possible cause: the monomorphism restriction applied to the following:"),
557 monomorphism_fix dflags]
559 monomorphism_fix :: DynFlags -> SDoc
560 monomorphism_fix dflags
561 = ptext (sLit "Probable fix:") <+> vcat
562 [ptext (sLit "give these definition(s) an explicit type signature"),
563 if xopt Opt_MonomorphismRestriction dflags
564 then ptext (sLit "or use -XNoMonomorphismRestriction")
565 else empty] -- Only suggest adding "-XNoMonomorphismRestriction"
566 -- if it is not already set!
569 -----------------------
570 -- findGlobals looks at the value environment and finds values whose
571 -- types mention any of the offending type variables. It has to be
572 -- careful to zonk the Id's type first, so it has to be in the monad.
573 -- We must be careful to pass it a zonked type variable, too.
575 mkEnvSigMsg :: SDoc -> [SDoc] -> SDoc
576 mkEnvSigMsg what env_sigs
577 | null env_sigs = empty
578 | otherwise = vcat [ ptext (sLit "The following variables have types that mention") <+> what
579 , nest 2 (vcat env_sigs) ]
581 findGlobals :: ReportErrCtxt
583 -> TcM (TidyEnv, [SDoc])
586 = do { lcl_ty_env <- case cec_encl ctxt of
588 (i:_) -> return (ic_env i)
589 ; go (cec_tidy ctxt) [] (nameEnvElts lcl_ty_env) }
591 go tidy_env acc [] = return (tidy_env, acc)
592 go tidy_env acc (thing : things) = do
593 (tidy_env1, maybe_doc) <- find_thing tidy_env ignore_it thing
595 Just d -> go tidy_env1 (d:acc) things
596 Nothing -> go tidy_env1 acc things
598 ignore_it ty = tvs `disjointVarSet` tyVarsOfType ty
600 -----------------------
601 find_thing :: TidyEnv -> (TcType -> Bool)
602 -> TcTyThing -> TcM (TidyEnv, Maybe SDoc)
603 find_thing tidy_env ignore_it (ATcId { tct_id = id })
604 = do { id_ty <- zonkTcType (idType id)
605 ; if ignore_it id_ty then
606 return (tidy_env, Nothing)
608 { let (tidy_env', tidy_ty) = tidyOpenType tidy_env id_ty
609 msg = sep [ ppr id <+> dcolon <+> ppr tidy_ty
610 , nest 2 (parens (ptext (sLit "bound at") <+>
611 ppr (getSrcLoc id)))]
612 ; return (tidy_env', Just msg) } }
614 find_thing tidy_env ignore_it (ATyVar tv ty)
615 = do { tv_ty <- zonkTcType ty
616 ; if ignore_it tv_ty then
617 return (tidy_env, Nothing)
619 { let -- The name tv is scoped, so we don't need to tidy it
620 (tidy_env1, tidy_ty) = tidyOpenType tidy_env tv_ty
621 msg = sep [ ptext (sLit "Scoped type variable") <+> quotes (ppr tv) <+> eq_stuff
624 eq_stuff | Just tv' <- tcGetTyVar_maybe tv_ty
625 , getOccName tv == getOccName tv' = empty
626 | otherwise = equals <+> ppr tidy_ty
627 -- It's ok to use Type.getTyVar_maybe because ty is zonked by now
628 bound_at = parens $ ptext (sLit "bound at:") <+> ppr (getSrcLoc tv)
630 ; return (tidy_env1, Just msg) } }
632 find_thing _ _ thing = pprPanic "find_thing" (ppr thing)
634 warnDefaulting :: [WantedEvVar] -> Type -> TcM ()
635 warnDefaulting wanteds default_ty
636 = do { warn_default <- doptM Opt_WarnTypeDefaults
637 ; setCtLoc loc $ warnTc warn_default warn_msg }
640 warn_msg = vcat [ ptext (sLit "Defaulting the following constraint(s) to type") <+>
641 quotes (ppr default_ty),
643 (loc, ppr_wanteds) = pprWithArising wanteds
646 %************************************************************************
648 Error from the canonicaliser
650 %************************************************************************
653 kindErrorTcS :: CtFlavor -> TcType -> TcType -> TcS ()
654 -- If there's a kind error, we don't want to blindly say "kind error"
655 -- We might, say, be unifying a skolem 'a' with a type 'Int',
656 -- in which case that's the error to report. So we set things
657 -- up to call reportEqErr, which does the business properly
658 kindErrorTcS fl ty1 ty2
659 = wrapEqErrTcS fl ty1 ty2 $ \ env0 extra ->
660 do { let (env1, ty1') = tidyOpenType env0 ty1
661 (env2, ty2') = tidyOpenType env1 ty2
662 ctxt = CEC { cec_encl = []
665 ; reportEqErr ctxt ty1' ty2' }
667 misMatchErrorTcS :: CtFlavor -> TcType -> TcType -> TcS a
668 misMatchErrorTcS fl ty1 ty2
669 = wrapEqErrTcS fl ty1 ty2 $ \ env0 extra ->
670 do { let (env1, ty1') = tidyOpenType env0 ty1
671 (env2, ty2') = tidyOpenType env1 ty2
672 (env3, msg) = misMatchMsgWithExtras env2 ty1' ty2'
673 ; failWithTcM (env3, inaccessible_msg $$ msg $$ extra) }
677 Given loc -> hang (ptext (sLit "Inaccessible code in"))
681 = case ctLocOrigin loc of
682 PatSkol dc mc -> sep [ ptext (sLit "a pattern with constructor")
683 <+> quotes (ppr dc) <> comma
684 , ptext (sLit "in") <+> pprMatchContext mc ]
685 other_skol -> pprSkolInfo other_skol
687 occursCheckErrorTcS :: CtFlavor -> TcTyVar -> TcType -> TcS a
688 occursCheckErrorTcS fl tv ty
689 = wrapEqErrTcS fl (mkTyVarTy tv) ty $ \ env0 extra2 ->
690 do { let (env1, tv') = tidyOpenTyVar env0 tv
691 (env2, ty') = tidyOpenType env1 ty
692 extra1 = sep [ppr tv', char '=', ppr ty']
693 ; failWithTcM (env2, hang msg 2 (extra1 $$ extra2)) }
695 msg = text $ "Occurs check: cannot construct the infinite type:"
697 solverDepthErrorTcS :: Int -> [CanonicalCt] -> TcS a
698 solverDepthErrorTcS depth stack
699 | null stack -- Shouldn't happen unless you say -fcontext-stack=0
700 = wrapErrTcS $ failWith msg
703 setCtFlavorLoc (cc_flavor top_item) $
704 do { env0 <- tcInitTidyEnv
705 ; let ev_vars = map cc_id stack
706 env1 = tidyFreeTyVars env0 free_tvs
707 free_tvs = foldr (unionVarSet . tyVarsOfEvVar) emptyVarSet ev_vars
708 extra = pprEvVars (map (tidyEvVar env1) ev_vars)
709 ; failWithTcM (env1, hang msg 2 extra) }
711 top_item = head stack
712 msg = vcat [ ptext (sLit "Context reduction stack overflow; size =") <+> int depth
713 , ptext (sLit "Use -fcontext-stack=N to increase stack size to N") ]
715 flattenForAllErrorTcS :: CtFlavor -> TcType -> Bag CanonicalCt -> TcS a
716 flattenForAllErrorTcS fl ty _bad_eqs
719 do { env0 <- tcInitTidyEnv
720 ; let (env1, ty') = tidyOpenType env0 ty
721 msg = sep [ ptext (sLit "Cannot deal with a type function under a forall type:")
723 ; failWithTcM (env1, msg) }
726 %************************************************************************
730 %************************************************************************
733 setCtFlavorLoc :: CtFlavor -> TcM a -> TcM a
734 setCtFlavorLoc (Wanted loc) thing = setCtLoc loc thing
735 setCtFlavorLoc (Derived loc) thing = setCtLoc loc thing
736 setCtFlavorLoc (Given loc) thing = setCtLoc loc thing
738 wrapEqErrTcS :: CtFlavor -> TcType -> TcType
739 -> (TidyEnv -> SDoc -> TcM a)
741 wrapEqErrTcS fl ty1 ty2 thing_inside
742 = do { ty_binds_var <- getTcSTyBinds
743 ; wrapErrTcS $ setCtFlavorLoc fl $
744 do { env0 <- tcInitTidyEnv
745 ; ty_binds_bag <- readTcRef ty_binds_var
746 ; let subst = mkOpenTvSubst (mkVarEnv (bagToList ty_binds_bag))
748 Wanted loc -> getWantedEqExtra subst env0 (ctLocOrigin loc) ty1 ty2 thing_inside
749 Derived loc -> getWantedEqExtra subst env0 (ctLocOrigin loc) ty1 ty2 thing_inside
750 Given {} -> thing_inside env0 empty -- We could print more info, but it
751 -- seems to be coming out already
754 getWantedEqExtra :: TvSubst -> TidyEnv -> CtOrigin -> TcType -> TcType
755 -> (TidyEnv -> SDoc -> TcM a)
757 getWantedEqExtra subst env0 (TypeEqOrigin item) ty1 ty2 thing_inside
758 -- If the types in the error message are the same
759 -- as the types we are unifying (remember to zonk the latter)
760 -- don't add the extra expected/actual message
762 -- The complication is that the types in the TypeEqOrigin must
764 -- (b) have any TcS-monad pending equalities applied to them
765 -- (hence the passed-in substitution)
766 = do { act0 <- zonkTcType (uo_actual item)
767 ; exp0 <- zonkTcType (uo_expected item)
768 ; let act1 = substTy subst act0
769 exp1 = substTy subst exp0
770 (env1, exp2) = tidyOpenType env0 exp1
771 (env2, act2) = tidyOpenType env1 act1
772 ; if (act1 `tcEqType` ty1 && exp1 `tcEqType` ty2)
773 || (exp1 `tcEqType` ty1 && act1 `tcEqType` ty2)
775 thing_inside env0 empty
777 thing_inside env2 (mkExpectedActualMsg act2 exp2) }
779 getWantedEqExtra _ env0 orig _ _ thing_inside
780 = thing_inside env0 (pprArising orig)