Commit 3b497ddb authored by Gergő Érdi's avatar Gergő Érdi
Browse files

Check dflags for language extensions when deciding if "foreign " and "deriving "

look like prefixes of valid declarations (fixes #9915)
parent 4523d669
......@@ -877,9 +877,17 @@ enqueueCommands cmds = do
-- | If we one of these strings prefixes a command, then we treat it as a decl
-- rather than a stmt.
declPrefixes :: [String]
declPrefixes = ["class ","data ","newtype ","type ","instance ", "deriving ",
"foreign ", "default ", "default("]
declPrefixes :: DynFlags -> [String]
declPrefixes dflags = keywords ++ concat opt_keywords
where
keywords = [ "class ", "instance "
, "data ", "newtype ", "type "
, "default ", "default("
]
opt_keywords = [ ["foreign " | xopt Opt_ForeignFunctionInterface dflags]
, ["deriving " | xopt Opt_StandaloneDeriving dflags]
]
-- | Entry point to execute some haskell code from user
runStmt :: String -> SingleStep -> GHCi Bool
......@@ -892,23 +900,28 @@ runStmt stmt step
| "import " `isPrefixOf` stmt
= do addImportToContext stmt; return False
-- data, class, newtype...
| any (flip isPrefixOf stmt) declPrefixes
= do _ <- liftIO $ tryIO $ hFlushAll stdin
result <- GhciMonad.runDecls stmt
afterRunStmt (const True) (GHC.RunOk result)
| otherwise
= do -- In the new IO library, read handles buffer data even if the Handle
-- is set to NoBuffering. This causes problems for GHCi where there
-- are really two stdin Handles. So we flush any bufferred data in
-- GHCi's stdin Handle here (only relevant if stdin is attached to
-- a file, otherwise the read buffer can't be flushed).
_ <- liftIO $ tryIO $ hFlushAll stdin
m_result <- GhciMonad.runStmt stmt step
case m_result of
Nothing -> return False
Just result -> afterRunStmt (const True) result
= do dflags <- getDynFlags
if any (`isPrefixOf` stmt) (declPrefixes dflags)
then run_decl
else run_stmt
where
run_decl =
do _ <- liftIO $ tryIO $ hFlushAll stdin
result <- GhciMonad.runDecls stmt
afterRunStmt (const True) (GHC.RunOk result)
run_stmt =
do -- In the new IO library, read handles buffer data even if the Handle
-- is set to NoBuffering. This causes problems for GHCi where there
-- are really two stdin Handles. So we flush any bufferred data in
-- GHCi's stdin Handle here (only relevant if stdin is attached to
-- a file, otherwise the read buffer can't be flushed).
_ <- liftIO $ tryIO $ hFlushAll stdin
m_result <- GhciMonad.runStmt stmt step
case m_result of
Nothing -> return False
Just result -> afterRunStmt (const True) result
-- | Clean up the GHCi environment after a statement has run
afterRunStmt :: (SrcSpan -> Bool) -> GHC.RunResult -> GHCi Bool
......
:set -XHaskell98
foreign = 42
let foreign = 42
foreign
foreign -- Note extra space after name!
......@@ -20,3 +20,4 @@ test('T3171',
test('ghcirun004', just_ghci, compile_and_run, [''])
test('T8377', just_ghci, compile_and_run, [''])
test('T9915', just_ghci, ghci_script, ['T9915.script'])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment