diff options
author | MorrowM <themorrowm@gmail.com> | 2022-08-14 23:17:55 +0300 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2022-08-21 16:51:38 -0400 |
commit | 9939e95fb7b808b68aca00dfabbb99079927f482 (patch) | |
tree | 25866e9a6a17c818dd64d132fb534682a246ef39 | |
parent | ab3e0f5a02f6a1b63407e08bb97a228a76c27abd (diff) | |
download | haskell-9939e95fb7b808b68aca00dfabbb99079927f482.tar.gz |
Recognize file-header pragmas in GHCi (#21507)
-rw-r--r-- | docs/users_guide/9.6.1-notes.rst | 15 | ||||
-rw-r--r-- | docs/users_guide/ghci.rst | 9 | ||||
-rw-r--r-- | ghc/GHCi/UI.hs | 10 | ||||
-rw-r--r-- | testsuite/tests/ghci/scripts/T21507.script | 5 | ||||
-rwxr-xr-x | testsuite/tests/ghci/scripts/all.T | 1 |
5 files changed, 40 insertions, 0 deletions
diff --git a/docs/users_guide/9.6.1-notes.rst b/docs/users_guide/9.6.1-notes.rst index 17637ee0f7..6730ad2b0c 100644 --- a/docs/users_guide/9.6.1-notes.rst +++ b/docs/users_guide/9.6.1-notes.rst @@ -66,6 +66,21 @@ Compiler - The :extension:`TypeInType` is now marked as deprecated. Its meaning has been included in :extension:`PolyKinds` and :extension:`DataKinds`. + +GHCi +~~~~ + +- GHCi will now accept any file-header pragmas it finds, such as + ``{-# OPTIONS_GHC ... #-}`` and ``{-# LANGUAGE ... #-}`` (see :ref:`pragmas`). For example, + instead of using :ghci-cmd:`:set` to enable :ghc-flag:`-Wmissing-signatures`, + you could instead write: + + .. code-block:: none + + ghci> {-# OPTIONS_GHC -Wmissing-signatures #-} + +This can be convenient when pasting large multi-line blocks of code into GHCi. + ``base`` library ~~~~~~~~~~~~~~~~ diff --git a/docs/users_guide/ghci.rst b/docs/users_guide/ghci.rst index cb09eab3ff..4a9f75e9bf 100644 --- a/docs/users_guide/ghci.rst +++ b/docs/users_guide/ghci.rst @@ -3173,6 +3173,15 @@ example, to turn on :ghc-flag:`-Wmissing-signatures`, you would say: ghci> :set -Wmissing-signatures +GHCi will also accept any file-header pragmas it finds, such as +``{-# OPTIONS_GHC ... #-}`` and ``{-# LANGUAGE ... #-}`` (see :ref:`pragmas`). For example, +instead of using :ghci-cmd:`:set` to enable :ghc-flag:`-Wmissing-signatures`, +you could instead write: + +.. code-block:: none + + ghci> {-# OPTIONS_GHC -Wmissing-signatures #-} + Any GHC command-line option that is designated as dynamic (see the table in :ref:`flag-reference`), may be set using :ghci-cmd:`:set`. To unset an option, you can set the reverse option: diff --git a/ghc/GHCi/UI.hs b/ghc/GHCi/UI.hs index b19939e9ac..4eb9cd9324 100644 --- a/ghc/GHCi/UI.hs +++ b/ghc/GHCi/UI.hs @@ -78,6 +78,7 @@ import GHC.Types.Name.Reader as RdrName ( getGRE_NameQualifier_maybes, getRdrNam import GHC.Types.SrcLoc as SrcLoc import qualified GHC.Parser.Lexer as Lexer import GHC.Parser.Header ( toArgs ) +import qualified GHC.Parser.Header as Header import GHC.Types.PkgQual import GHC.Unit @@ -1249,6 +1250,9 @@ runStmt input step = do let source = progname st let line = line_number st + -- Add any LANGUAGE/OPTIONS_GHC pragmas we find find. + set_pragmas pflags + if | GHC.isStmt pflags input -> do hsc_env <- GHC.getSession mb_stmt <- liftIO (runInteractiveHsc hsc_env (hscParseStmtWithLocation source line input)) @@ -1282,6 +1286,12 @@ runStmt input step = do run_imports imports = mapM_ (addImportToContext . unLoc) imports + set_pragmas pflags = + let stringbuf = stringToStringBuffer input + (_msgs, loc_opts) = Header.getOptions pflags stringbuf "<interactive>" + opts = unLoc <$> loc_opts + in setOptions opts + run_stmt :: GhciMonad m => GhciLStmt GhcPs -> m (Maybe GHC.ExecResult) run_stmt stmt = do m_result <- GhciMonad.runStmt stmt input step diff --git a/testsuite/tests/ghci/scripts/T21507.script b/testsuite/tests/ghci/scripts/T21507.script new file mode 100644 index 0000000000..7c5d3df4cf --- /dev/null +++ b/testsuite/tests/ghci/scripts/T21507.script @@ -0,0 +1,5 @@ +:{ +{-# LANGUAGE TypeFamilies #-} +type family T21507 a where + T21507 a = a +:} diff --git a/testsuite/tests/ghci/scripts/all.T b/testsuite/tests/ghci/scripts/all.T index 9e7ca144a6..0ad107c4e4 100755 --- a/testsuite/tests/ghci/scripts/all.T +++ b/testsuite/tests/ghci/scripts/all.T @@ -368,3 +368,4 @@ test('T21110', [extra_files(['T21110A.hs'])], ghci_script, ['T21110.script']) test('T17830', [filter_stdout_lines(r'======.*')], ghci_script, ['T17830.script']) test('T21294a', normal, ghci_script, ['T21294a.script']) +test('T21507', normal, ghci_script, ['T21507.script']) |