diff options
author | Morrow <themorrowm@gmail.com> | 2021-10-04 02:30:47 +0300 |
---|---|---|
committer | Zubin <zubin.duggal@gmail.com> | 2021-11-17 11:14:37 +0000 |
commit | 7850142c09090a2eef1e1b0281acd641e843356a (patch) | |
tree | 962450cf647b18af467fe890bb212cf2ce759d59 /ghc | |
parent | 16d86b97ee3056b54441e7dfd349477f32347a26 (diff) | |
download | haskell-7850142c09090a2eef1e1b0281acd641e843356a.tar.gz |
Improve handling of import statements in GHCi (#20473)
Currently in GHCi, when given a line of user input we:
1. Attempt to parse and handle it as a statement
2. Otherwise, attempt to parse and handle a single import
3. Otherwise, check if there are imports present (and if so display an error message)
4. Otherwise, attempt to parse a module and only handle the declarations
This patch simplifies the process to:
Attempt to parse and handle it as a statement
Otherwise, attempt to parse a module and handle the imports and declarations
This means that multiple imports in a multiline are now accepted, and a multiline containing both imports and declarations is now accepted (as well as when separated by semicolons).
Diffstat (limited to 'ghc')
-rw-r--r-- | ghc/GHCi/UI.hs | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/ghc/GHCi/UI.hs b/ghc/GHCi/UI.hs index fc19207cc2..362fe0b40a 100644 --- a/ghc/GHCi/UI.hs +++ b/ghc/GHCi/UI.hs @@ -59,7 +59,7 @@ import GHC ( LoadHowMuch(..), Target(..), TargetId(..), Resume, SingleStep, Ghc, GetDocsFailure(..), putLogMsgM, pushLogHookM, getModuleGraph, handleSourceError, ms_mod ) -import GHC.Driver.Main (hscParseDeclsWithLocation, hscParseStmtWithLocation) +import GHC.Driver.Main (hscParseModuleWithLocation, hscParseStmtWithLocation) import GHC.Hs.ImpExp import GHC.Hs import GHC.Driver.Env @@ -1231,16 +1231,8 @@ runStmt input step = do Just stmt -> run_stmt stmt - | GHC.isImport pflags input -> run_import - - -- Every import declaration should be handled by `run_import`. As GHCi - -- in general only accepts one command at a time, we simply throw an - -- exception when the input contains multiple commands of which at least - -- one is an import command (see #10663). - | GHC.hasImport pflags input -> throwGhcException - (CmdLineError "error: expecting a single import declaration") - -- Otherwise assume a declaration (or a list of declarations) + -- and/or import(s) (#20473). -- Note: `GHC.isDecl` returns False on input like -- `data Infix a b = a :@: b; infixl 4 :@:` -- and should therefore not be used here. @@ -1250,14 +1242,14 @@ runStmt input step = do setDumpFilePrefix ic -- `-ddump-to-file` must work for normal GHCi compilations / -- evaluations. (#17500) - decls <- liftIO (hscParseDeclsWithLocation hsc_env source line input) + HsModule { hsmodDecls = decls, hsmodImports = imports } <- + liftIO (hscParseModuleWithLocation hsc_env source line input) + run_imports imports run_decls decls where exec_complete = GHC.ExecComplete (Right []) 0 - run_import = do - addImportToContext input - return (Just exec_complete) + run_imports imports = mapM_ (addImportToContext . unLoc) imports run_stmt :: GhciMonad m => GhciLStmt GhcPs -> m (Maybe GHC.ExecResult) run_stmt stmt = do @@ -2713,9 +2705,8 @@ setContext starred unstarred = restoreContextOnFailure $ do -- delete the transient context addModulesToContext_ starred unstarred -addImportToContext :: GhciMonad m => String -> m () -addImportToContext str = restoreContextOnFailure $ do - idecl <- GHC.parseImportDecl str +addImportToContext :: GhciMonad m => ImportDecl GhcPs -> m () +addImportToContext idecl = restoreContextOnFailure $ do addII (IIDecl idecl) -- #5836 setGHCContextFromGHCiState |