summaryrefslogtreecommitdiff
path: root/compiler/GHC/Rename/Env.hs
diff options
context:
space:
mode:
authorVance Palacio <vance@vanceism7.ml>2022-10-21 20:20:58 +0000
committerVance Palacio <vance@vanceism7.ml>2022-10-25 15:30:39 -0700
commit4668a4e7a0bf9aef2a3eeb57a1944e9c97d02066 (patch)
treed862d607d7b47f83d4b96e8a69b27aded61c2c53 /compiler/GHC/Rename/Env.hs
parentaec5a443bc45ca99cfeedc1777edb0aceca142cf (diff)
downloadhaskell-wip/ghc-21100.tar.gz
Refactor getCaretDiagnosticwip/ghc-21100
In order to properly create this error message, we need access to the line of code that caused the error. We need it without the carets though, so we factor the part of `getCaretDiagnostic` out that grabs the code ==== Add a new value ctor for `NotInScopeError` Types.hs: We need a new value ctor for `NotInScopeError` so we can tell if the error is specifically related to associated types. Codes.hs: Because we made a new ctor, we also need a new error code for it. ==== Create the `UnknownAssociatedType` error Env.hs: The only way we can tell that we're dealing with an associated type is via the `what` parameter. We change the parameter type to a string, deferring SDoc creation to within `lookupInstDeclBndr` so we can examine what `what` is. If it's `associated type`, we throw out the `err` contained in `mb_name` and create the more specific `UnknownAssociatedType` error. Otherwise we just do the normal thing. Bind.hs: Pass our `what` param in as a plain string since `lookupInstDeclBndr` requires that now ==== Construct the associated type error message Now that we have everything in place, we can create our error message for unknown associated types. ==== Accomodate for the `Maybe String` I'm not exactly sure why the srcSpan would fail to generate a code, but if it does, we use a simplified error message ==== Reword error and use `GhcHint` I might've gone too far with this one, but we can just switch it back if so. I thought of new wording for the error that might be a little more direct. We also utilize GhcHint for the suggestion on how to resolve the error. Hint.hs: Create a new value ctor for GhcHint: `SuggestDeclareAssociatedType` which instructs the user how to resolve `UnknownAssociatedType` error. Types.hs: We add `RdrName` as a parameter to `UnknownAssociatedType` so we can hand it off to our new GhcHint type Env.hs: We pass the RdrName to UnknownAssociatedType so we have it available when creating the hint in `scopeErrorHints` Errors/Ppr.hs: Reword the error message and remove the resolution hint since it's been moved to GhcHint In `scopeErrorHints`, we pass the params from `UnknownAssociatedType` to `SuggestDeclareAssociatedType`. Hint/Ppr.hs: We add in our SDoc wording for the `SuggestDeclareAssociatedType` hint ==== Update some failing test cases for the new wording ==== Add comments and use explicit fields Use explicit fields On `UnknownAssociatedType` and `SuggestDeclareAssociatedType` ctors, just so the constructors are a little better documented
Diffstat (limited to 'compiler/GHC/Rename/Env.hs')
-rw-r--r--compiler/GHC/Rename/Env.hs17
1 files changed, 13 insertions, 4 deletions
diff --git a/compiler/GHC/Rename/Env.hs b/compiler/GHC/Rename/Env.hs
index 3d3ded48f0..c79dfb38c1 100644
--- a/compiler/GHC/Rename/Env.hs
+++ b/compiler/GHC/Rename/Env.hs
@@ -350,7 +350,7 @@ lookupExactOcc_either name
}
-----------------------------------------------
-lookupInstDeclBndr :: Name -> SDoc -> RdrName -> RnM Name
+lookupInstDeclBndr :: Name -> String -> RdrName -> RnM Name
-- This is called on the method name on the left-hand side of an
-- instance declaration binding. eg. instance Functor T where
-- fmap = ...
@@ -378,11 +378,20 @@ lookupInstDeclBndr cls what rdr
-- when it's used
cls doc rdr
; case mb_name of
- Left err -> do { addErr (mkTcRnNotInScope rdr err)
+ Left err ->
+ -- If `what` is an associated type, we ignore the `err` value and create
+ -- our own error specifically dealing with associated types
+ case what of
+ "associated type" -> do { srcSpan <- getSrcSpanM
+ ; code <- liftIO $ getSrcCodeString srcSpan
+ ; addErr (mkTcRnNotInScope rdr (UnknownAssociatedType cls rdr code))
+ ; return (mkUnboundNameRdr rdr) }
+
+ _ -> do { addErr (mkTcRnNotInScope rdr err)
; return (mkUnboundNameRdr rdr) }
Right nm -> return nm }
where
- doc = what <+> text "of class" <+> quotes (ppr cls)
+ doc = text what <+> text "of class" <+> quotes (ppr cls)
-----------------------------------------------
lookupFamInstName :: Maybe Name -> LocatedN RdrName
@@ -390,7 +399,7 @@ lookupFamInstName :: Maybe Name -> LocatedN RdrName
-- Used for TyData and TySynonym family instances only,
-- See Note [Family instance binders]
lookupFamInstName (Just cls) tc_rdr -- Associated type; c.f GHC.Rename.Bind.rnMethodBind
- = wrapLocMA (lookupInstDeclBndr cls (text "associated type")) tc_rdr
+ = wrapLocMA (lookupInstDeclBndr cls "associated type") tc_rdr
lookupFamInstName Nothing tc_rdr -- Family instance; tc_rdr is an *occurrence*
= lookupLocatedOccRnConstr tc_rdr