on master help sign in
Restore ‘--ghc-opt’ options again, report unrecognized options

While migrating to ‘ghc-lib-parser’ we accidentally stopped taking into account the ‘--ghc-opt’ options. This commit fixes that and makes sure we do not ignore unrecognized GHC options.

commitfa96da7d6759a8d5007f9c8c38b2ae4ed12f8746 authormrkkrp <mark.karpov@tweag.io> dateSat 2 Nov 2019, 12:34 (7 years ago) parentb08af17
4 files changed+32−7
CHANGELOG.md+3 src/Ormolu.hs+4−4 src/Ormolu/Exception.hs+10 src/Ormolu/Parser.hs+15−3
CHANGELOG.md+3
@@ -5,6 +5,9 @@
55 necessarily depending on the newest version of the compiler. In addition
66 to that Ormolu is now GHCJS-compatible.
77
8+* Now unrecognized GHC options passed with `--ghc-opt` cause Ormolu to fail
9+ (exit code 7).
10+
811* Fixed formatting of result type in closed type families. See [issue
912 420](https://github.com/tweag/ormolu/issues/420).
1013
src/Ormolu.hs+4−4
@@ -51,11 +51,11 @@ ormolu ::
5151 String ->
5252 m Text
5353ormolu cfg path str = do
54− (ws, result0) <-
54+ (warnings, result0) <-
5555 parseModule' cfg OrmoluParsingFailed path str
5656 when (cfgDebug cfg) $ do
5757 traceM "warnings:\n"
58− traceM (concatMap showWarn ws)
58+ traceM (concatMap showWarn warnings)
5959 traceM (prettyPrintParseResult result0)
6060 -- NOTE We're forcing 'txt' here because otherwise errors (such as
6161 -- messages about not-yet-supported functionality) will be thrown later
@@ -133,10 +133,10 @@ parseModule' ::
133133 String ->
134134 m ([GHC.Warn], ParseResult)
135135parseModule' cfg mkException path str = do
136− (ws, r) <- parseModule cfg path str
136+ (warnings, r) <- parseModule cfg path str
137137 case r of
138138 Left (spn, err) -> liftIO $ throwIO (mkException spn err)
139− Right x -> return (ws, x)
139+ Right x -> return (warnings, x)
140140
141141-- | Pretty-print a 'GHC.Warn'.
142142showWarn :: GHC.Warn -> String
src/Ormolu/Exception.hs+10
@@ -8,6 +8,8 @@ module Ormolu.Exception
88where
99
1010import Control.Exception
11+import Data.List.NonEmpty (NonEmpty (..))
12+import qualified Data.List.NonEmpty as NE
1113import Data.Text (Text)
1214import qualified GHC
1315import Ormolu.Utils (showOutputable)
@@ -27,6 +29,8 @@ data OrmoluException
2729 OrmoluASTDiffers FilePath [GHC.SrcSpan]
2830 | -- | Formatted source code is not idempotent
2931 OrmoluNonIdempotentOutput GHC.RealSrcLoc Text Text
32+ | -- | Some GHC options were not recognized
33+ OrmoluUnrecognizedOpts (NonEmpty String)
3034 deriving (Eq, Show)
3135
3236instance Exception OrmoluException where
@@ -56,6 +60,11 @@ instance Exception OrmoluException where
5660 loc
5761 ["before: " ++ show left, "after: " ++ show right]
5862 ++ "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+ ]
5968
6069-- | Inside this wrapper 'OrmoluException' will be caught and displayed
6170-- nicely using 'displayException'.
@@ -76,6 +85,7 @@ withPrettyOrmoluExceptions m = m `catch` h
7685 OrmoluOutputParsingFailed _ _ -> 4
7786 OrmoluASTDiffers _ _ -> 5
7887 OrmoluNonIdempotentOutput _ _ _ -> 6
88+ OrmoluUnrecognizedOpts _ -> 7
7989
8090----------------------------------------------------------------------------
8191-- Helpers
src/Ormolu/Parser.hs+15−3
@@ -15,6 +15,7 @@ import Control.Exception
1515import Control.Monad
1616import Control.Monad.IO.Class
1717import Data.List ((\\), foldl', isPrefixOf)
18+import qualified Data.List.NonEmpty as NE
1819import Data.Maybe (catMaybes)
1920import qualified DynFlags as GHC
2021import DynFlags as GHC
@@ -57,8 +58,9 @@ parseModule Config {..} path input' = liftIO $ do
5758 GHC.setGeneralFlag'
5859 GHC.Opt_Haddock
5960 (setDefaultExts baseDynFlags)
61+ extraOpts = dynOptionToLocatedStr <$> cfgDynOptions
6062 (warnings, dynFlags) <-
61− parsePragmasIntoDynFlags baseFlags path input' >>= \case
63+ parsePragmasIntoDynFlags baseFlags extraOpts path input' >>= \case
6264 Right res -> pure res
6365 Left err ->
6466 let loc =
@@ -170,14 +172,24 @@ setDefaultExts flags = foldl' GHC.xopt_set flags autoExts
170172-- More helpers (taken from HLint)
171173
172174parsePragmasIntoDynFlags ::
175+ -- | Pre-set 'DynFlags'
173176 DynFlags ->
177+ -- | Extra options (provided by user)
178+ [Located String] ->
179+ -- | File name (only for source location annotations)
174180 FilePath ->
181+ -- | Input for parser
175182 String ->
176183 IO (Either String ([GHC.Warn], DynFlags))
177−parsePragmasIntoDynFlags flags filepath str =
184+parsePragmasIntoDynFlags flags extraOpts filepath str =
178185 catchErrors $ do
179186 let opts = GHC.getOptions flags (GHC.stringToStringBuffer str) filepath
180− (flags', _, warnings) <- parseDynamicFilePragma flags opts
187+ (flags', leftovers, warnings) <-
188+ parseDynamicFilePragma flags (opts <> extraOpts)
189+ case NE.nonEmpty leftovers of
190+ Nothing -> return ()
191+ Just unrecognizedOpts ->
192+ throwIO (OrmoluUnrecognizedOpts (unLoc <$> unrecognizedOpts))
181193 let flags'' = flags' `gopt_set` Opt_KeepRawTokenStream
182194 return $ Right (warnings, flags'')
183195 where