Skip to content

Commit 81e684e

Browse files
committed
#993, deal with infix declarations in the module they occur
1 parent 3d290b3 commit 81e684e

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Changelog for HLint (* = breaking change)
22

3+
#993, deal with infix declarations in the module they occur
4+
#993, make createModuleEx use the default HLint fixities
35
#995, add unpackSrcSpan to the API
46
3.1, released 2020-05-07
57
#979, suggest removing flip only for simple final variables

data/hlint.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,4 +1171,6 @@
11711171
# f = map (flip (,) "a") "123" -- (,"a")
11721172
# f = map ((,) "a") "123" -- ("a",)
11731173
# test979 = flip Map.traverseWithKey blocks \k v -> lots_of_code_goes_here @NoRefactor
1174+
# infixl 4 <*! \
1175+
# test993 = f =<< g <$> x <*! y
11741176
# </TEST>

src/HSE/All.hs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Extension
2121
import FastString
2222

2323
import GHC.Hs
24+
import qualified BasicTypes as GHC
2425
import SrcLoc
2526
import ErrUtils
2627
import Outputable
@@ -112,6 +113,14 @@ ghcExtensionsFromParseFlags ParseFlags{enabledExtensions=es, disabledExtensions=
112113
ghcFixitiesFromParseFlags :: ParseFlags -> [(String, Fixity)]
113114
ghcFixitiesFromParseFlags = map toFixity . fixities
114115

116+
ghcFixitiesFromModule :: Located (HsModule GhcPs) -> [(String, Fixity)]
117+
ghcFixitiesFromModule (L _ (HsModule _ _ _ decls _ _)) = concatMap f decls
118+
where
119+
f :: LHsDecl GhcPs -> [(String, Fixity)]
120+
f (L _ (SigD _ (FixSig _ (FixitySig _ ops (GHC.Fixity _ p dir))))) =
121+
fixity dir p (map rdrNameStr' ops)
122+
f _ = []
123+
115124
-- These next two functions get called frorm 'Config/Yaml.hs' for user
116125
-- defined hint rules.
117126

@@ -141,11 +150,10 @@ parseDeclGhcWithMode parseMode s =
141150

142151
-- | Create a 'ModuleEx' from GHC annotations and module tree. It
143152
-- is assumed the incoming parse module has not been adjusted to
144-
-- account for operator fixities.
153+
-- account for operator fixities (it uses the HLint default fixities).
145154
createModuleEx :: ApiAnns -> Located (HsModule GhcPs) -> ModuleEx
146155
createModuleEx anns ast =
147-
-- Use builtin fixities.
148-
ModuleEx (applyFixities [] ast) anns
156+
ModuleEx (applyFixities (ghcFixitiesFromModule ast ++ map toFixity defaultFixities) ast) anns
149157

150158
-- | Parse a Haskell module. Applies the C pre processor, and uses
151159
-- best-guess fixity resolution if there are ambiguities. The
@@ -161,7 +169,6 @@ parseModuleEx flags file str = timedIO "Parse" file $ do
161169
str <- pure $ dropPrefix "\65279" str -- remove the BOM if it exists, see #130
162170
ppstr <- runCpp (cppFlags flags) file str
163171
let enableDisableExts = ghcExtensionsFromParseFlags flags
164-
fixities = ghcFixitiesFromParseFlags flags
165172
dynFlags <- parsePragmasIntoDynFlags baseDynFlags enableDisableExts file ppstr
166173
case dynFlags of
167174
Right ghcFlags -> do
@@ -176,7 +183,8 @@ parseModuleEx flags file str = timedIO "Parse" file $ do
176183
( Map.fromListWith (++) $ annotations s
177184
, Map.fromList ((noSrcSpan, comment_q s) : annotations_comments s)
178185
)
179-
pure $ Right (ModuleEx (applyFixities fixities a) anns)
186+
let fixes = ghcFixitiesFromModule a ++ ghcFixitiesFromParseFlags flags
187+
pure $ Right (ModuleEx (applyFixities fixes a) anns)
180188
PFailed s ->
181189
handleParseFailure ghcFlags ppstr file str $ bagToList . snd $ getMessages s ghcFlags
182190
Left msg -> do

0 commit comments

Comments
 (0)