@@ -330,12 +330,13 @@ findSigOfBinds range = go
330330 findSigOfBind range (unLoc lHsBindLR)
331331 go _ = Nothing
332332
333- findInstanceHead :: (Outputable (HsType p ), p ~ GhcPass p0 ) => DynFlags -> String -> [LHsDecl p ] -> Maybe (LHsType p )
334- findInstanceHead df instanceHead decls =
333+ findInstanceHead :: (Outputable (HsType p ), p ~ GhcPass p0 ) => DynFlags -> Range -> [LHsDecl p ] -> Maybe (LHsType p )
334+ findInstanceHead df diagnosticLocation decls =
335335 listToMaybe
336336 [ hsib_body
337- | L _ (InstD _ (ClsInstD _ ClsInstDecl {cid_poly_ty = (unLoc -> HsSig {sig_body = hsib_body})})) <- decls,
338- showSDoc df (ppr hsib_body) == instanceHead
337+ | L (locA -> instanceLocation) (InstD _ (ClsInstD _ ClsInstDecl {cid_poly_ty = (unLoc -> HsSig {sig_body = hsib_body})})) <- decls
338+ , Just instanceRange <- [srcSpanToRange instanceLocation]
339+ , (subRange diagnosticLocation instanceRange)
339340 ]
340341
341342findDeclContainingLoc :: Foldable t => Position -> t (GenLocated (SrcSpanAnn' a ) e ) -> Maybe (GenLocated (SrcSpanAnn' a ) e )
@@ -1250,8 +1251,7 @@ suggestInstanceConstraint df (L _ HsModule {hsmodDecls}) Diagnostic {..} missing
12501251 -- • In the expression: x == y
12511252 -- In an equation for ‘==’: (Wrap x) == (Wrap y) = x == y
12521253 -- In the instance declaration for ‘Eq (Wrap a)’
1253- | Just [instanceDeclaration] <- matchRegexUnifySpaces _message " In the instance declaration for ‘([^`]*)’"
1254- , Just instHead <- findInstanceHead df (T. unpack instanceDeclaration) hsmodDecls
1254+ | Just instHead <- findInstanceHead df _range hsmodDecls
12551255 = Just instHead
12561256 -- Suggests a constraint for an instance declaration with one or more existing constraints.
12571257 -- • Could not deduce (Eq b) arising from a use of ‘==’
0 commit comments