diff options
author | Simon Peyton Jones <simonpj@microsoft.com> | 2018-08-24 16:40:28 +0100 |
---|---|---|
committer | Simon Peyton Jones <simonpj@microsoft.com> | 2018-08-24 16:47:22 +0100 |
commit | ff29fc84c03c800cfa04c2a00eb8edf6fa5f4183 (patch) | |
tree | 16660e00d3d8f57af74384eb642a3723b45f4f4e /compiler | |
parent | 4b79329f24dfdf907f223ff9fc41c77d9df86e04 (diff) | |
download | haskell-ff29fc84c03c800cfa04c2a00eb8edf6fa5f4183.tar.gz |
Better error reporting for inaccessible code
This patch fixes Trac #15558. There turned out to be
two distinct problems
* In TcExpr.tc_poly_expr_nc we had
tc_poly_expr_nc (L loc expr) res_ty
= do { traceTc "tcPolyExprNC" (ppr res_ty)
; (wrap, expr')
<- tcSkolemiseET GenSigCtxt res_ty $ \ res_ty ->
setSrcSpan loc $
-- NB: setSrcSpan *after* skolemising,
-- so we get better skolem locations
tcExpr expr res_ty
Putting the setSrcSpan inside the tcSkolemise means that
the location on the Implication constraint is the /call/
to the function rather than the /argument/ to the call,
and that is really quite wrong.
I don't know what Richard's comment NB means -- I moved the
setSrcSpan outside, and the "binding site" info in error
messages actually improved.
The reason I found this is that it affects the span reported
for Trac #15558.
* In TcErrors.mkGivenErrorReporter we carefully munge the location
for an insoluble Given constraint (Note [Inaccessible code]).
But the 'implic' passed in wasn't necesarily the immediately-
enclosing implication -- but for location-munging purposes
it jolly well should be.
Solution: use the innermost implication. This actually
simplifies the code -- no need to pass an implication in to
mkGivenErrorReporter.
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/typecheck/TcErrors.hs | 42 | ||||
-rw-r--r-- | compiler/typecheck/TcExpr.hs | 6 |
2 files changed, 26 insertions, 22 deletions
diff --git a/compiler/typecheck/TcErrors.hs b/compiler/typecheck/TcErrors.hs index 06b660f193..87b853f42e 100644 --- a/compiler/typecheck/TcErrors.hs +++ b/compiler/typecheck/TcErrors.hs @@ -614,25 +614,28 @@ reportWanteds ctxt tc_lvl (WC { wc_simple = simples, wc_impl = implics }) is_irred _ (IrredPred {}) = True is_irred _ _ = False - given_eq_spec = case find_gadt_match (cec_encl ctxt) of - Just imp -> ("insoluble1a", is_given_eq, True, mkGivenErrorReporter imp) - Nothing -> ("insoluble1b", is_given_eq, False, ignoreErrorReporter) - -- False means don't suppress subsequent errors - -- Reason: we don't report all given errors - -- (see mkGivenErrorReporter), and we should only suppress - -- subsequent errors if we actually report this one! - -- Trac #13446 is an example - - find_gadt_match [] = Nothing - find_gadt_match (implic : implics) + given_eq_spec -- See Note [Given errors] + | has_gadt_match (cec_encl ctxt) + = ("insoluble1a", is_given_eq, True, mkGivenErrorReporter) + | otherwise + = ("insoluble1b", is_given_eq, False, ignoreErrorReporter) + -- False means don't suppress subsequent errors + -- Reason: we don't report all given errors + -- (see mkGivenErrorReporter), and we should only suppress + -- subsequent errors if we actually report this one! + -- Trac #13446 is an example + + -- See Note [Given errors] + has_gadt_match [] = False + has_gadt_match (implic : implics) | PatSkol {} <- ic_info implic , not (ic_no_eqs implic) , wopt Opt_WarnInaccessibleCode (implicDynFlags implic) -- Don't bother doing this if -Winaccessible-code isn't enabled. -- See Note [Avoid -Winaccessible-code when deriving] in TcInstDcls. - = Just implic + = True | otherwise - = find_gadt_match implics + = has_gadt_match implics --------------- isSkolemTy :: TcLevel -> Type -> Bool @@ -701,14 +704,17 @@ mkUserTypeError ctxt ct = mkErrorMsgFromCt ctxt ct Nothing -> pprPanic "mkUserTypeError" (ppr ct) -mkGivenErrorReporter :: Implication -> Reporter +mkGivenErrorReporter :: Reporter -- See Note [Given errors] -mkGivenErrorReporter implic ctxt cts +mkGivenErrorReporter ctxt cts = do { (ctxt, binds_msg, ct) <- relevantBindings True ctxt ct ; dflags <- getDynFlags - ; let ct' = setCtLoc ct (setCtLocEnv (ctLoc ct) (implicLclEnv implic)) + ; let (implic:_) = cec_encl ctxt + -- Always non-empty when mkGivenErrorReporter is called + ct' = setCtLoc ct (setCtLocEnv (ctLoc ct) (implicLclEnv implic)) -- For given constraints we overwrite the env (and hence src-loc) - -- with one from the implication. See Note [Inaccessible code] + -- with one from the immediately-enclosing implication. + -- See Note [Inaccessible code] inaccessible_msg = hang (text "Inaccessible code in") 2 (ppr (ic_info implic)) @@ -761,7 +767,7 @@ which arguably is OK. It's more debatable for but it's tricky to distinguish these cases so we don't report either. -The bottom line is this: find_gadt_match looks for an enclosing +The bottom line is this: has_gadt_match looks for an enclosing pattern match which binds some equality constraints. If we find one, we report the insoluble Given. -} diff --git a/compiler/typecheck/TcExpr.hs b/compiler/typecheck/TcExpr.hs index 30b46c74bd..b70276da7e 100644 --- a/compiler/typecheck/TcExpr.hs +++ b/compiler/typecheck/TcExpr.hs @@ -110,12 +110,10 @@ tc_poly_expr expr res_ty do { traceTc "tcPolyExpr" (ppr res_ty); tc_poly_expr_nc expr res_ty } tc_poly_expr_nc (L loc expr) res_ty - = do { traceTc "tcPolyExprNC" (ppr res_ty) + = setSrcSpan loc $ + do { traceTc "tcPolyExprNC" (ppr res_ty) ; (wrap, expr') <- tcSkolemiseET GenSigCtxt res_ty $ \ res_ty -> - setSrcSpan loc $ - -- NB: setSrcSpan *after* skolemising, so we get better - -- skolem locations tcExpr expr res_ty ; return $ L loc (mkHsWrap wrap expr') } |