on master help sign in
Restore ‘--ghc-opt’ options again, report unrecognized options
fa96da7mrkkrp
CHANGELOG.md+3
@@ -5,6 +5,9 @@
5 necessarily depending on the newest version of the compiler. In addition
5 necessarily depending on the newest version of the compiler. In addition
6 to that Ormolu is now GHCJS-compatible.
6 to that Ormolu is now GHCJS-compatible.
7
7
8+* Now unrecognized GHC options passed with `--ghc-opt` cause Ormolu to fail
9+ (exit code 7).
10+
8* Fixed formatting of result type in closed type families. See [issue
11* Fixed formatting of result type in closed type families. See [issue
9 420](https://github.com/tweag/ormolu/issues/420).
12 420](https://github.com/tweag/ormolu/issues/420).
10
13
src/Ormolu.hs+4−4
@@ -51,11 +51,11 @@ ormolu ::
51 String ->
51 String ->
52 m Text
52 m Text
53ormolu cfg path str = do
53ormolu cfg path str = do
54− (ws, result0) <-
54+ (warnings, result0) <-
55 parseModule' cfg OrmoluParsingFailed path str
55 parseModule' cfg OrmoluParsingFailed path str
56 when (cfgDebug cfg) $ do
56 when (cfgDebug cfg) $ do
57 traceM "warnings:\n"
57 traceM "warnings:\n"
58− traceM (concatMap showWarn ws)
58+ traceM (concatMap showWarn warnings)
59 traceM (prettyPrintParseResult result0)
59 traceM (prettyPrintParseResult result0)
60 -- NOTE We're forcing 'txt' here because otherwise errors (such as
60 -- NOTE We're forcing 'txt' here because otherwise errors (such as
61 -- messages about not-yet-supported functionality) will be thrown later
61 -- messages about not-yet-supported functionality) will be thrown later
@@ -133,10 +133,10 @@ parseModule' ::
133 String ->
133 String ->
134 m ([GHC.Warn], ParseResult)
134 m ([GHC.Warn], ParseResult)
135parseModule' cfg mkException path str = do
135parseModule' cfg mkException path str = do
136− (ws, r) <- parseModule cfg path str
136+ (warnings, r) <- parseModule cfg path str
137 case r of
137 case r of
138 Left (spn, err) -> liftIO $ throwIO (mkException spn err)
138 Left (spn, err) -> liftIO $ throwIO (mkException spn err)
139− Right x -> return (ws, x)
139+ Right x -> return (warnings, x)
140
140
141-- | Pretty-print a 'GHC.Warn'.
141-- | Pretty-print a 'GHC.Warn'.
142showWarn :: GHC.Warn -> String
142showWarn :: GHC.Warn -> String
src/Ormolu/Exception.hs+10
@@ -8,6 +8,8 @@ module Ormolu.Exception
8where
8where
9
9
10import Control.Exception
10import Control.Exception
11+import Data.List.NonEmpty (NonEmpty (..))
12+import qualified Data.List.NonEmpty as NE
11import Data.Text (Text)
13import Data.Text (Text)
12import qualified GHC
14import qualified GHC
13import Ormolu.Utils (showOutputable)
15import Ormolu.Utils (showOutputable)
@@ -27,6 +29,8 @@ data OrmoluException
27 OrmoluASTDiffers FilePath [GHC.SrcSpan]
29 OrmoluASTDiffers FilePath [GHC.SrcSpan]
28 | -- | Formatted source code is not idempotent
30 | -- | Formatted source code is not idempotent
29 OrmoluNonIdempotentOutput GHC.RealSrcLoc Text Text
31 OrmoluNonIdempotentOutput GHC.RealSrcLoc Text Text
32+ | -- | Some GHC options were not recognized
33+ OrmoluUnrecognizedOpts (NonEmpty String)
30 deriving (Eq, Show)
34 deriving (Eq, Show)
31
35
32instance Exception OrmoluException where
36instance Exception OrmoluException where
@@ -56,6 +60,11 @@ instance Exception OrmoluException where
56 loc
60 loc
57 ["before: " ++ show left, "after: " ++ show right]
61 ["before: " ++ show left, "after: " ++ show right]
58 ++ "Please, consider reporting the bug.\n"
62 ++ "Please, consider reporting the bug.\n"
63+ OrmoluUnrecognizedOpts opts ->
64+ unlines
65+ [ "The following GHC options were not recognized:",
66+ (withIndent . unwords . NE.toList) opts
67+ ]
59
68
60-- | Inside this wrapper 'OrmoluException' will be caught and displayed
69-- | Inside this wrapper 'OrmoluException' will be caught and displayed
61-- nicely using 'displayException'.
70-- nicely using 'displayException'.
@@ -76,6 +85,7 @@ withPrettyOrmoluExceptions m = m `catch` h
76 OrmoluOutputParsingFailed _ _ -> 4
85 OrmoluOutputParsingFailed _ _ -> 4
77 OrmoluASTDiffers _ _ -> 5
86 OrmoluASTDiffers _ _ -> 5
78 OrmoluNonIdempotentOutput _ _ _ -> 6
87 OrmoluNonIdempotentOutput _ _ _ -> 6
88+ OrmoluUnrecognizedOpts _ -> 7
79
89
80----------------------------------------------------------------------------
90----------------------------------------------------------------------------
81-- Helpers
91-- Helpers
src/Ormolu/Parser.hs+15−3
@@ -15,6 +15,7 @@ import Control.Exception
15import Control.Monad
15import Control.Monad
16import Control.Monad.IO.Class
16import Control.Monad.IO.Class
17import Data.List ((\\), foldl', isPrefixOf)
17import Data.List ((\\), foldl', isPrefixOf)
18+import qualified Data.List.NonEmpty as NE
18import Data.Maybe (catMaybes)
19import Data.Maybe (catMaybes)
19import qualified DynFlags as GHC
20import qualified DynFlags as GHC
20import DynFlags as GHC
21import DynFlags as GHC
@@ -57,8 +58,9 @@ parseModule Config {..} path input' = liftIO $ do
57 GHC.setGeneralFlag'
58 GHC.setGeneralFlag'
58 GHC.Opt_Haddock
59 GHC.Opt_Haddock
59 (setDefaultExts baseDynFlags)
60 (setDefaultExts baseDynFlags)
61+ extraOpts = dynOptionToLocatedStr <$> cfgDynOptions
60 (warnings, dynFlags) <-
62 (warnings, dynFlags) <-
61− parsePragmasIntoDynFlags baseFlags path input' >>= \case
63+ parsePragmasIntoDynFlags baseFlags extraOpts path input' >>= \case
62 Right res -> pure res
64 Right res -> pure res
63 Left err ->
65 Left err ->
64 let loc =
66 let loc =
@@ -170,14 +172,24 @@ setDefaultExts flags = foldl' GHC.xopt_set flags autoExts
170-- More helpers (taken from HLint)
172-- More helpers (taken from HLint)
171
173
172parsePragmasIntoDynFlags ::
174parsePragmasIntoDynFlags ::
175+ -- | Pre-set 'DynFlags'
173 DynFlags ->
176 DynFlags ->
177+ -- | Extra options (provided by user)
178+ [Located String] ->
179+ -- | File name (only for source location annotations)
174 FilePath ->
180 FilePath ->
181+ -- | Input for parser
175 String ->
182 String ->
176 IO (Either String ([GHC.Warn], DynFlags))
183 IO (Either String ([GHC.Warn], DynFlags))
177−parsePragmasIntoDynFlags flags filepath str =
184+parsePragmasIntoDynFlags flags extraOpts filepath str =
178 catchErrors $ do
185 catchErrors $ do
179 let opts = GHC.getOptions flags (GHC.stringToStringBuffer str) filepath
186 let opts = GHC.getOptions flags (GHC.stringToStringBuffer str) filepath
187+ (flags', leftovers, warnings) <-
180− (flags', _, warnings) <- parseDynamicFilePragma flags opts
188+ parseDynamicFilePragma flags (opts <> extraOpts)
189+ case NE.nonEmpty leftovers of
190+ Nothing -> return ()
191+ Just unrecognizedOpts ->
192+ throwIO (OrmoluUnrecognizedOpts (unLoc <$> unrecognizedOpts))
181 let flags'' = flags' `gopt_set` Opt_KeepRawTokenStream
193 let flags'' = flags' `gopt_set` Opt_KeepRawTokenStream
182 return $ Right (warnings, flags'')
194 return $ Right (warnings, flags'')
183 where
195 where