[Git][ghc/ghc][wip/romes/async-rpc] Close pipe cleans up pending requests and the other process can handle EOF and...
by Rodrigo Mesquita (@alt-romes) 14 Oct '26
by Rodrigo Mesquita (@alt-romes) 14 Oct '26
14 Oct '26
Rodrigo Mesquita pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC
Commits:
852b80a7 by Rodrigo Mesquita at 2026-10-14T14:48:41+01:00
Close pipe cleans up pending requests and the other process can handle EOF and raise correct exceptions when pipe is read or written when closed
- - - - -
3 changed files:
- libraries/ghci/GHCi/Message.hs
- libraries/ghci/GHCi/Server.hs
- testsuite/tests/ghci/custom-external-interpreter-commands/Main.hs
Changes:
=====================================
libraries/ghci/GHCi/Message.hs
=====================================
@@ -27,7 +27,7 @@ module GHCi.Message
, Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe
, remoteCall, remoteTHCall
, handleRemoteCall, handleRemoteTHCall
- , RemoteProcessException(..)
+ , RemoteProcessException(..), PipeClosedException(..)
, BreakModule
, BreakUnitId
@@ -89,7 +89,6 @@ import qualified GHC.Boot.TH.Syntax as TH
import qualified GHC.Boot.TH.Monad as TH
import System.Exit
import System.IO
-import System.IO.Error
import Control.Monad
import Control.Concurrent.STM
import Data.Bifunctor
@@ -722,38 +721,55 @@ data Pipe = Pipe
, pipeLeftovers :: !(IORef (Maybe ByteString))
, pipeLock :: !(MVar ()) -- ^ Lock to prevent concurrent writes to the stream
, pipeAsyncMngr :: !AsyncManager
- , pipeCleanupRW :: !(IO ())
- -- ^ Clean up the read/write resources, e.g. closes the handles if 'mkPipeFromHandles'
+ , pipeIsClosed :: !(TVar Bool)
+ -- ^ After the pipe is closed, requests can no longer be received nor sent
}
-- | Make a 'Pipe' from a 'Handle' to read and a 'Handle' to write.
--- Must call 'closePipe' at the end to clean up and close the handles.
+-- Must call 'closePipe' at the end to clean up.
mkPipeFromHandles :: Handle -> Handle -> IO Pipe
mkPipeFromHandles pipeRead pipeWrite = do
let getSome = B.hGetSome pipeRead (32*1024)
putAll b = do
B.hPutBuilder pipeWrite b
hFlush pipeWrite
- mkPipeFromContinuations getSome putAll (hClose pipeRead >> hClose pipeWrite)
+ mkPipeFromContinuations getSome putAll
--- | Make a 'Pipe' from a reader function and a writer function, plus an action
--- to clean up the reader and writer resources. The pipe should be closed with
--- 'closePipe', which will call this clean up action too.
-mkPipeFromContinuations :: IO ByteString -> (B.Builder -> IO ()) -> IO () -> IO Pipe
-mkPipeFromContinuations getSome putAll pipeCleanupRW = mdo
+-- | Make a 'Pipe' from a reader function and a writer function.
+-- The pipe should be closed with 'closePipe'.
+mkPipeFromContinuations :: IO ByteString -> (B.Builder -> IO ()) -> IO Pipe
+mkPipeFromContinuations getSome putAll = mdo
pipeLeftovers <- newIORef Nothing
pipeLock <- newMVar ()
+ pipeIsClosed <- newTVarIO False
mdo
pipeAsyncMngr <- newAsyncManager p
let p = Pipe { .. }
pure p
--- | Cleanly close this pipe, terminating the async manager's drain-read-end thread.
--- This doesn't close the handles
+-- | Cleanly close our end of the Pipe, terminating the async manager's
+-- drain-read-end thread. Calling 'closePipe' on an already closed pipe is a no-op.
+--
+-- If the pipe was created using 'mkPipeFromHandles', the handles must only by
+-- closed after calling 'closePipe'.
+--
+-- The requests we've sent that are pending are all canceled with
+-- 'PipeClosedException' by ourselves.
+--
+-- The remote process does not necessarily detect that this end of the pipe was
+-- closed, but it must handle cleanly and clean up after exceptions caused from
+-- it such as a handle being closed.
closePipe :: Pipe -> IO ()
closePipe Pipe{..} = do
- killThread (asyncDrainTid pipeAsyncMngr) -- releases handle lock taken by reading
- pipeCleanupRW -- hClose won't deadlock now that the drainThread is killed
+
+ isClosed <- readTVarIO pipeIsClosed
+
+ unless isClosed $ do
+ atomically $ writeTVar pipeIsClosed True
+
+ -- releases handle lock taken by reading, and cleans up by replying to every
+ -- pending request with 'PipeClosedException'.
+ throwTo (asyncDrainTid pipeAsyncMngr) PipeClosedException
-- | Send a 'Message' request to and wait for a reply from the interpreter
-- (blocks on the calling side, doesn't necessarily block the interpreter)
@@ -799,16 +815,24 @@ remoteAsyncReq pipe msg = do
-- Mark pending request
uq <- freshReqId (pipeAsyncMngr pipe)
wv <- newEmptyMVar
- atomicModifyIORef' pendingReplies $
- \m -> (IntMap.insert uq wv m, ())
+
+ isClosed <- atomically $ do
+ isClosed <- readTVar (pipeIsClosed pipe)
+ if isClosed
+ then pure True
+ else do
+ modifyTVar pendingReplies (IntMap.insert uq wv)
+ pure False
+
+ when isClosed $ do
+ throwIO PipeClosedException
-- Write request
writePipe pipe (Request uq (runPut msg))
`onException`
-- the req wasn't done, or no one will be reading the reply:
-- in either case, drop it from the pending map.
- atomicModifyIORef' pendingReplies
- (\m -> (IntMap.delete uq m, ()))
+ atomically (modifyTVar pendingReplies (IntMap.delete uq))
-- Block waiting for reply
takeMVar wv
@@ -826,32 +850,40 @@ handleAsyncReq pipe getit k = do
ack <- newEmptyMVar
-- Block waiting for request
- (uq, bs) <- atomically (readTQueue (requestsQueue (pipeAsyncMngr pipe)))
-
- let reply p = do
- writePipe pipe (Reply uq (bimap toSerializableException runPut p))
- putMVar ack ()
-
- -- Run the continuation which must reply exactly once with 'writeReply'
- case runGetOrFail getit bs of
- Left (_, _, err) -> do
- let exc = DecodeAsyncReqException err
- reply (Left (toException exc))
- throwIO exc
- Right (_, _, it) ->
- k (WriteReply reply) it
-
- -- If there's an exception before the reply is sent, reply with an error instead.
- `catch` \(e :: SomeException) -> do
-
- tryTakeMVar ack >>= \case
- Just () ->
- -- Already replied
- throwIO e
- Nothing -> do
- -- Reply with an error
- reply (Left e)
- throwIO e
+ mbreq <- atomically $ do
+ isClosed <- readTVar (pipeIsClosed pipe)
+ isEmpty <- isEmptyTQueue (requestsQueue (pipeAsyncMngr pipe))
+ if isClosed && isEmpty
+ then return Nothing
+ else Just <$> readTQueue (requestsQueue (pipeAsyncMngr pipe))
+
+ case mbreq of
+ Nothing -> throwIO PipeClosedException
+ Just (uq, bs) -> do
+ let reply p = do
+ writePipe pipe (Reply uq (bimap toSerializableException runPut p))
+ putMVar ack ()
+
+ -- Run the continuation which must reply exactly once with 'writeReply'
+ case runGetOrFail getit bs of
+ Left (_, _, err) -> do
+ let exc = DecodeAsyncReqException err
+ reply (Left (toException exc))
+ throwIO exc
+ Right (_, _, it) ->
+ k (WriteReply reply) it
+
+ -- If there's an exception before the reply is sent, reply with an error instead.
+ `catch` \(e :: SomeException) -> do
+
+ tryTakeMVar ack >>= \case
+ Just () ->
+ -- Already replied
+ throwIO e
+ Nothing -> do
+ -- Reply with an error
+ reply (Left e)
+ throwIO e
-- | An exception that can be thrown on the host process representing an
-- expection that happened on the remote one.
@@ -862,10 +894,14 @@ instance Exception RemoteProcessException
newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
instance Exception DecodeAsyncReqException
+-- | We've closed the pipe on our end and we terminate all pending requests with this.
+data PipeClosedException = PipeClosedException deriving Show
+instance Exception PipeClosedException
+
-- ** Async matching "internals" -----------------------------------------------
data AsyncManager = AsyncManager
- { pendingReply :: !(IORef (IntMap (MVar (Either SomeException LB.ByteString))))
+ { pendingReply :: !(TVar (IntMap (MVar (Either SomeException LB.ByteString))))
-- ^ When a request is sent, keep track that we're expecting a reply. The
-- MVar is signaled when the reply arrives. The exception can originate
-- from the host process, or from the remote one (with 'RemoteProcessException').
@@ -904,45 +940,60 @@ instance Binary IddMsg where
newAsyncManager :: Pipe -> IO AsyncManager
newAsyncManager pipe = do
asyncNextReq <- newIORef 0
- pendingReply <- newIORef IntMap.empty
+ pendingReply <- newTVarIO IntMap.empty
requestsQueue <- newTQueueIO
- mdo
- -- Fork thread to drain read end.
- -- (cleaned up on 'closePipe')
- asyncDrainTid <- forkIO (drainPipe pipe am)
+ -- Fork thread to drain read end.
+ -- (cleaned up on 'closePipe')
+ asyncDrainTid <- forkIO (drainPipe pipe)
- let am = AsyncManager{..}
- pure am
+ let am = AsyncManager{..}
+ pure am
-- | Drains the read end of the Pipe and match 'Reply'es against pending 'Request's.
--
-- In pratice, we'll have both a thread draining the read end of the host's
-- 'Pipe' and a thread draining the read end of the interpreter's 'Pipe', since
--- both processes construct a 'Pipe' and send and receive messages:
-drainPipe :: Pipe -> AsyncManager -> IO ()
-drainPipe p am = flip catch cancelPending $ forever $ do
- readPipe p >>= \case
- -- Receive a reply to a pending notification
- Reply uq bs -> do
- mwv <- atomicModifyIORef' (pendingReply am) $ \m -> do
- case IntMap.lookup uq m of
- Nothing -> (m, Nothing)
- Just wv -> (IntMap.delete uq m, Just wv)
- case mwv of
- Just wv ->
- putMVar wv (bimap (toException . RemoteProcessException) id bs) -- signal matching request
- Nothing ->
- -- the req was dropped from pending bc there was an exception, and no
- -- one's listening any more. the reply is ignored
- pure ()
- -- Receive a request and queue it
- Request uq bs ->
- atomically $ writeTQueue (requestsQueue am) (uq,bs)
- where
- cancelPending (e::SomeException) = do
- pendings <- readIORef (pendingReply am)
- mapM_ (\p -> putMVar p (Left e {-host exception-})) pendings
+-- both processes construct a 'Pipe' and send and receive messages
+drainPipe :: Pipe -> IO ()
+drainPipe p = catch loop replyToPendings where
+ loop = do
+ readPipe p >>= \case
+ -- Receive a reply to a pending notification
+ Just (Reply uq bs) -> do
+
+ mwv <- atomically $ do
+ m <- readTVar (pendingReply am)
+ case IntMap.lookup uq m of
+ Nothing -> pure Nothing
+ Just wv -> do
+ writeTVar (pendingReply am) (IntMap.delete uq m)
+ pure (Just wv)
+ case mwv of
+ Just wv ->
+ putMVar wv (bimap (toException . RemoteProcessException) id bs) -- signal matching request
+ Nothing ->
+ -- the req was dropped from pending bc there was an exception, and no
+ -- one's listening any more. the reply is ignored
+ pure ()
+ loop
+ -- Receive a request and queue it
+ Just (Request uq bs) -> do
+ atomically $ writeTQueue (requestsQueue am) (uq,bs)
+ loop
+ -- EOF on read handle
+ Nothing -> do
+ -- clean up pipe then exit loop
+ replyToPendings (toException PipeClosedException)
+ pure ()
+
+ replyToPendings e = do
+ pendings <- atomically $ do
+ writeTVar (pipeIsClosed p) True
+ readTVar (pendingReply am)
+ mapM_ (\p -> putMVar p (Left e {-host exception-})) pendings
+
+ am = pipeAsyncMngr p
freshReqId :: AsyncManager -> IO CorrelationId
freshReqId AsyncManager{asyncNextReq} =
@@ -960,16 +1011,17 @@ writePipe p@Pipe{..} = withLock p . putAll . execPut . put
-- | Only the 'drainPipe' thread should read from the pipe directly. Other
-- functions should wait for the matching reply to be delivered asynchronously.
-- (see 'withAsyncReq'). It doesn't lock to read bc it assumes single reader is 'drainPipe'.
-readPipe :: Pipe -> IO IddMsg
+--
+-- Returns @Nothing@ on EOF.
+readPipe :: Pipe -> IO (Maybe IddMsg)
readPipe Pipe{..} = do
leftovers <- readIORef pipeLeftovers
m <- getBin getSome get leftovers
case m of
- Nothing -> throw $
- mkIOError eofErrorType "GHCi.Message.readPipe" Nothing Nothing
+ Nothing -> return Nothing
Just (result, new_leftovers) -> do
writeIORef pipeLeftovers new_leftovers
- return result
+ return (Just result)
getBin
:: (IO ByteString) -> Get a -> Maybe ByteString
=====================================
libraries/ghci/GHCi/Server.hs
=====================================
@@ -79,7 +79,7 @@ servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop
_other -> run msg >>= reply
isDone <- readIORef doneRef
- if isDone then return ()
+ if isDone then closePipe pipe
else loop doneRef
handleCustom tag payload = do
@@ -184,7 +184,7 @@ defaultServerWithCustom customHandler = do
#if defined(wasm32_HOST_ARCH)
-- See Note [wasm ghci signal handlers] for details
installSignalHandlers $ js_register_signal_handler cb_sig
- pipe <- mkPipeFromContinuations (recv_buf cb_recv) (send_buf cb_send) (pure ())
+ pipe <- mkPipeFromContinuations (recv_buf cb_recv) (send_buf cb_send)
#else
when verbose $
printf "GHC iserv starting (in: %s; out: %s)\n" (show inh) (show outh)
=====================================
testsuite/tests/ghci/custom-external-interpreter-commands/Main.hs
=====================================
@@ -171,12 +171,14 @@ withServer serverExe action = do
, std_out = Inherit
, std_err = Inherit
}
- bracket (mkHandles ghcRead ghcWrite >>= uncurry mkPipeFromHandles)
- (\pipe -> do
+ bracket (mkHandles ghcRead ghcWrite >>= \hds -> (hds,) <$> uncurry mkPipeFromHandles hds)
+ (\((hFromServer, hToServer), pipe) -> do
closePipe pipe
+ hClose hFromServer
+ hClose hToServer
terminateProcess ph
void (waitForProcess ph))
- (\pipe -> action pipe)
+ (\(_, pipe) -> action pipe)
where
mkHandles r w = do
hR <- fdToHandle r
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/852b80a714fd395f42f42929dd9bc2f…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/852b80a714fd395f42f42929dd9bc2f…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/marge_bot_batch_merge_job] 3 commits: add runGhcWithSignalHandlers and remove handling from runGhc[T]
by Marge Bot (@marge-bot) 14 Oct '26
by Marge Bot (@marge-bot) 14 Oct '26
14 Oct '26
Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC
Commits:
7df3ba2f by Andrea Vezzosi at 2026-10-14T06:15:48-04:00
add runGhcWithSignalHandlers and remove handling from runGhc[T]
it only ever makes sense to call `withSignalHandlers` from `main`, otherwise
signals can be too easily swallowed without notification to the rest of your
process.
This commit takes withSignalHandlers out of `runGhc/runGhcT` and provides
`runGhcWithSignalHandlers` for the old behaviour.
- - - - -
5bbee62c by Hannes Siebenhandl at 2026-10-14T06:48:08-04:00
Fix #27836, normalise unit database path in ExternalUnitDatabases
Add a regression test, proofing that all variations:
- local-unit-db/my-test.conf
- ./local-unit-db/my-test.conf
- ./local-unit-db/my-test.conf/
are parsed only once.
- - - - -
821e5730 by Florian Ragwitz at 2026-10-14T06:48:09-04:00
Remove obsolete HPC types
Fixes #27946.
- - - - -
32 changed files:
- + changelog.d/T4162
- + changelog.d/hpc-cleanup
- changelog.d/unit-index
- compiler/GHC.hs
- compiler/GHC/Core/Opt/Specialise.hs
- compiler/GHC/CoreToIface.hs
- compiler/GHC/CoreToStg.hs
- compiler/GHC/Data/OsPath.hs
- compiler/GHC/StgToJS/ExprCtx.hs
- compiler/GHC/Types/Id/Info.hs
- compiler/GHC/Unit/External/Database.hs
- compiler/GHC/Utils/Panic.hs
- ghc/Main.hs
- linters/lint-codes/LintCodes/Static.hs
- + testsuite/tests/driver/T27836/A.hs
- + testsuite/tests/driver/T27836/B.hs
- + testsuite/tests/driver/T27836/C.hs
- + testsuite/tests/driver/T27836/Makefile
- + testsuite/tests/driver/T27836/T27836.stdout
- + testsuite/tests/driver/T27836/all.T
- + testsuite/tests/driver/T27836/unitA
- + testsuite/tests/driver/T27836/unitB
- + testsuite/tests/driver/T27836/unitC
- + testsuite/tests/ghc-api/T4162.hs
- + testsuite/tests/ghc-api/T4162.stdout
- testsuite/tests/ghc-api/all.T
- utils/check-exact/Parsers.hs
- utils/check-ppr/Main.hs
- utils/count-deps/Main.hs
- utils/dump-decls/Main.hs
- utils/haddock/haddock-api/src/Haddock.hs
- utils/haddock/haddock-api/test/Haddock/Backends/Hyperlinker/ParserSpec.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/079b1aa931b7a8c1ae829f784744b6…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/079b1aa931b7a8c1ae829f784744b6…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/romes/async-rpc] 2 commits: Asynchronous RPC with external interpreter
by Rodrigo Mesquita (@alt-romes) 14 Oct '26
by Rodrigo Mesquita (@alt-romes) 14 Oct '26
14 Oct '26
Rodrigo Mesquita pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC
Commits:
ee89cbd5 by Rodrigo Mesquita at 2026-10-14T11:43:41+01:00
Asynchronous RPC with external interpreter
There's a single pipe to connect the host compiler to the external
interpreter. On this pipe, we have historically done synchronous
request/reply communication to invoke methods on the external
interpreter.
However, with the advent of multi-threaded debugging, it has become
necessary to do asynchronous calls on the external interpreter, where we
may block on the host waiting for a reply, but the external interpreter
should handle the request asynchronously and reply when ready. For
instance, we want to start the evaluation of the debuggee but not block
the interpreter waiting for it to finish or hit a breakpoint. Or e.g. we
want to resume two threads of the debuggee simulatenously instead of
blocking waiting for the resume result of each one sequentially.
And it turns out, we were already doing some ad-hoc asynchronous
communication to evaluate Template Haskell on the external interpreter.
It used to go as an ad-hoc "send request without expecting reply",
followed by handling on the host a set of 'THMessage's sent by the
interpreter, followed by "expect a response to arrive now". It worked
because there was still a fixed order of messages, but it fits an async
model in a non-ad-hoc way.
Asynchronous communication that unblocks the external interpreter main
thread will also make it possible to resolve issues such as #18852
(exceptions leaving the external interpreter in an unrecoverrable state)
The new design
--------------
The new invariant: every single request should receive a matching reply.
The pipe is bidirectional (the ext. interpreter can receive requests,
but it can also send requests for TH evaluation; the host can send
requests, but it can also receive TH requests from the interpreter).
When a request is sent in whichever direction, it always contains a
correlation-id header. The Sender places an empty MVar on a
pending-reply map keyed by id and waits on it for a response.
See 'remoteCall' and 'remoteTHCall'.
The pipe is associated to a thread which drains the read end. Because
it's bidirectional, two things may be read off the pipe:
- A Reply, with a correlation-id header, which we match against the
pending requests map to unblock the corresponding pending-reply MVar
- A Request, which is queued to the pending-request queue.
To handle a message, we take a Request from the pending-request queue
and run a handler that produces a Reply. Currently, the handler is run
synchronously in the thread, but it is trivial to make the handler run
in a forked thread for certain ("interruptible") messages in an upcoming
patch (when it is necessary for the multi-threaded debugger).
See 'handleRemoteCall' and 'handleRemoteTHCall'.
The old interp lock now just protect the pipe writing (reads don't lock
bc they are done exclusively by drainPipe), it doesn't need nor should
lock the full remote procedure call.
- - - - -
4771d003 by Rodrigo Mesquita at 2026-10-14T11:43:41+01:00
Close pipe cleans up pending requests and the other process can handle EOF and raise correct exceptions when pipe is read or written when closed
- - - - -
10 changed files:
- compiler/GHC/Runtime/Interpreter.hs
- compiler/GHC/Runtime/Interpreter/JS.hs
- compiler/GHC/Runtime/Interpreter/Process.hs
- compiler/GHC/Runtime/Interpreter/Types.hs
- compiler/GHC/Runtime/Interpreter/Wasm.hs
- compiler/GHC/Tc/Gen/Splice.hs
- libraries/ghci/GHCi/Message.hs
- libraries/ghci/GHCi/Server.hs
- libraries/ghci/ghci.cabal.in
- testsuite/tests/ghci/custom-external-interpreter-commands/Main.hs
Changes:
=====================================
compiler/GHC/Runtime/Interpreter.hs
=====================================
@@ -626,11 +626,9 @@ spawnIServ conf = do
[]
(iservConfOpts conf)
interpPipe <- mkPipeFromHandles rh wh
- lock <- newMVar ()
let process = InterpProcess
{ interpHandle = ph
, interpPipe
- , interpLock = lock
}
pending_frees <- newMVar []
=====================================
compiler/GHC/Runtime/Interpreter/JS.hs
=====================================
@@ -127,11 +127,9 @@ startTHRunnerProcess interp_js settings = do
std_in <- readIORef interp_in
interpPipe <- mkPipeFromHandles rh wh
- lock <- newMVar ()
let proc = InterpProcess
{ interpHandle = hdl
, interpPipe
- , interpLock = lock
}
pure (std_in, proc)
@@ -382,7 +380,7 @@ jsLinkPlan logger tmpfs tmp_dir ar_cache link_cfg cfg inst link_plan = do
-- Haskell server.
server_started <- jsServerStarted <$> readMVar (instJSState (instExtra inst))
if server_started
- then sendMessageNoResponse inst $ LoadObj all_js
+ then sendMessage inst $ LoadObj all_js
else jsLoadFile inst all_js
----------------------------------------------------------------
=====================================
compiler/GHC/Runtime/Interpreter/Process.hs
=====================================
@@ -2,16 +2,8 @@ module GHC.Runtime.Interpreter.Process
(
-- * Message API
Message(..)
- , DelayedResponse (..)
- -- * Top-level message API (these acquire/release a lock)
, sendMessage
- , sendMessageNoResponse
- , sendMessageDelayedResponse
- , receiveDelayedResponse
- -- * Nested message API (these require the interpreter to already be locked)
- , sendAnyValue
- , receiveAnyValue
- , receiveTHMessage
+ , sendTHComputation
)
where
@@ -27,97 +19,62 @@ import GHC.Utils.Exception as Ex
import Data.Binary
import System.Exit
import System.Process
-import Control.Concurrent.MVar (MVar, withMVar, takeMVar, putMVar, isEmptyMVar)
-
-data DelayedResponse a = DelayedResponse
+import Control.Concurrent
+import Data.IORef
+import Control.Concurrent.STM
-- -----------------------------------------------------------------------------
-- Top-level Message API
--- | Send a message to the interpreter process that doesn't expect a response
--- (locks the interpreter while sending)
-sendMessageNoResponse :: ExtInterpInstance d -> Message () -> IO ()
-sendMessageNoResponse i m =
- withLock i $ writeInterpProcess (instProcess i) (putMessage m)
-
--- | Send a message to the interpreter that expects a response
--- (locks the interpreter while until the response is received)
+-- | Send a message to the interpreter and expect a response, blocking until it arrives.
+--
+-- This issues an asynchronous RPC on the external interpreter which does not
+-- block the external interpreter thread. We block here on the host because we
+-- wait for the asynchronous response to arrive.
sendMessage :: Binary a => ExtInterpInstance d -> Message a -> IO a
-sendMessage i m = withLock i $ callInterpProcess (instProcess i) m
-
--- | Send a message to the interpreter process whose response is expected later
+sendMessage i msg =
+ remoteCall (interpPipe proc) msg
+ `catchException` \(e :: SomeException) -> handleInterpProcessFailure proc e
+ where
+ proc = instProcess i
+
+-- | Send a message with a TH computation to the ext. interpreter (like
+-- 'RunTH', or 'RunModFinalizers') and handle 'THMessage' requests from the
+-- ext. interpreter to us in the meantime.
--
--- This is useful to avoid forgetting to receive the value and to ensure that
--- the type of the response isn't lost. Use receiveDelayedResponse to read it.
--- (locks the interpreter until the response is received using
--- `receiveDelayedResponse`)
-sendMessageDelayedResponse :: ExtInterpInstance d -> Message a -> IO (DelayedResponse a)
-sendMessageDelayedResponse i m = do
- lock i
- writeInterpProcess (instProcess i) (putMessage m)
- pure DelayedResponse
-
--- | Expect a delayed result to be received now
-receiveDelayedResponse :: Binary a => ExtInterpInstance d -> DelayedResponse a -> IO a
-receiveDelayedResponse i DelayedResponse = do
- ensureLocked i
- r <- readInterpProcess (instProcess i) get
- unlock i
- pure r
-
--- -----------------------------------------------------------------------------
--- Nested Message API
-
--- | Send any value (requires locked interpreter)
-sendAnyValue :: Binary a => ExtInterpInstance d -> a -> IO ()
-sendAnyValue i m = ensureLocked i >> writeInterpProcess (instProcess i) (put m)
-
--- | Expect a value to be received (requires locked interpreter)
-receiveAnyValue :: ExtInterpInstance d -> Get a -> IO a
-receiveAnyValue i get = ensureLocked i >> readInterpProcess (instProcess i) get
-
--- | Wait for a Template Haskell message (requires locked interpreter)
-receiveTHMessage :: ExtInterpInstance d -> IO THMsg
-receiveTHMessage i = ensureLocked i >> receiveAnyValue i getTHMessage
-
--- -----------------------------------------------------------------------------
-
-getLock :: ExtInterpInstance d -> MVar ()
-getLock = interpLock . instProcess
-
-withLock :: ExtInterpInstance d -> IO a -> IO a
-withLock i f = withMVar (getLock i) (const f)
-
-lock :: ExtInterpInstance d -> IO ()
-lock i = takeMVar (getLock i)
-
-unlock :: ExtInterpInstance d -> IO ()
-unlock i = putMVar (getLock i) ()
-
-ensureLocked :: ExtInterpInstance d -> IO ()
-ensureLocked i =
- isEmptyMVar (getLock i) >>= \case
- False -> panic "ensureLocked: external interpreter not locked"
- _ -> pure ()
-
-
--- | Send a 'Message' and receive the response from the interpreter process
-callInterpProcess :: Binary a => InterpProcess -> Message a -> IO a
-callInterpProcess i msg =
- remoteCall (interpPipe i) msg
- `catchException` \(e :: SomeException) -> handleInterpProcessFailure i e
-
--- | Read a value from the interpreter process
-readInterpProcess :: InterpProcess -> Get a -> IO a
-readInterpProcess i get =
- readPipe (interpPipe i) get
- `catchException` \(e :: SomeException) -> handleInterpProcessFailure i e
-
--- | Send a value to the interpreter process
-writeInterpProcess :: InterpProcess -> Put -> IO ()
-writeInterpProcess i put =
- writePipe (interpPipe i) put
- `catchException` \(e :: SomeException) -> handleInterpProcessFailure i e
+-- This function blocks until the TH computation returns a value, but spawns in
+-- the background a thread to handle the 'THMessage's until the TH computation
+-- is fully done.
+sendTHComputation
+ :: Binary a
+ => ExtInterpInstance d
+ -> Message a
+ -> (forall b. Binary b => THMessage b -> IO (Bool, b))
+ -- ^ Handle one message. Return True to end the loop.
+ -- This handler must never uninterrutibly mask an expression which may get
+ -- stuck. If it does, then 'sendTHComputation' will also get uninterruptibly stuck.
+ -> IO a
+sendTHComputation i msg reqHandler = mask $ \restore -> do
+ -- inline "withAsync/wait"
+ var <- newEmptyTMVarIO
+ ht <- forkFinally (restore handlerLoop) (atomically . putTMVar var)
+ restore (sendMessage i msg <* atomically (readTMVar var))
+ `onException` uninterruptibleMask_ (throwTo ht CancelTHHandler)
+ where
+ proc = instProcess i
+ handleOne doneRef = do
+ handleRemoteTHCall (interpPipe proc) $ \req -> do
+ (stop, res) <- reqHandler req
+ writeIORef doneRef stop
+ return res
+ handlerLoop = do
+ doneRef <- newIORef False
+ let loop = do
+ handleOne doneRef
+ isDone <- readIORef doneRef
+ if isDone then return ()
+ else loop
+ in loop
handleInterpProcessFailure :: InterpProcess -> SomeException -> IO a
handleInterpProcessFailure i e = do
@@ -130,3 +87,8 @@ handleInterpProcessFailure i e = do
terminateProcess hdl
_ <- waitForProcess hdl
throw e
+
+data CancelTHHandler = CancelTHHandler deriving Show
+instance Exception CancelTHHandler where
+ fromException = asyncExceptionFromException
+ toException = asyncExceptionToException
=====================================
compiler/GHC/Runtime/Interpreter/Types.hs
=====================================
@@ -111,7 +111,6 @@ type WasmInterp = ExtInterpState WasmInterpConfig ()
data InterpProcess = InterpProcess
{ interpPipe :: !Pipe -- ^ Pipe to communicate with the server
, interpHandle :: !ProcessHandle -- ^ Process handle of the server
- , interpLock :: !(MVar ()) -- ^ Lock to prevent concurrent access to the stream
}
-- | Status of an external interpreter
=====================================
compiler/GHC/Runtime/Interpreter/Wasm.hs
=====================================
@@ -73,14 +73,12 @@ spawnWasmInterp WasmInterpConfig {..} = do
hSetBuffering rh NoBuffering
interpPipe <- mkPipeFromHandles rh wh
pending_frees <- newMVar []
- lock <- newMVar ()
pure
$ ExtInterpInstance
{ instProcess =
InterpProcess
{ interpHandle = ph,
- interpPipe,
- interpLock = lock
+ interpPipe
},
instPendingFrees = pending_frees,
instExtra = ()
=====================================
compiler/GHC/Tc/Gen/Splice.hs
=====================================
@@ -170,6 +170,7 @@ import Data.IORef
import GHC.Parser.HaddockLex (lexHsDoc)
import GHC.Parser (parseIdentifier)
import GHC.Rename.Doc (rnHsDoc)
+import Data.Function
{-
Note [Template Haskell state diagram]
@@ -1165,11 +1166,12 @@ runRemoteModFinalizers (ThModFinalizers finRefs) = do
case th_state of
Nothing -> return () -- TH was not started, nothing to do
Just fhv -> do
- r <- liftIO $ withForeignRef fhv $ \st ->
- withForeignRefs finRefs $ \qrefs ->
- sendMessageDelayedResponse inst (RunModFinalizers st qrefs)
- () <- runRemoteTH inst []
- qr <- liftIO $ receiveDelayedResponse inst r
+ qr <- withRunInIO $ \runInIO -> do
+ recovers <- newIORef []
+ withForeignRef fhv $ \st ->
+ withForeignRefs finRefs $ \qrefs ->
+ sendTHComputation inst (RunModFinalizers st qrefs) $ \msg ->
+ runInIO (runRemoteTH recovers msg)
checkQResult qr
runQResult
@@ -1824,48 +1826,47 @@ runTH ty fhv = do
rstate <- getTHState inst
loc <- location
-- run a remote TH request
- r <- liftIO $
+ qr <- withRunInIO $ \runInIO -> do
+ recovers <- newIORef []
withForeignRef rstate $ \state_hv ->
- withForeignRef fhv $ \q_hv ->
- sendMessageDelayedResponse inst (RunTH state_hv q_hv ty (Just loc))
- -- respond to requests from the interpreter
- runRemoteTH inst []
- -- get the final result
- qr <- liftIO $ receiveDelayedResponse inst r
+ withForeignRef fhv $ \q_hv ->
+ sendTHComputation inst (RunTH state_hv q_hv ty (Just loc)) $ \msg ->
+ runInIO (runRemoteTH recovers msg)
bs <- checkQResult qr
return $! runGet get (LB.fromStrict bs)
--- | communicate with a remotely-running TH computation until it finishes.
+-- | Communicate with a remotely-running TH computation until it finishes.
-- See Note [Remote Template Haskell] in libraries/ghci/GHCi/TH.hs.
runRemoteTH
- :: ExtInterpInstance d
- -> [Messages TcRnMessage] -- saved from nested calls to qRecover
- -> TcM ()
-runRemoteTH inst recovers = do
- THMsg msg <- liftIO $ receiveTHMessage inst
+ :: Binary a
+ => IORef [Messages TcRnMessage] -- ^ saved from nested calls to qRecover
+ -> THMessage a
+ -> TcM (Bool, a)
+runRemoteTH recovers msg = do
case msg of
- RunTHDone -> return ()
+ RunTHDone -> return (True, ())
StartRecover -> do -- Note [TH recover with -fexternal-interpreter]
v <- getErrsVar
msgs <- readTcRef v
writeTcRef v emptyMessages
- runRemoteTH inst (msgs : recovers)
+ modifyIORef recovers (msgs:) & liftIO
+ return (False, ())
EndRecover caught_error -> do
- let (prev_msgs, rest) = case recovers of
- [] -> panic "EndRecover"
- a : b -> (a,b)
+ (prev_msgs, rest) <- liftIO $
+ readIORef recovers >>= \case
+ [] -> panic "EndRecover"
+ a : b -> pure (a,b)
v <- getErrsVar
warn_msgs <- getWarningMessages <$> readTcRef v
-- keep the warnings only if there were no errors
writeTcRef v $ if caught_error
then prev_msgs
else mkMessages warn_msgs `unionMessages` prev_msgs
- runRemoteTH inst rest
+ writeIORef recovers rest & liftIO
+ return (False, ())
_other -> do
- r <- handleTHMessage msg
- liftIO $ sendAnyValue inst r
- runRemoteTH inst recovers
+ (False,) <$> handleTHMessage msg
-- | Check a QResult
checkQResult :: QResult a -> TcM a
=====================================
libraries/ghci/GHCi/Message.hs
=====================================
@@ -1,6 +1,7 @@
-{-# LANGUAGE GADTs, DeriveGeneric, StandaloneDeriving, ScopedTypeVariables,
- GeneralizedNewtypeDeriving, ExistentialQuantification, RecordWildCards,
- CPP, NamedFieldPuns #-}
+{-# LANGUAGE RecursiveDo, GADTs, DeriveGeneric, StandaloneDeriving, ScopedTypeVariables,
+ GeneralizedNewtypeDeriving, ExistentialQuantification, RecordWildCards, CPP,
+ NamedFieldPuns, LambdaCase, TypeAbstractions, TypeApplications,
+ LinearTypes, RankNTypes, UnboxedTuples, BlockArguments, TupleSections, MagicHash #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing -fno-warn-orphans #-}
-- |
@@ -22,10 +23,25 @@ module GHCi.Message
, ResumeContext(..)
, QState(..)
, getMessage, putMessage, getTHMessage, putTHMessage
- , Pipe, mkPipeFromHandles, mkPipeFromContinuations, remoteCall, remoteTHCall, readPipe, writePipe
+
+ , Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe
+ , remoteCall, remoteTHCall
+ , handleRemoteCall, handleRemoteTHCall
+ , RemoteProcessException(..)
+
, BreakModule
, BreakUnitId
, LoadedDLL
+
+ -- * Low-level interface for async bidirectional RPC
+ , remoteAsyncReq
+ , handleAsyncReq
+ -- ** Consume `reply` exactly once
+ , WriteReply -- abstract
+ , writeReply
+ -- ** Async matching internals
+ , AsyncManager(..), CorrelationId
+ , IddMsg(..), newAsyncManager
) where
import Prelude -- See note [Why do we import Prelude here?]
@@ -45,6 +61,7 @@ import qualified GHC.Exts.Heap as Heap
import GHC.ForeignSrcLang
import GHC.Fingerprint
import GHC.Conc (pseq, par)
+import GHC.IO (IO(..))
import Control.Concurrent
import Control.DeepSeq
import Control.Exception
@@ -63,6 +80,8 @@ import Data.Dynamic
import Data.Typeable (TypeRep)
import Data.IORef
import Data.Map (Map)
+import Data.IntMap (IntMap)
+import qualified Data.IntMap as IntMap
import Foreign
import GHC.Generics
import GHC.Stack.CCS
@@ -71,6 +90,11 @@ import qualified GHC.Boot.TH.Monad as TH
import System.Exit
import System.IO
import System.IO.Error
+import Control.Monad
+import Control.Concurrent.STM
+import Data.Bifunctor
+import GHC.Base (State#)
+import GHC.Exts (RealWorld)
-- -----------------------------------------------------------------------------
-- The RPC protocol between GHC and the interactive server
@@ -693,50 +717,298 @@ serializeBCOs rbcos = parMap doChunk (chunkList 100 rbcos)
-- | An opaque pipe for bidirectional binary data transmission.
data Pipe = Pipe
- { getSome :: !(IO ByteString)
- , putAll :: !(B.Builder -> IO ())
- , pipeLeftovers :: !(IORef (Maybe ByteString))
+ { getSome :: !(IO ByteString)
+ , putAll :: !(B.Builder -> IO ())
+ , pipeLeftovers :: !(IORef (Maybe ByteString))
+ , pipeLock :: !(MVar ()) -- ^ Lock to prevent concurrent writes to the stream
+ , pipeAsyncMngr :: !AsyncManager
+ , pipeCleanupRW :: !(IO ())
+ -- ^ Clean up the read/write resources, e.g. closes the handles if 'mkPipeFromHandles'
+ , pipeIsClosed :: !(TVar Bool)
}
-- | Make a 'Pipe' from a 'Handle' to read and a 'Handle' to write.
+-- Must call 'closePipe' at the end to clean up and close the handles.
mkPipeFromHandles :: Handle -> Handle -> IO Pipe
mkPipeFromHandles pipeRead pipeWrite = do
let getSome = B.hGetSome pipeRead (32*1024)
putAll b = do
B.hPutBuilder pipeWrite b
hFlush pipeWrite
- pipeLeftovers <- newIORef Nothing
- pure $ Pipe { getSome, putAll, pipeLeftovers }
+ mkPipeFromContinuations getSome putAll (hClose pipeRead >> hClose pipeWrite)
+
+-- | Make a 'Pipe' from a reader function and a writer function, plus an action
+-- to clean up the reader and writer resources. The pipe should be closed with
+-- 'closePipe', which will call this clean up action too.
+mkPipeFromContinuations :: IO ByteString -> (B.Builder -> IO ()) -> IO () -> IO Pipe
+mkPipeFromContinuations getSome putAll pipeCleanupRW = mdo
+ pipeLeftovers <- newIORef Nothing
+ pipeLock <- newMVar ()
+ pipeIsClosed <- newTVarIO False
+ mdo
+ pipeAsyncMngr <- newAsyncManager p
+ let p = Pipe { .. }
+ pure p
+
+-- | Cleanly close our end of the Pipe, terminating the async manager's
+-- drain-read-end thread. (This will close the handles if created from
+-- 'mkPipeFromHandles').
+--
+-- The remote process does not necessarily detect that this end of the pipe was
+-- closed, but it must handle cleanly e.g. a handle being closed (perhaps by
+-- closing the pipe on its end too)
+--
+-- The requests we've sent that are pending are all canceled with
+-- 'PipeClosedException' by ourselves.
+closePipe :: Pipe -> IO ()
+closePipe Pipe{..} = do
+
+ -- releases handle lock taken by reading, and cleans up by replying to every
+ -- pending request with 'PipeClosedException'.
+ throwTo (asyncDrainTid pipeAsyncMngr) PipeClosedException
--- | Make a 'Pipe' from a reader function and a writer function.
-mkPipeFromContinuations :: IO ByteString -> (B.Builder -> IO ()) -> IO Pipe
-mkPipeFromContinuations getSome putAll = do
- pipeLeftovers <- newIORef Nothing
- pure $ Pipe { getSome, putAll, pipeLeftovers }
+ pipeCleanupRW -- hClose won't deadlock now that the drainPipe thread is killed
+-- | Send a 'Message' request to and wait for a reply from the interpreter
+-- (blocks on the calling side, doesn't necessarily block the interpreter)
remoteCall :: Binary a => Pipe -> Message a -> IO a
-remoteCall pipe msg = do
- writePipe pipe (putMessage msg)
- readPipe pipe get
+remoteCall pipe msg = either throwIO (pure . runGet get)
+ =<< remoteAsyncReq pipe (putMessage msg)
+
+-- | Handle a single 'Message' request on the interpreter and reply to the host
+handleRemoteCall :: Pipe
+ -> (Msg -> IO Msg) {-^ Msg hook -}
+ -> (forall a. (Show a, Binary a) => Message a -> IO a)
+ -> IO ()
+handleRemoteCall pipe hook k =
+ handleAsyncReq pipe getMessage \writer msg0 -> IO \s0 ->
+ case hook msg0 |#> s0 of -- unIO for linearity checking
+ (# s1, Msg msg #) -> case k msg |#> s1 of
+ (# s2, a #) -> writeReply writer (put a) |#> s2
+
+-- | Send a 'THMessage' request to the host and wait for a reply from the host
+-- to the interpreter
+remoteTHCall :: Binary a => Pipe -> THMessage a -> IO a
+remoteTHCall pipe msg = either throwIO (pure . runGet get)
+ =<< remoteAsyncReq pipe (putTHMessage msg)
+
+-- | Handle a 'THMessage' request on the host and reply to the interpreter.
+handleRemoteTHCall
+ :: Pipe -> (forall a. (Show a, Binary a) => THMessage a -> IO a) -> IO ()
+handleRemoteTHCall pipe k =
+ handleAsyncReq pipe getTHMessage \writer (THMsg msg) -> IO \s0 ->
+ case k msg |#> s0 of -- unIO for linearity checking
+ (# s1, a #) -> writeReply writer (put a) |#> s1
+
+--------------------------------------------------------------------------------
+-- * Matching requests with async replies
+--------------------------------------------------------------------------------
+
+-- | Send an asynchronous request and block waiting for a matching reply
+remoteAsyncReq :: Pipe -> Put -> IO (Either SomeException LB.ByteString)
+remoteAsyncReq pipe msg = do
+
+ let pendingReplies = pendingReply (pipeAsyncMngr pipe)
+
+ isClosed <- readTVarIO (pipeIsClosed pipe)
+ when isClosed $
+ throwIO PipeClosedException
+
+ -- Mark pending request
+ uq <- freshReqId (pipeAsyncMngr pipe)
+ wv <- newEmptyMVar
+ atomicModifyIORef' pendingReplies $
+ \m -> (IntMap.insert uq wv m, ())
+
+ -- Write request
+ writePipe pipe (Request uq (runPut msg))
+ `onException`
+ -- the req wasn't done, or no one will be reading the reply:
+ -- in either case, drop it from the pending map.
+ atomicModifyIORef' pendingReplies
+ (\m -> (IntMap.delete uq m, ()))
+
+ -- Block waiting for reply
+ takeMVar wv
+
+-- | Receive a single asynchronous request and send a matching reply
+-- asynchronously (the handler may fork a computation that eventually replies
+-- and can return before actually replying, as long as it eventually does).
+--
+-- The handler receives as an argument the function to use to reply to the request.
+-- It must be used exactly once (all requests expect a reply, and you can't
+-- reply more than once). Linear types enforce the handler replies in every
+-- branch, and if the handler otherwise throws, it is handled by 'withAsyncHandler'.
+handleAsyncReq :: Pipe -> Get a -> (WriteReply Put %1 -> a -> IO r) -> IO r
+handleAsyncReq pipe getit k = do
+ ack <- newEmptyMVar
+
+ isOver <- atomically $ do
+ isClosed <- readTVar (pipeIsClosed pipe)
+ isEmpty <- isEmptyTQueue (requestsQueue (pipeAsyncMngr pipe))
+ return (isClosed && isEmpty)
+
+ when isOver $ do
+ throwIO PipeClosedException
+
+ -- Block waiting for request
+ (uq, bs) <- atomically $ readTQueue (requestsQueue (pipeAsyncMngr pipe))
+
+ let reply p = do
+ writePipe pipe (Reply uq (bimap toSerializableException runPut p))
+ putMVar ack ()
+
+ -- Run the continuation which must reply exactly once with 'writeReply'
+ case runGetOrFail getit bs of
+ Left (_, _, err) -> do
+ let exc = DecodeAsyncReqException err
+ reply (Left (toException exc))
+ throwIO exc
+ Right (_, _, it) ->
+ k (WriteReply reply) it
+
+ -- If there's an exception before the reply is sent, reply with an error instead.
+ `catch` \(e :: SomeException) -> do
+
+ tryTakeMVar ack >>= \case
+ Just () ->
+ -- Already replied
+ throwIO e
+ Nothing -> do
+ -- Reply with an error
+ reply (Left e)
+ throwIO e
+
+-- | An exception that can be thrown on the host process representing an
+-- expection that happened on the remote one.
+newtype RemoteProcessException = RemoteProcessException SerializableException deriving Show
+instance Exception RemoteProcessException
+
+-- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
+newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
+instance Exception DecodeAsyncReqException
+
+-- | We've closed the pipe on our end and we terminate all pending requests with this.
+data PipeClosedException = PipeClosedException deriving Show
+instance Exception PipeClosedException
+
+-- ** Async matching "internals" -----------------------------------------------
+
+data AsyncManager = AsyncManager
+ { pendingReply :: !(IORef (IntMap (MVar (Either SomeException LB.ByteString))))
+ -- ^ When a request is sent, keep track that we're expecting a reply. The
+ -- MVar is signaled when the reply arrives. The exception can originate
+ -- from the host process, or from the remote one (with 'RemoteProcessException').
+ , requestsQueue :: !(TQueue (CorrelationId, LB.ByteString))
+ -- ^ A queue of pending received requests. When a request is received,
+ -- queue it here. A request handler will pop the next request from it.
+ -- (Nothing except the 'drainPipe' thread should read from the pipe).
+ , asyncDrainTid :: !ThreadId
+ , asyncNextReq :: !(IORef Int)
+ }
-writePipe :: Pipe -> Put -> IO ()
-writePipe Pipe{..} put = putAll $ execPut put
+type CorrelationId = Int
-remoteTHCall :: Binary a => Pipe -> THMessage a -> IO a
-remoteTHCall pipe msg = do
- writePipe pipe (putTHMessage msg)
- readPipe pipe get
+-- | We have a single type for requests and replies because both the host and
+-- interpreter may send requests *and* replies on the write end *and* on recv
+-- both on the read end.
+--
+-- For instance:
+-- 1. The host sends a RunTH request on its Host Write End (HWE)
+-- 2. The interpreter sends a NewName TH request on its Interpreter Write End (IWE)
+-- 3. The host sends a reply to the TH request on its HWE
+-- 4. The interpreter sends the reply to the RunTH request on its IWE
+data IddMsg = Request !CorrelationId LB.ByteString
+ | Reply !CorrelationId (Either SerializableException LB.ByteString)
+
+instance Binary IddMsg where
+ put (Request i bs) = putWord8 0 >> put i >> put bs
+ put (Reply i bs) = putWord8 1 >> put i >> put bs
+ get = do
+ b <- getWord8
+ case b of
+ 0 -> Request <$> get <*> get
+ 1 -> Reply <$> get <*> get
+ _ -> error $ "impossible: Binary IddMsg - " ++ show b
+
+newAsyncManager :: Pipe -> IO AsyncManager
+newAsyncManager pipe = do
+ asyncNextReq <- newIORef 0
+ pendingReply <- newIORef IntMap.empty
+ requestsQueue <- newTQueueIO
+
+ -- Fork thread to drain read end.
+ -- (cleaned up on 'closePipe')
+ asyncDrainTid <- forkIO (drainPipe pipe)
-readPipe :: Pipe -> Get a -> IO a
-readPipe Pipe{..} get = do
+ let am = AsyncManager{..}
+ pure am
+
+-- | Drains the read end of the Pipe and match 'Reply'es against pending 'Request's.
+--
+-- In pratice, we'll have both a thread draining the read end of the host's
+-- 'Pipe' and a thread draining the read end of the interpreter's 'Pipe', since
+-- both processes construct a 'Pipe' and send and receive messages
+drainPipe :: Pipe -> IO ()
+drainPipe p = catch loop cancelPending where
+ loop = do
+ readPipe p >>= \case
+ -- Receive a reply to a pending notification
+ Just (Reply uq bs) -> do
+ mwv <- atomicModifyIORef' (pendingReply am) $ \m -> do
+ case IntMap.lookup uq m of
+ Nothing -> (m, Nothing)
+ Just wv -> (IntMap.delete uq m, Just wv)
+ case mwv of
+ Just wv ->
+ putMVar wv (bimap (toException . RemoteProcessException) id bs) -- signal matching request
+ Nothing ->
+ -- the req was dropped from pending bc there was an exception, and no
+ -- one's listening any more. the reply is ignored
+ pure ()
+ loop
+ -- Receive a request and queue it
+ Just (Request uq bs) -> do
+ atomically $ writeTQueue (requestsQueue am) (uq,bs)
+ loop
+ -- EOF on read handle
+ Nothing -> do
+ atomically $ writeTVar (pipeIsClosed p) True
+ pure () -- stops thread
+
+ cancelPending (e::SomeException) = do
+ pendings <- readIORef (pendingReply am)
+ mapM_ (\p -> putMVar p (Left e {-host exception-})) pendings
+
+ am = pipeAsyncMngr p
+
+freshReqId :: AsyncManager -> IO CorrelationId
+freshReqId AsyncManager{asyncNextReq} =
+ atomicModifyIORef' asyncNextReq $ \i -> (i+1, i)
+
+--------------------------------------------------------------------------------
+-- * Pipe thread-safe read/write
+--------------------------------------------------------------------------------
+-- All messages on the wire are 'IddMsg', which contain a correlation header
+-- and identify whether they are requests or replies.
+
+writePipe :: Pipe -> IddMsg -> IO ()
+writePipe p@Pipe{..} = withLock p . putAll . execPut . put
+
+-- | Only the 'drainPipe' thread should read from the pipe directly. Other
+-- functions should wait for the matching reply to be delivered asynchronously.
+-- (see 'withAsyncReq'). It doesn't lock to read bc it assumes single reader is 'drainPipe'.
+--
+-- Returns @Nothing@ on EOF.
+readPipe :: Pipe -> IO (Maybe IddMsg)
+readPipe Pipe{..} = do
leftovers <- readIORef pipeLeftovers
m <- getBin getSome get leftovers
case m of
- Nothing -> throw $
- mkIOError eofErrorType "GHCi.Message.readPipe" Nothing Nothing
+ Nothing -> return Nothing
Just (result, new_leftovers) -> do
writeIORef pipeLeftovers new_leftovers
- return result
+ return (Just result)
getBin
:: (IO ByteString) -> Get a -> Maybe ByteString
@@ -758,3 +1030,22 @@ getBin getsome get leftover = go leftover (runGetIncremental get)
else go Nothing (fun (Just b))
go _lft (Fail _rest _off str) =
throwIO (ErrorCall ("getBin: " ++ str))
+
+withLock :: Pipe -> IO c -> IO c
+withLock Pipe{..} = bracket (takeMVar pipeLock) (putMVar pipeLock) . const
+
+--------------------------------------------------------------------------------
+-- * Consume `reply` exactly once
+--------------------------------------------------------------------------------
+
+-- | Every request should get exactly one reply.
+data WriteReply a where
+ WriteReply :: (Either SomeException a -> IO ()) %1 -> WriteReply a
+
+-- | Write a reply exactly once (consuming the ability to write more replies)
+writeReply :: WriteReply a %1 -> a -> IO ()
+writeReply (WriteReply r) = \x -> r (Right x)
+
+-- | Infix, linear, 'unIO'
+(|#>) :: IO a %1 -> State# RealWorld -> (# State# RealWorld, a #)
+(|#>) (IO fs) s = fs s
=====================================
libraries/ghci/GHCi/Server.hs
=====================================
@@ -1,4 +1,4 @@
-{-# LANGUAGE CPP, RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables #-}
+{-# LANGUAGE LambdaCase, CPP, RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables #-}
module GHCi.Server
( MessageHook
, CustomMessageHandler
@@ -15,6 +15,7 @@ import GHCi.Signals
import GHCi.TH
import GHCi.Message
import Data.ByteString (ByteString)
+import Data.IORef
#if defined(wasm32_HOST_ARCH)
import qualified Data.ByteString.Builder as B
import qualified Data.ByteString.Internal as B
@@ -60,27 +61,26 @@ servWithCustom
-> (forall a .IO a -> IO a)
-> CustomMessageHandler
-> IO ()
-servWithCustom verbose hook pipe restore customHandler = loop
+servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop
where
- loop = do
+ loop doneRef = do
when verbose $ trace "reading pipe..."
- Msg msg <- readPipe pipe getMessage >>= hook
+ handleRemoteCall pipe hook $ \(msg :: Message a) -> do
- discardCtrlC
+ discardCtrlC
- when verbose $ trace ("msg: " ++ (show msg))
- case msg of
- CustomMessage tag payload -> handleCustom tag payload
- Shutdown -> return ()
- RunTH st q ty loc -> wrapRunTH $ runTH pipe st q ty loc
- RunModFinalizers st qrefs -> wrapRunTH $ runModFinalizerRefs pipe st qrefs
- _other -> run msg >>= reply
+ when verbose $ trace ("msg: " ++ (show msg))
+ case msg of
+ CustomMessage tag payload -> handleCustom tag payload
+ Shutdown -> writeIORef doneRef True >> reply ()
+ -- TODO: These two should probably be done off the main thread
+ RunTH st q ty loc -> wrapRunTH $ runTH pipe st q ty loc
+ RunModFinalizers st qrefs -> wrapRunTH $ runModFinalizerRefs pipe st qrefs
+ _other -> run msg >>= reply
- reply :: forall a. (Binary a, Show a) => a -> IO ()
- reply r = do
- when verbose $ trace ("writing pipe: " ++ show r)
- writePipe pipe (put r)
- loop
+ isDone <- readIORef doneRef
+ if isDone then closePipe
+ else loop doneRef
handleCustom tag payload = do
mresp <- customHandler tag payload
@@ -89,17 +89,22 @@ servWithCustom verbose hook pipe restore customHandler = loop
Nothing ->
error $ "GHCi.Server: unhandled CustomMessage with tag " ++ show tag
+ reply :: forall a. Show a => a -> IO a
+ reply r = do
+ when verbose $ trace ("writing pipe: " ++ show r)
+ return r -- the result of the handler gets sent
+
-- Run some TH code, which may interact with GHC by sending
-- THMessage requests, and then finally send RunTHDone followed by a
-- QResult. For an overview of how TH works with Remote GHCi, see
-- Note [Remote Template Haskell] in libraries/ghci/GHCi/TH.hs.
- wrapRunTH :: forall a. (Binary a, Show a) => IO a -> IO ()
+ wrapRunTH :: forall a. (Binary a, Show a) => IO a -> IO (QResult a)
wrapRunTH io = do
when verbose $ trace "wrapRunTH..."
r <- try io
when verbose $ trace "wrapRunTH done."
when verbose $ trace "writing RunTHDone."
- writePipe pipe (putTHMessage RunTHDone)
+ remoteTHCall pipe RunTHDone
case r of
Left e
| Just (GHCiQException _ err) <- fromException e -> do
@@ -179,7 +184,7 @@ defaultServerWithCustom customHandler = do
#if defined(wasm32_HOST_ARCH)
-- See Note [wasm ghci signal handlers] for details
installSignalHandlers $ js_register_signal_handler cb_sig
- pipe <- mkPipeFromContinuations (recv_buf cb_recv) (send_buf cb_send)
+ pipe <- mkPipeFromContinuations (recv_buf cb_recv) (send_buf cb_send) (pure ())
#else
when verbose $
printf "GHC iserv starting (in: %s; out: %s)\n" (show inh) (show outh)
=====================================
libraries/ghci/ghci.cabal.in
=====================================
@@ -95,7 +95,8 @@ library
filepath >= 1.4 && < 1.6,
ghc-boot == @ProjectVersionMunged@,
ghc-heap >= 9.10.1 && <=@ProjectVersionMunged@,
- transformers >= 0.5 && < 0.7
+ transformers >= 0.5 && < 0.7,
+ stm >= 2.4 && < 2.6
if flag(bootstrap)
build-depends:
=====================================
testsuite/tests/ghci/custom-external-interpreter-commands/Main.hs
=====================================
@@ -12,6 +12,7 @@ import GHCi.Message
, mkPipeFromHandles
, remoteCall
, Pipe
+ , closePipe
)
import GHCi.Server
( CustomMessageHandler
@@ -153,14 +154,13 @@ customMessage pipe c = do
runClient :: Int -> IO ()
runClient input = do
serverExe <- getExecutablePath
- withServer serverExe $ \hFromServer hToServer -> do
- pipe <- mkPipeFromHandles hFromServer hToServer
+ withServer serverExe $ \pipe -> do
res <- customMessage pipe (SquareCommand input)
putStrLn $ "Square returned: " ++ show res
res2 <- customMessage pipe (MulCommand 2 res)
putStrLn $ "Mul returned: " ++ show res2
-withServer :: FilePath -> (Handle -> Handle -> IO a) -> IO a
+withServer :: FilePath -> (Pipe -> IO a) -> IO a
withServer serverExe action = do
(ghcRead, serverWrite) <- createPipe
(serverRead, ghcWrite) <- createPipe
@@ -171,13 +171,12 @@ withServer serverExe action = do
, std_out = Inherit
, std_err = Inherit
}
- bracket (mkHandles ghcRead ghcWrite)
- (\(hFromServer, hToServer) -> do
- hClose hFromServer
- hClose hToServer
+ bracket (mkHandles ghcRead ghcWrite >>= uncurry mkPipeFromHandles)
+ (\pipe -> do
+ closePipe pipe
terminateProcess ph
void (waitForProcess ph))
- (\(hFromServer, hToServer) -> action hFromServer hToServer)
+ (\pipe -> action pipe)
where
mkHandles r w = do
hR <- fdToHandle r
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/9d19106179bdf00b60e163603e5a51…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/9d19106179bdf00b60e163603e5a51…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/rafl/hpc-viewpatterns] 5 commits: Line up simpleUnifyCheck and check_ty_eq_rhs
by Florian Ragwitz (@rafl) 14 Oct '26
by Florian Ragwitz (@rafl) 14 Oct '26
14 Oct '26
Florian Ragwitz pushed to branch wip/rafl/hpc-viewpatterns at Glasgow Haskell Compiler / GHC
Commits:
97adaf89 by Simon Peyton Jones at 2026-10-14T00:07:27+01:00
Line up simpleUnifyCheck and check_ty_eq_rhs
Even after #26543 was allegedly fixed, the original repo case in the
Description continued to fail. The reason was that the QuickLook
unifier, `qlUnify`, used `simpleUnifyCheck` for checking unification
invariants; and `simpleUnifyCheck` conservatively rejected any RHS
with a coercion hole in it. The upshot was that QuickLook was not
as clever as it should be, wrongly failing to do an impredicative
instantiation.
Interestingly `check_ty_eq_rhs`, which does the same job during
constraint solving, was more liberal: it just looked at the free vars
of the coercion, and allowed coercion holes.
This MR lines them up, adding some careful notes. See
Note [simpleUnifyCheck]
Note [Occurs check and coercions]
Note [Unification preconditions] esp (COERCIONS)
Things are better than before, but I am still uncomfortable about the
possibilty that a hole might be filled with a coercion that mentions
the LHS type variable; for now I have left this discomfort documented
in Note [Occurs check and coercions].
Main changes
* `qlUnify` now only unifies /instantiation variables/ not regular unification
variables. This makes QuickLook more predictable. See (UQL6) in
Note [QuickLook unification].
* `qlUnify` no longer needs to call `simpleUnifyCheck`, so we can get rid of
`UC_QuickLook` and its code in `simpleUnifyCheck`.
* I found I could get rid of `mkOccFolders` in favour of `anyFreeVarsOfType`.
Hooray.
* Improve documentation of CycleBreakerTv, and rename it to GivenCycleBreakerTv
because they are only uesd for Givens!
* I accepted a 9% worsening in compile time allocation on T5030
because the newly-consistent story triggered #27934.
Other changes
* I refactored `atvFolder` so that it pairs the "interesting-var" function
with the in-scope set, to avoid building a closure with a free variable.
Now we get nice tight loops for `anyFreeVarsOfType` and friends.
See Note [Any-free-var folder].
Metric Increase:
T5030
- - - - -
5f7b3047 by Simon Peyton Jones at 2026-10-14T00:07:27+01:00
Fix order-dependence in QuickLook
Ticket #27917 pointed out that QuickLook was order-dependent
-- very bad!
The solution is in `qlUnify`: spot when we are unifying a type with a
definite monotype, and then monomorphise any free instantiation
variables of that type.
See (UQL8) and (UQL9) in Note [QuickLook unification].
- - - - -
c391c70c by Simon Peyton Jones at 2026-10-14T00:07:28+01:00
Add some comments about RuntimeUnk
This commit just adds comments explaining why we don't need to worry about
RuntimeUnkTv in the constraint solver.
It also tightens up docmentation of earlier (overlapping) commits in
this MR.
- - - - -
7df3ba2f by Andrea Vezzosi at 2026-10-14T06:15:48-04:00
add runGhcWithSignalHandlers and remove handling from runGhc[T]
it only ever makes sense to call `withSignalHandlers` from `main`, otherwise
signals can be too easily swallowed without notification to the rest of your
process.
This commit takes withSignalHandlers out of `runGhc/runGhcT` and provides
`runGhcWithSignalHandlers` for the old behaviour.
- - - - -
b19b9a47 by Florian Ragwitz at 2026-10-14T03:37:48-07:00
hpc: Traverse patterns to tick contained HsExprs
So we can have coverage for view patterns.
Fixes #18590.
- - - - -
43 changed files:
- + changelog.d/T26543
- + changelog.d/T4162
- + changelog.d/hpc-viewpats
- compiler/GHC.hs
- compiler/GHC/Core/FVs.hs
- compiler/GHC/Core/TyCo/FVs.hs
- compiler/GHC/HsToCore/Ticks.hs
- compiler/GHC/Runtime/Heap/Inspect.hs
- compiler/GHC/Tc/Gen/App.hs
- compiler/GHC/Tc/Solver/Equality.hs
- compiler/GHC/Tc/Solver/InertSet.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/Utils/TcMType.hs
- compiler/GHC/Tc/Utils/TcType.hs
- compiler/GHC/Tc/Utils/Unify.hs
- compiler/GHC/Types/Var/FV.hs
- compiler/GHC/Utils/Panic.hs
- ghc/Main.hs
- linters/lint-codes/LintCodes/Static.hs
- testsuite/tests/default/default-fail05.stderr
- + testsuite/tests/ghc-api/T4162.hs
- + testsuite/tests/ghc-api/T4162.stdout
- testsuite/tests/ghc-api/all.T
- testsuite/tests/ghci.debugger/scripts/T14628.stderr
- testsuite/tests/hpc/all.T
- + testsuite/tests/hpc/viewpats.hs
- + testsuite/tests/hpc/viewpats.stdout
- + testsuite/tests/impredicative/T27917.hs
- testsuite/tests/impredicative/all.T
- testsuite/tests/indexed-types/should_fail/T1897b.stderr
- testsuite/tests/typecheck/should_compile/LocalGivenEqs.hs
- + testsuite/tests/typecheck/should_compile/T26543_orig.hs
- testsuite/tests/typecheck/should_compile/all.T
- testsuite/tests/typecheck/should_fail/T22707.stderr
- + testsuite/tests/typecheck/should_fail/T26543a.hs
- + testsuite/tests/typecheck/should_fail/T26543a.stderr
- testsuite/tests/typecheck/should_fail/all.T
- utils/check-exact/Parsers.hs
- utils/check-ppr/Main.hs
- utils/count-deps/Main.hs
- utils/dump-decls/Main.hs
- utils/haddock/haddock-api/src/Haddock.hs
- utils/haddock/haddock-api/test/Haddock/Backends/Hyperlinker/ParserSpec.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/1abc18550fa898d37a5b8213cb14fb…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/1abc18550fa898d37a5b8213cb14fb…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][master] add runGhcWithSignalHandlers and remove handling from runGhc[T]
by Marge Bot (@marge-bot) 14 Oct '26
by Marge Bot (@marge-bot) 14 Oct '26
14 Oct '26
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
7df3ba2f by Andrea Vezzosi at 2026-10-14T06:15:48-04:00
add runGhcWithSignalHandlers and remove handling from runGhc[T]
it only ever makes sense to call `withSignalHandlers` from `main`, otherwise
signals can be too easily swallowed without notification to the rest of your
process.
This commit takes withSignalHandlers out of `runGhc/runGhcT` and provides
`runGhcWithSignalHandlers` for the old behaviour.
- - - - -
14 changed files:
- + changelog.d/T4162
- compiler/GHC.hs
- compiler/GHC/Utils/Panic.hs
- ghc/Main.hs
- linters/lint-codes/LintCodes/Static.hs
- + testsuite/tests/ghc-api/T4162.hs
- + testsuite/tests/ghc-api/T4162.stdout
- testsuite/tests/ghc-api/all.T
- utils/check-exact/Parsers.hs
- utils/check-ppr/Main.hs
- utils/count-deps/Main.hs
- utils/dump-decls/Main.hs
- utils/haddock/haddock-api/src/Haddock.hs
- utils/haddock/haddock-api/test/Haddock/Backends/Hyperlinker/ParserSpec.hs
Changes:
=====================================
changelog.d/T4162
=====================================
@@ -0,0 +1,17 @@
+section: compiler
+issues: #4162
+mrs: !16774
+synopsis:
+ `runGhc/runGhcT` no longer hijack process signals, old behaviour provided by
+ `runGhcWithSignalHandlers`.
+description:
+ Functions `runGhc/runGhcT` no longer go out of their way to redirect process
+ signals as exceptions to them.
+
+ The installed handlers make it so other threads are unaware of any signals,
+ which can cause the process to become unresponsive whenever a crash of the GHC
+ session(s) does not cascade into a shutdown of the process itself.
+
+ The old behaviour is available through `runGhcWithSignalHandlers` or by
+ calling `withSignalHandlers`. Only intended to be used from the main thread of
+ your process, see the documentation.
=====================================
compiler/GHC.hs
=====================================
@@ -23,6 +23,7 @@ module GHC (
-- * GHC Monad
Ghc, GhcT, GhcMonad(..), HscEnv,
runGhc, runGhcT, initGhcMonad,
+ runGhcWithSignalHandlers,
printException,
handleSourceError,
@@ -555,10 +556,26 @@ runGhc :: Maybe FilePath -- ^ See argument to 'initGhcMonad'.
runGhc mb_top_dir ghc = do
ref <- newIORef (panic "empty session")
let session = Session ref
- flip unGhc session $ withSignalHandlers $ do -- catch ^C
+ flip unGhc session $ do
initGhcMonad mb_top_dir
withCleanupSession ghc
+-- | Like 'runGhc' but uses 'withSignalHandlers' to redirect selected signals to
+-- this thread to trigger 'withCleanupSession', and so that interactive
+-- evaluation receives 'UserInterrupt' exceptions.
+--
+-- __WARNING__: Only intended to be called from the __main thread__ of your
+-- process, see 'withSignalHandlers' documentation for more details.
+--
+-- More complex multi-threaded programs are recommended to use 'runGhc' or
+-- 'runGhcT', and handle signals as appropriate for their control flow.
+
+runGhcWithSignalHandlers :: Maybe FilePath -- ^ See argument to 'initGhcMonad'.
+ -> Ghc a -- ^ The action to perform.
+ -> IO a
+runGhcWithSignalHandlers mb_top_dir ghc = runGhc mb_top_dir $
+ withSignalHandlers $ ghc
+
-- | Run function for 'GhcT' monad transformer.
--
-- It initialises the GHC session and warnings via 'initGhcMonad'. Each call
@@ -572,7 +589,7 @@ runGhcT :: ExceptionMonad m =>
runGhcT mb_top_dir ghct = do
ref <- liftIO $ newIORef (panic "empty session")
let session = Session ref
- flip unGhcT session $ withSignalHandlers $ do -- catch ^C
+ flip unGhcT session $ do
initGhcMonad mb_top_dir
withCleanupSession ghct
=====================================
compiler/GHC/Utils/Panic.hs
=====================================
@@ -234,8 +234,21 @@ signalHandlersRefCount :: MVar (Word, Maybe S.Handler)
signalHandlersRefCount = unsafePerformIO $ newMVar (0,Nothing)
--- | Temporarily install standard signal handlers for catching ^C, which just
--- throw an exception in the current thread.
+-- | Temporarily install signal handlers to redirect the following signals to
+-- the calling thread as async exceptions: @INT@, @QUIT@, @HUP@, @TERM@ on unix, and
+-- @CtrlC@, @Break@ on windows. The original signal handlers are restored when all
+-- concurrent calls of this function are exited.
+--
+-- @INT@, @QUIT@, @CtrlC@, and @Break@ are thrown as @'UserInterrupt'@.
+--
+-- @HUP@, and @TERM@ are thrown as @'Panic' <signal number>@.
+--
+-- __WARNING__: Only intended to be called from the __main thread__ of your
+-- process, as otherwise it can swallow signals the process should react to in
+-- other ways. Nested calls from a single thread are fine, but concurrent calls
+-- from different threads will have the exceptions delivered to only one of
+-- them. If that thread is not @main@, extra care is needed to e.g. actually
+-- terminate the process when it receives @TERM@.
withSignalHandlers :: ExceptionMonad m => m a -> m a
#if !defined(HAVE_SIGNAL_H)
-- No signal functionality exist on the host platform (e.g. on
=====================================
ghc/Main.hs
=====================================
@@ -141,7 +141,7 @@ main = do
ShowOptions isInteractive -> showOptions isInteractive
Right postStartupMode ->
-- start our GHC session
- GHC.runGhc mbMinusB $ do
+ GHC.runGhcWithSignalHandlers mbMinusB $ do
dflags <- GHC.getSessionDynFlags
=====================================
linters/lint-codes/LintCodes/Static.hs
=====================================
@@ -31,7 +31,7 @@ import GHC.Types.Error.Codes
-- ghc (API usage)
import GHC
- ( runGhc, parseDynamicFlags
+ ( runGhcWithSignalHandlers, parseDynamicFlags
, getSessionDynFlags, setSessionDynFlags
, getSession, getLogger
, noLoc
@@ -145,7 +145,7 @@ newtype LibDir = LibDir { libDir :: FilePath }
-- | Look up the 'GhcDiagnosticCode' type family using the GHC API.
ghcDiagnosticCodeTyCon :: Maybe LibDir -> IO TyCon
ghcDiagnosticCodeTyCon mb_libDir =
- runGhc (libDir <$> mb_libDir)
+ runGhcWithSignalHandlers (libDir <$> mb_libDir)
-- STEP 1: start a GHC API session with "-package ghc"
do { dflags1 <- getSessionDynFlags
=====================================
testsuite/tests/ghc-api/T4162.hs
=====================================
@@ -0,0 +1,145 @@
+{-# LANGUAGE CPP #-}
+{-# LANGUAGE LambdaCase #-}
+
+-- | Test for #4162: 'runGhc' must not install signal handlers on behalf of its
+-- caller.
+--
+-- 'withSignalHandlers' redirects signals to the thread that called it. While
+-- 'runGhc' did that implicitly, a GHC session running in a 'forkIO' thread
+-- would capture signals aimed at the process as a whole and deliver them to a
+-- thread that may not be in a position to act on them. Here that thread sits
+-- under 'uninterruptibleMask', so the exception can never be delivered and the
+-- signal is swallowed outright.
+--
+-- The worker process parks a 'forkIO' thread inside 'runGhc' under
+-- 'uninterruptibleMask' and blocks its main thread waiting for that thread to
+-- finish. The driver process waits for the worker to report that the thread is
+-- inside 'runGhc', signals it, and checks that it dies promptly:
+--
+-- * on unix, SIGTERM has no handler, so the kernel kills the process;
+-- * on Windows, CTRL_BREAK is handled by GHC.TopHandler, which throws
+-- UserInterrupt at the main thread, which exits with 252.
+--
+-- Before #4162 both were swallowed by the masked 'forkIO' thread and the worker
+-- carried on running.
+module Main (main) where
+
+import Control.Concurrent
+import Control.Exception
+import Control.Monad
+import Control.Monad.IO.Class (liftIO)
+import GHC (runGhc)
+import System.Environment
+import System.Exit
+import System.IO
+import System.Process
+import System.Timeout (timeout)
+
+#if !defined(mingw32_HOST_OS)
+import System.Posix.Signals (sigKILL, sigTERM, signalProcess)
+#endif
+
+main :: IO ()
+main = getArgs >>= \case
+ [libdir] -> driver libdir
+ [libdir, "--worker"] -> worker libdir
+ args -> die ("FAILED: unexpected arguments: " ++ show args)
+
+-- | The process under test: a GHC session pinned in a 'forkIO' thread, with the
+-- main thread waiting on it.
+worker :: FilePath -> IO ()
+worker libdir = do
+ hSetBuffering stdout LineBuffering
+ done <- newEmptyMVar
+ _ <- forkIO $
+ flip finally (putMVar done ()) $
+ uninterruptibleMask_ $
+ runGhc (Just libdir) $ liftIO $ do
+ putStrLn ready
+ forever (threadDelay oneSecond)
+ takeMVar done
+ -- Only reachable if the session thread died on its own; the signal was
+ -- supposed to take the whole process down first.
+ putStrLn "worker: session thread finished"
+
+-- | Wait for the worker to reach 'runGhc', signal it, and require a prompt exit.
+driver :: FilePath -> IO ()
+driver libdir = do
+ self <- getExecutablePath
+ let cp = (proc self [libdir, "--worker"])
+ { std_out = CreatePipe
+#if defined(mingw32_HOST_OS)
+ -- GenerateConsoleCtrlEvent can only target a process group, so the
+ -- worker needs one of its own.
+ , create_group = True
+#endif
+ }
+ bracket (createProcess cp) kill $ \(_, Just out, _, ph) -> do
+ handshake <- timeout handshakeTimeout
+ (try (hGetLine out) :: IO (Either IOException String))
+ case handshake of
+ Just (Right l) | l == ready -> return ()
+ _ -> die ("FAILED: worker never reached runGhc: " ++ show handshake)
+ interruptWorker ph
+ waitForExit ph >>= \case
+ Just code | code == expectedExit -> putStrLn "worker terminated promptly"
+ other -> die ("FAILED: expected " ++ show expectedExit
+ ++ " from the worker, got " ++ show other)
+
+-- | Deliver the signal that the worker must not be able to swallow.
+interruptWorker :: ProcessHandle -> IO ()
+#if defined(mingw32_HOST_OS)
+-- CTRL_BREAK_EVENT rather than CTRL_C_EVENT: GenerateConsoleCtrlEvent cannot
+-- deliver CTRL+C to a specific process group. Both reach the same handler, in
+-- GHC.TopHandler now and in withSignalHandlers before #4162.
+interruptWorker = interruptProcessGroupOf
+#else
+interruptWorker ph = getPid ph >>= \case
+ Just pid -> signalProcess sigTERM pid
+ Nothing -> die "FAILED: worker exited before it could be signalled"
+#endif
+
+-- | How the worker is expected to die once signalled.
+expectedExit :: ExitCode
+#if defined(mingw32_HOST_OS)
+-- UserInterrupt reaches the top handler, which calls exitInterrupted.
+expectedExit = ExitFailure 252
+#else
+expectedExit = ExitFailure (negate (fromIntegral sigTERM))
+#endif
+
+-- | Poll for the worker's exit, giving up after 'terminationTimeout'. Polling
+-- rather than 'waitForProcess' so that a worker that ignored the signal fails
+-- the test quickly instead of hanging it until the testsuite timeout.
+waitForExit :: ProcessHandle -> IO (Maybe ExitCode)
+waitForExit ph = go (terminationTimeout `div` pollInterval)
+ where
+ go :: Int -> IO (Maybe ExitCode)
+ go n
+ | n <= 0 = return Nothing
+ | otherwise = getProcessExitCode ph >>= \case
+ Just code -> return (Just code)
+ Nothing -> threadDelay pollInterval >> go (n - 1)
+
+-- | Keep a worker that ignored the signal from outliving the test and holding
+-- the inherited stderr open. Note that 'terminateProcess' is only SIGTERM on
+-- unix, which is precisely what such a worker swallows.
+kill :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO ()
+kill (_, _, _, ph) = do
+#if defined(mingw32_HOST_OS)
+ terminateProcess ph
+#else
+ getPid ph >>= mapM_ (signalProcess sigKILL)
+#endif
+ void (waitForProcess ph)
+
+-- | Printed by the worker from inside 'runGhc', so that by the time the driver
+-- sees it any handlers 'runGhc' might install are already in place.
+ready :: String
+ready = "ready"
+
+oneSecond, pollInterval, handshakeTimeout, terminationTimeout :: Int
+oneSecond = 1000000
+pollInterval = 100000 -- 0.1s
+handshakeTimeout = 60 * oneSecond -- starting a GHC session can be slow
+terminationTimeout = 10 * oneSecond
=====================================
testsuite/tests/ghc-api/T4162.stdout
=====================================
@@ -0,0 +1 @@
+worker terminated promptly
=====================================
testsuite/tests/ghc-api/all.T
=====================================
@@ -91,3 +91,13 @@ test('T27240', [extra_run_opts(f'"{config.libdir}"')], compile_and_run, ['-packa
test('T27273', [extra_run_opts(f'"{config.libdir}"')],
compile_and_run,
['-package ghc'])
+
+# runGhc must not install signal handlers on behalf of its caller, or a session
+# running off the main thread makes the process unresponsive to signals.
+test('T4162', [ extra_run_opts(f'"{config.libdir}"')
+ # the test re-executes itself, so it has to be a real executable
+ , only_ways(['normal'])
+ , req_process
+ # no subprocesses or signals
+ , when(arch('wasm32') or arch('javascript'), skip)
+ ], compile_and_run, ['-package ghc'])
=====================================
utils/check-exact/Parsers.hs
=====================================
@@ -231,7 +231,7 @@ parseModuleEpAnnsWithCpp libdir cppOptions file = ghcWrapper libdir $ do
ghcWrapper :: LibDir -> GHC.Ghc a -> IO a
ghcWrapper libdir a =
GHC.defaultErrorHandler GHC.defaultFatalMessager GHC.defaultFlushOut
- $ GHC.runGhc (Just libdir) a
+ $ GHC.runGhcWithSignalHandlers (Just libdir) a
-- | Internal function. Exposed if you want to muck with DynFlags
-- before parsing.
=====================================
utils/check-ppr/Main.hs
=====================================
@@ -81,7 +81,7 @@ testOneFile libdir fileName = do
parseOneFile :: FilePath -> FilePath -> IO ParsedModule
parseOneFile libdir fileName = do
- runGhc (Just libdir) $ do
+ runGhcWithSignalHandlers (Just libdir) $ do
dflags <- getSessionDynFlags
let dflags2 = dflags `gopt_set` Opt_KeepRawTokenStream
_ <- setSessionDynFlags dflags2
=====================================
utils/count-deps/Main.hs
=====================================
@@ -51,7 +51,7 @@ printDeps libdir modName dot = do
calcDeps :: String -> FilePath -> IO (Map.Map ModuleName [ModuleName])
calcDeps modName libdir =
defaultErrorHandler defaultFatalMessager defaultFlushOut $ do
- runGhc (Just libdir) $ do
+ runGhcWithSignalHandlers (Just libdir) $ do
df <- getSessionDynFlags
logger <- getLogger
(df, _, _) <- parseDynamicFlags logger df [noLoc "-package=ghc"]
=====================================
utils/dump-decls/Main.hs
=====================================
@@ -32,7 +32,7 @@ main = do
mapM_ (run ghcRoot) pkg_names
run :: FilePath -> String -> IO ()
-run root pkg_nm = runGhc (Just root) $ do
+run root pkg_nm = runGhcWithSignalHandlers (Just root) $ do
let args = map noLoc
[ "-package=" ++ pkg_nm
, "-dppr-cols=1000"
=====================================
utils/haddock/haddock-api/src/Haddock.hs
=====================================
@@ -196,7 +196,7 @@ handleGhcExceptions =
--
-- > main = getArgs >>= haddock
haddock :: [String] -> IO ()
-haddock args = haddockWithGhc withGhc args
+haddock args = withSignalHandlers $ haddockWithGhc withGhc args
haddockWithGhc :: (forall a. [Flag] -> Ghc a -> IO a) -> [String] -> IO ()
haddockWithGhc ghc args = handleTopExceptions $ do
=====================================
utils/haddock/haddock-api/test/Haddock/Backends/Hyperlinker/ParserSpec.hs
=====================================
@@ -20,7 +20,7 @@ import Haddock.Backends.Hyperlinker.Types
withDynFlags :: (DynFlags -> IO ()) -> IO ()
withDynFlags cont = do
libDir <- fmap snd (getGhcDirs [])
- runGhc libDir $ do
+ runGhcWithSignalHandlers libDir $ do
dflags <- getSessionDynFlags
liftIO $ cont dflags
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/7df3ba2fc251cd8f35ee9dfd8dbddec…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/7df3ba2fc251cd8f35ee9dfd8dbddec…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][master] 3 commits: Line up simpleUnifyCheck and check_ty_eq_rhs
by Marge Bot (@marge-bot) 14 Oct '26
by Marge Bot (@marge-bot) 14 Oct '26
14 Oct '26
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
97adaf89 by Simon Peyton Jones at 2026-10-14T00:07:27+01:00
Line up simpleUnifyCheck and check_ty_eq_rhs
Even after #26543 was allegedly fixed, the original repo case in the
Description continued to fail. The reason was that the QuickLook
unifier, `qlUnify`, used `simpleUnifyCheck` for checking unification
invariants; and `simpleUnifyCheck` conservatively rejected any RHS
with a coercion hole in it. The upshot was that QuickLook was not
as clever as it should be, wrongly failing to do an impredicative
instantiation.
Interestingly `check_ty_eq_rhs`, which does the same job during
constraint solving, was more liberal: it just looked at the free vars
of the coercion, and allowed coercion holes.
This MR lines them up, adding some careful notes. See
Note [simpleUnifyCheck]
Note [Occurs check and coercions]
Note [Unification preconditions] esp (COERCIONS)
Things are better than before, but I am still uncomfortable about the
possibilty that a hole might be filled with a coercion that mentions
the LHS type variable; for now I have left this discomfort documented
in Note [Occurs check and coercions].
Main changes
* `qlUnify` now only unifies /instantiation variables/ not regular unification
variables. This makes QuickLook more predictable. See (UQL6) in
Note [QuickLook unification].
* `qlUnify` no longer needs to call `simpleUnifyCheck`, so we can get rid of
`UC_QuickLook` and its code in `simpleUnifyCheck`.
* I found I could get rid of `mkOccFolders` in favour of `anyFreeVarsOfType`.
Hooray.
* Improve documentation of CycleBreakerTv, and rename it to GivenCycleBreakerTv
because they are only uesd for Givens!
* I accepted a 9% worsening in compile time allocation on T5030
because the newly-consistent story triggered #27934.
Other changes
* I refactored `atvFolder` so that it pairs the "interesting-var" function
with the in-scope set, to avoid building a closure with a free variable.
Now we get nice tight loops for `anyFreeVarsOfType` and friends.
See Note [Any-free-var folder].
Metric Increase:
T5030
- - - - -
5f7b3047 by Simon Peyton Jones at 2026-10-14T00:07:27+01:00
Fix order-dependence in QuickLook
Ticket #27917 pointed out that QuickLook was order-dependent
-- very bad!
The solution is in `qlUnify`: spot when we are unifying a type with a
definite monotype, and then monomorphise any free instantiation
variables of that type.
See (UQL8) and (UQL9) in Note [QuickLook unification].
- - - - -
c391c70c by Simon Peyton Jones at 2026-10-14T00:07:28+01:00
Add some comments about RuntimeUnk
This commit just adds comments explaining why we don't need to worry about
RuntimeUnkTv in the constraint solver.
It also tightens up docmentation of earlier (overlapping) commits in
this MR.
- - - - -
24 changed files:
- + changelog.d/T26543
- compiler/GHC/Core/FVs.hs
- compiler/GHC/Core/TyCo/FVs.hs
- compiler/GHC/Runtime/Heap/Inspect.hs
- compiler/GHC/Tc/Gen/App.hs
- compiler/GHC/Tc/Solver/Equality.hs
- compiler/GHC/Tc/Solver/InertSet.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/Utils/TcMType.hs
- compiler/GHC/Tc/Utils/TcType.hs
- compiler/GHC/Tc/Utils/Unify.hs
- compiler/GHC/Types/Var/FV.hs
- testsuite/tests/default/default-fail05.stderr
- testsuite/tests/ghci.debugger/scripts/T14628.stderr
- + testsuite/tests/impredicative/T27917.hs
- testsuite/tests/impredicative/all.T
- testsuite/tests/indexed-types/should_fail/T1897b.stderr
- testsuite/tests/typecheck/should_compile/LocalGivenEqs.hs
- + testsuite/tests/typecheck/should_compile/T26543_orig.hs
- testsuite/tests/typecheck/should_compile/all.T
- testsuite/tests/typecheck/should_fail/T22707.stderr
- + testsuite/tests/typecheck/should_fail/T26543a.hs
- + testsuite/tests/typecheck/should_fail/T26543a.stderr
- testsuite/tests/typecheck/should_fail/all.T
Changes:
=====================================
changelog.d/T26543
=====================================
@@ -0,0 +1,7 @@
+section: compiler
+synopsis: Fix a bug in ImpredicativeTypes
+description:
+ The QuickLook algorithm (which implements `ImpredicativeTypes`) was defeated if there
+ was a kind coercion in the types being unified. That bug is now fixed.
+mrs: !16566
+issues: #26543
=====================================
compiler/GHC/Core/FVs.hs
=====================================
@@ -223,11 +223,11 @@ addCoreBndrsFV bndrs fv = foldr addCoreBndrFV fv bndrs
unitFV :: Var -> SelectiveDFV
-- Deals with an occurrence
-- Shallow: does not look at the kind
-unitFV v = MkFV (\bvs -> EndoOS (do_it bvs))
+unitFV v = MkFV (\env -> EndoOS (do_it env))
where
- do_it (is_interesting,bvs) acc
+ do_it (bvs, is_interesting) acc
+ | v `elemVarSet` bvs = acc -- First exclude the bound vars
| not (is_interesting v) = acc -- The "selective" bit
- | v `elemVarSet` bvs = acc
| v `elemDVarSet` acc = acc
| otherwise = acc `extendDVarSet` v
=====================================
compiler/GHC/Core/TyCo/FVs.hs
=====================================
@@ -278,14 +278,15 @@ deepUnitFV :: (Type -> TyCoFV) -> TyCoVar -> TyCoFV
-- Deal with a single TyCoVar
-- Takes a function to find free vars of the kind
-- See Note [Computing deep free variables]
-deepUnitFV fvs_of_kind v
- = MkFV (\bvs -> EndoOS (do_it bvs))
+{-# INLINE deepUnitFV #-} -- INLINE to specialise on fvs_of_kind
+deepUnitFV fvs_of_kind
+ = \v -> MkFV (\bvs -> EndoOS (do_it bvs v))
where
- do_it :: BoundVars -> TyCoVarSet -> TyCoVarSet
- do_it bvs acc | v `elemVarSet` bvs = acc
- | v `elemVarSet` acc = acc
- | otherwise = runFVAcc (fvs_of_kind (varType v)) acc
- `extendVarSet` v
+ do_it :: BoundVars -> TyCoVar -> TyCoVarSet -> TyCoVarSet
+ do_it bvs v acc | v `elemVarSet` bvs = acc
+ | v `elemVarSet` acc = acc
+ | otherwise = runFVAcc (fvs_of_kind (varType v)) acc
+ `extendVarSet` v
-- Left-to-right: add the kind variables to the
-- accumulator before v itself
@@ -443,11 +444,11 @@ selectiveTcvFolder
, tcf_hole = do_hole, tcf_lit = const mempty
, tcf_tycobinder = addBndrSelectiveFV }
where
- do_tcv v = MkFV (\bvs -> EndoOS (do_it bvs))
+ do_tcv v = MkFV (\env -> EndoOS (do_it env))
where
- do_it (is_interesting,bvs) acc
+ do_it (bvs,is_interesting) acc
+ | v `elemVarSet` bvs = acc -- First exclude bound variables
| not (is_interesting v) = acc -- The "selective" bit
- | v `elemVarSet` bvs = acc
| v `elemDVarSet` acc = acc
| otherwise = acc `extendDVarSet` v
@@ -848,43 +849,77 @@ invisibleVarsOfTypes = foldr (unionVarSet . invisibleVarsOfType) emptyVarSet
* *
********************************************************************* -}
-{-# INLINE afvFolder #-} -- so that specialization to (const True) works
-afvFolder :: (TyCoVar -> Bool) -> TyCoFolder (FV TyCoVarSet DM.Any)
+{- Note [Any-free-var folder]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The "any-free-var" folders take a `check_fv` predicate (TyCoVar -> Bool) and
+check if any /deep/ free variable of the type/coercion satisfies it. So
+
+ anyFreeVarsOfType p ty = anyVarSet p (tyCoVarsOfType ty)
+
+Our main folders look like:
+
+ afv_type :: Type -> FV (BoundVars, TyVar -> Bool) Any
+
+where we cleverly pair the predicate with the forall-bound variables of the type
+in the "env" field of (FV env Any). This way we can build the /folder/ once and
+for all (independent of the `check_fv` predicate), and the loop passes down
+the pair.
+
+The same idiom is used in `exprFreeVarsDSet` and friends.
+
+However we are careful to INLINE `atvFolder` and `runAny` so that we get those
+nice tight loops.
+-}
+
+{-# INLINE afvFolder #-} -- See Note [Any-free-var folder]
+afvFolder :: TyCoFolder (FV (BoundVars, TyCoVar -> Bool) DM.Any)
-- 'afvFolder' is short for "any-free-var folder", good for checking
--- if any free var of a type satisfies a predicate `check_fv`
-afvFolder check_fv = TyCoFolder { tcf_view = noView -- See Note [Free vars and synonyms]
- , tcf_tyvar = do_tcv, tcf_covar = do_tcv
- , tcf_hole = do_hole, tcf_lit = const mempty
- , tcf_tycobinder = addBndrFV }
+-- if any /deep/ free var of a type satisfies a predicate `check_fv`
+-- The "env" of the (FV env Any) is a pair (bvs,pred) of
+-- the bound variables (which are definitely not free), and
+-- an "interesting var" predicate which the client supplies
+afvFolder = TyCoFolder { tcf_view = noView -- See Note [Free vars and synonyms]
+ , tcf_tyvar = do_tcv
+ , tcf_covar = do_tcv
+ , tcf_hole = do_hole
+ , tcf_lit = const mempty
+ , tcf_tycobinder = addBndrSelectiveFV }
where
- do_tcv tv = MkFV $ \ bvs ->
- Any (not (tv `elemVarSet` bvs) && check_fv tv)
- do_hole _ = mempty -- I'm unsure; probably never happens
+ -- Check the variable itself with check_fv,
+ -- and check its kind (this is a deep folder)
+ do_tcv tcv = MkFV $ \ (bvs,check_fv) ->
+ Any (not (tcv `elemVarSet` bvs) &&
+ (check_fv tcv || runAny check_fv (afv_type (varType tcv))))
+ do_hole hole = do_tcv (coHoleCoVar hole)
+
+afv_type :: Type -> FV (BoundVars, TyVar -> Bool) DM.Any
+afv_types :: [Type] -> FV (BoundVars, TyVar -> Bool) DM.Any
+afv_co :: Coercion -> FV (BoundVars, TyVar -> Bool) DM.Any
+(afv_type, afv_types, afv_co, _) = foldTyCo afvFolder
+
+runAny :: (TyVar -> Bool) -> FV (BoundVars, TyCoVar -> Bool) DM.Any -> Bool
+{-# INLINE runAny #-}
+runAny check_fv fvs = DM.getAny (runFV fvs (emptyVarSet, check_fv))
anyFreeVarsOfType :: (TyCoVar -> Bool) -> Type -> Bool
-anyFreeVarsOfType check_fv ty = DM.getAny (runFVTop (f ty))
- where (f, _, _, _) = foldTyCo (afvFolder check_fv)
+{-# INLINE anyFreeVarsOfType #-}
+anyFreeVarsOfType check_fv ty = runAny check_fv (afv_type ty)
anyFreeVarsOfTypes :: (TyCoVar -> Bool) -> [Type] -> Bool
-anyFreeVarsOfTypes check_fv tys = DM.getAny (runFVTop (f tys))
- where (_, f, _, _) = foldTyCo (afvFolder check_fv)
+{-# INLINE anyFreeVarsOfTypes #-}
+anyFreeVarsOfTypes check_fv tys = runAny check_fv (afv_types tys)
anyFreeVarsOfCo :: (TyCoVar -> Bool) -> Coercion -> Bool
-anyFreeVarsOfCo check_fv co = DM.getAny (runFVTop (f co))
- where (_, _, f, _) = foldTyCo (afvFolder check_fv)
+anyFreeVarsOfCo check_fv co = runAny check_fv (afv_co co)
noFreeVarsOfType :: Type -> Bool
-noFreeVarsOfType ty = not $ DM.getAny (runFVTop (f ty))
- where (f, _, _, _) = foldTyCo (afvFolder (const True))
+noFreeVarsOfType ty = not $ runAny (const True) (afv_type ty)
noFreeVarsOfTypes :: [Type] -> Bool
-noFreeVarsOfTypes tys = not $ DM.getAny (runFVTop (f tys))
- where (_, f, _, _) = foldTyCo (afvFolder (const True))
+noFreeVarsOfTypes tys = not $ runAny (const True) (afv_types tys)
noFreeVarsOfCo :: Coercion -> Bool
-noFreeVarsOfCo co = not $ DM.getAny (runFVTop (f co))
- where (_, _, f, _) = foldTyCo (afvFolder (const True))
-
+noFreeVarsOfCo co = not $ runAny (const True) (afv_co co)
{-
************************************************************************
=====================================
compiler/GHC/Runtime/Heap/Inspect.hs
=====================================
@@ -628,18 +628,16 @@ be sigmas and not taus.
{- Note [RuntimeUnkTv]
~~~~~~~~~~~~~~~~~~~~~~
-In the GHCi debugger we use unification variables whose MetaInfo is
-RuntimeUnkTv. The special property of a RuntimeUnkTv is that it can
-unify with a polytype (see GHC.Tc.Solver.Equality.checkTypeEqNoUnification).
-If we don't do this `:print <term>` will fail if the type of <term>
-has nested `forall`s or `=>`s.
-
-This is because the GHCi debugger's internals will attempt to unify a
-metavariable with the type of <term> and then display the result, but
-if the type has nested `forall`s or `=>`s, then unification will fail
-unless we do something special. As a result, `:print` will bail out
-and the unhelpful result will be `<term> = (_t1::t1)` (where `t1` is a
-metavariable).
+In the GHCi debugger we use unification variables whose MetaInfo is RuntimeUnkTv.
+The special property of a RuntimeUnkTv is that it can unify with a polytype (see
+GHC.Tc.Utils.Unify.simpleUnifyCheck). If we don't do this `:print <term>` will fail if
+the type of <term> has nested `forall`s or `=>`s.
+
+This is because the GHCi debugger's internals will attempt to unify a metavariable
+with the type of <term> and then display the result, but if the type has nested
+`forall`s or `=>`s, then unification will fail unless we do something special. As
+a result, `:print` will bail out and the unhelpful result will be `<term> =
+(_t1::t1)` (where `t1` is a metavariable).
Beware: <term> can have nested `forall`s even if its definition doesn't use
RankNTypes! Here is an example from #14828:
@@ -654,13 +652,18 @@ We could envision deeply instantiating this type to get the type
`forall f a b. Functor f => (a -> b) -> f a -> f b`,
but this trick wouldn't work for higher-rank types.
-Instead, we adopt a simpler fix: allow RuntimeUnkTv to unify with a
-polytype (specifically, see ghci_tv in GHC.Tc.Utils.Unify.preCheck).
-This allows metavariables to unify with types that have
-nested (or higher-rank) `forall`s/`=>`s, which makes `:print fmap`
-display as
-`fmap = (_t1::forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b)`,
+Instead, we adopt a simpler fix: allow RuntimeUnkTv to unify with a polytype
+(specifically, see ghci_tv in GHC.Tc.Utils.Unify.preCheck). This allows
+metavariables to unify with types that have nested (or higher-rank)
+`forall`s/`=>`s, which makes `:print fmap` display as
+ `fmap = (_t1::forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b)`,
as expected.
+
+(RUT1) The debugger uses the "on-the-fly" unifier, namely
+ GHC.Tc.Utils.Unify.unifyType
+ and /ignores/ any constraints it generates: see `addConstraint`. It never
+ calls the constraint solver, so the constraint solver does not need to
+ understand about RuntimeUnkTvs.
-}
=====================================
compiler/GHC/Tc/Gen/App.hs
=====================================
@@ -37,6 +37,7 @@ import GHC.Core.ConLike ( ConLike(..) )
import GHC.Core.DataCon ( dataConConcreteTyVars, isNewDataCon, dataConOrigArgTys )
import GHC.Core.TyCon
import GHC.Core.TyCo.Rep
+import GHC.Core.TyCo.FVs
import GHC.Core.TyCo.Ppr
import GHC.Core.TyCo.Subst ( substTyWithInScope )
import GHC.Core.Type
@@ -631,11 +632,13 @@ tcInstFun :: QLFlag
-- plus the modification in Fig 5, of the QL paper:
-- "A quick look at impredicativity" (ICFP'20).
tcInstFun do_ql inst_final rn_head@(_, fun_lspan) tc_fun fun_sigma rn_args
- = do { traceTc "tcInstFun" (vcat [ text "tc_fun" <+> ppr tc_fun
+ = do { lvl <- getTcLevel
+ ; traceTc "tcInstFun" (vcat [ text "tc_fun" <+> ppr tc_fun
, text "rn_fun" <+> ppr rn_head
, text "fun_sigma" <+> ppr fun_sigma
, text "args:" <+> ppr rn_args
- , text "do_ql" <+> ppr do_ql])
+ , text "do_ql" <+> ppr do_ql
+ , text "lvl:" <+> ppr lvl ])
; fun_origin <- mk_origin rn_head
; res@(_, fun_ty) <- go fun_origin 1 [] fun_sigma rn_args
; traceTc "tcInstFun:ret" (ppr fun_ty)
@@ -1377,13 +1380,15 @@ tc_inst_forall_arg conc_tvs (tvb, inner_ty) hs_ty
-- is not fully zonked, because ty_arg is fully zonked.
-- See Note [Type application substitution].
+ ; lvl <- getTcLevel
; traceTc "tc_inst_forall_arg (VTA/VDQ)" (
vcat [ text "fun_ty" <+> ppr fun_ty
, text "tv" <+> ppr tv <+> dcolon <+> debugPprType kind
, text "ty_arg" <+> debugPprType ty_arg <+> dcolon
<+> debugPprType (typeKind ty_arg)
, text "inner_ty" <+> debugPprType inner_ty
- , text "insted_ty" <+> debugPprType insted_ty ])
+ , text "insted_ty" <+> debugPprType insted_ty
+ , text "lvl:" <+> ppr lvl ])
; return (ty_arg, insted_ty) }
{- Note [Visible type application and abstraction]
@@ -1934,11 +1939,13 @@ quickLookArg1 pos app_lspan rn_head larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_
-- capture and save it in the `EValArgQL`. See (QLA6) in
-- Note [Quick Look at value arguments]
+ ; lvl <- getTcLevel
; traceTc "quickLookArg {" $
vcat [ text "arg:" <+> ppr arg
, text "orig_arg_rho:" <+> ppr orig_arg_rho
, text "head:" <+> ppr rn_fun_arg <+> dcolon <+> ppr mb_fun_ty
- , text "args:" <+> ppr rn_args ]
+ , text "args:" <+> ppr rn_args
+ , text "level:" <+> ppr lvl ]
; case mb_fun_ty of {
Nothing -> skipQuickLook app_lspan larg sc_arg_ty ; -- fun is too complicated
@@ -2048,6 +2055,8 @@ qlMonoHsWrapper (WpTyApp ty) = qlMonoTcType ty
qlMonoHsWrapper _ = return ()
qlMonoTcType :: TcType -> ZonkM ()
+-- Monomorphise any free instantiation variables of `ty`
+-- This functions zonks as it goes
-- See Note [Monomorphise instantiation variables]
qlMonoTcType ty
= do { traceZonk "monomorphiseQLInstVars {" (ppr ty)
@@ -2152,8 +2161,7 @@ foldQLInstVars check_tv ty
qlUnify :: TcType -> TcType -> TcM ()
-- Unify ty1 with ty2:
--- * It can unify both instantiation variables (possibly with polytypes),
--- and ordinary unification variables (but only with monotypes)
+-- * It unifies /only/ instantiation variables (see (UQL6)), hopefully with polytypes
-- * It does not return a coercion (unlike unifyType); it is called
-- for the sole purpose of unifying instantiation variables, although it
-- may also (opportunistically) unify regular unification variables.
@@ -2161,18 +2169,23 @@ qlUnify :: TcType -> TcType -> TcM ()
-- * It may return without having made the argument types equal, of course;
-- it just makes best efforts.
qlUnify ty1 ty2
- = do { traceTc "qlUnify" (ppr ty1 $$ ppr ty2)
+ = do { lvl <- getTcLevel
+ ; traceTc "qlUnify" (ppr lvl $$ ppr ty1 $$ ppr ty2)
; go ty1 ty2 }
where
go :: TcType -> TcType -> TcM ()
+ go t1 t2 = do { traceTc "qlUinfy:go" (ppr t1 <+> char '~' <+> ppr t2)
+ ; go' t1 t2 }
+
-- Decompose (arg1 -> res1) ~ (arg2 -> res2)
-- and (c1 => res1) ~ (c2 => res2)
-- But for the latter we only learn instantiation info from res1~res2
- go (FunTy { ft_af = af1, ft_arg = arg1, ft_res = res1 })
+ go' (FunTy { ft_af = af1, ft_arg = arg1, ft_res = res1 })
(FunTy { ft_af = af2, ft_arg = arg2, ft_res = res2 })
| af1 == af2 -- Match the arrow TyCon
- = do { when (isVisibleFunArg af1) (go arg1 arg2)
+ = do { traceTc "go_fun" (ppr arg1 $$ ppr arg2)
+ ; when (isVisibleFunArg af1) (go arg1 arg2)
-- NB: we do not unify the multiplicities; that would be too strong.
-- We might only require mult1 ⩽ mult2, as in Note [Multiplicity in deep subsumption].
@@ -2181,30 +2194,29 @@ qlUnify ty1 ty2
; go res1 res2 }
-- Make sure to not unify "kappa := (a %1 -> b)". See (UQL5).
- go (FunTy { ft_mult = OneTy }) _ = return ()
- go _ (FunTy { ft_mult = OneTy }) = return ()
- -- NB: we do want to be able to unify "kappa := a => b", as that's
- -- the main point of QuickLook (allowing meta-variables to be unified
- -- with qualified types).
+ -- NB: we /do/ want to be able to unify "kappa := a => b", as that's
+ -- the main point of QuickLook (allowing meta-variables to be unified
+ -- with qualified types).
+ go' (FunTy { ft_mult = OneTy }) _ = return ()
+ go' _ (FunTy { ft_mult = OneTy }) = return ()
- go (TyVarTy tv) ty2
- | isMetaTyVar tv = go_kappa tv ty2
- go ty1 (TyVarTy tv)
- | isMetaTyVar tv = go_kappa tv ty1
+ -- Type variables; see (UQL6) in Note [QuickLook unification]
+ go' (TyVarTy tv) ty2 | isMetaTyVar tv = go_meta tv ty2
+ go' ty1 (TyVarTy tv) | isMetaTyVar tv = go_meta tv ty1
- go (CastTy ty1 _) ty2 = go ty1 ty2
- go ty1 (CastTy ty2 _) = go ty1 ty2
+ go' (CastTy ty1 _) ty2 = go ty1 ty2
+ go' ty1 (CastTy ty2 _) = go ty1 ty2
- go (TyConApp tc1 []) (TyConApp tc2 [])
+ go' (TyConApp tc1 []) (TyConApp tc2 [])
| tc1 == tc2 -- See GHC.Tc.Utils.Unify
= return () -- Note [Expanding synonyms during unification]
-- Now, and only now, expand synonyms
- go rho1 rho2
+ go' rho1 rho2
| Just rho1 <- coreView rho1 = go rho1 rho2
| Just rho2 <- coreView rho2 = go rho1 rho2
- go (TyConApp tc1 tys1) (TyConApp tc2 tys2)
+ go' (TyConApp tc1 tys1) (TyConApp tc2 tys2)
| tc1 == tc2
, not (isTypeFamilyTyCon tc1)
, tys1 `equalLength` tys2
@@ -2212,67 +2224,79 @@ qlUnify ty1 ty2
-- Don't allow unifying (a => b) with the AppTy 'arr[tau] a b'.
-- To ensure this, use 'tcSplitAppTyNoView_maybe' which does not split (=>).
- go (AppTy t1a t1b) ty2
+ go' (AppTy t1a t1b) ty2
| Just (t2a, t2b) <- tcSplitAppTyNoView_maybe ty2
= do { go t1a t2a; go t1b t2b }
- go ty1 (AppTy t2a t2b)
+ go' ty1 (AppTy t2a t2b)
| Just (t1a, t1b) <- tcSplitAppTyNoView_maybe ty1
= do { go t1a t2a; go t1b t2b }
- go _ _ = return ()
+ -- Type families have a monomorphic result, so if we see (say)
+ -- Maybe kappa ~ F ty
+ -- we know that kappa must be a monotype
+ go' (TyConApp tc1 _) ty2 | isTypeFamilyTyCon tc1 = monomorphise ty2
+ go' ty1 (TyConApp tc2 _) | isTypeFamilyTyCon tc2 = monomorphise ty1
+
+ go' _ _ = return ()
-- Don't look under foralls; see (UQL4) of Note [QuickLook unification]
----------------
- go_kappa kappa ty2
- = assertPpr (isMetaTyVar kappa) (ppr kappa) $
- do { info <- readMetaTyVar kappa
- ; case info of
+ go_meta tv1 ty2
+ = assertPpr (isMetaTyVar tv1) (ppr tv1) $
+ do { info <- readMetaTyVar tv1
+ ; case info of -- See (UQL9)
Indirect ty1 -> go ty1 ty2
Flexi -> do { ty2 <- liftZonkM $ zonkTcType ty2
- ; go_flexi kappa ty2 } }
+ ; go_flexi tv1 ty2 } }
----------------
- -- Swap (kappa1[conc] ~ kappa2[tau])
- -- otherwise we'll fail to unify and emit a coercion.
- -- Just an optimisation: emitting a coercion is fine
- go_flexi kappa (TyVarTy tv2)
- | lhsPriority tv2 > lhsPriority kappa
- = go_flexi1 tv2 (TyVarTy kappa)
- go_flexi kappa ty2
- = go_flexi1 kappa ty2
-
- go_flexi1 kappa ty2 -- ty2 is zonked
- = do { cur_lvl <- getTcLevel
- -- See Note [Unification preconditions], (UNTOUCHABLE) wrinkles
- -- Here we are in the TcM monad, which does not track enclosing
- -- Given equalities; so for quick-look unification we conservatively
- -- treat /any/ level outside this one as untouchable. Hence cur_lvl.
- ; case simpleUnifyCheck UC_QuickLook cur_lvl kappa ty2 of
- SUC_CanUnify ->
- do { co <- unifyKind (Just (TypeThing ty2)) ty2_kind kappa_kind
- -- unifyKind: see (UQL2) in Note [QuickLook unification]
- -- and (MIV2) in Note [Monomorphise instantiation variables]
- ; let ty2' = mkCastTy ty2 co
- ; traceTc "qlUnify:update" $
- ppr kappa <+> text ":=" <+> ppr ty2
- ; liftZonkM $ writeMetaTyVar kappa ty2' }
- _ -> return () -- e.g. occurs-check or forall-bound variable
- }
+ -- Both tv1 and ty2 are zonked; tv1 is a unfilled meta-tyvar
+ -- Zonking ty2 makes the occurs-check easier
+ go_flexi tv1 ty2
+ | TyVarTy tv2 <- ty2
+ , lhsPriority tv2 > lhsPriority tv1
+ = go_flexi1 tv2 (TyVarTy tv1)
+ | otherwise
+ = go_flexi1 tv1 ty2
+
+ -- Both tv1 and ty2 are zonked; tv1 is a unfilled meta-tyvar
+ go_flexi1 tv1 ty2
+ | isQLInstTyVar tv1 = go_kappa tv1 ty2
+ | otherwise = monomorphise ty2
+
+ go_kappa kappa ty2
+ | isConcreteTyVar kappa -- See (UQL7) in Note [QuickLook unification]
+ = return ()
+ | anyFreeVarsOfType (== kappa) ty2
+ = return () -- Occurs check. See (UQL10) in Note [QuickLook unification]
+ | otherwise
+ = do { co <- unifyKind (Just (TypeThing ty2)) ty2_kind kappa_kind
+ -- unifyKind: see (UQL2) in Note [QuickLook unification]
+ -- and (MIV2) in Note [Monomorphise instantiation variables]
+ ; let ty2' = mkCastTy ty2 co
+ ; traceTc "qlUnify:update" $
+ ppr kappa <+> text ":=" <+> ppr ty2
+ ; liftZonkM $ writeMetaTyVar kappa ty2'
+ ; return () }
where
kappa_kind = tyVarKind kappa
ty2_kind = typeKind ty2
+ monomorphise :: TcType -> TcM ()
+ monomorphise ty -- See (UQL8)
+ = do { _ <- liftZonkM $ qlMonoTcType ty; return () }
+
{- Note [QuickLook unification]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In qlUnify, if we find (kappa ~ ty), we are going to update kappa := ty.
That is the entire point of qlUnify! Wrinkles:
-(UQL1) Before unifying an instantiation variable in `go_flexi`, we must check
- the usual unification conditions, by calling `GHC.Tc.Utils.Unify.simpleUnifyCheck`.
- For example that checks for
+(UQL1) Before unifying an instantiation variable in `go_kappa`, we must check
+ the usual unification conditions. In particular:
* An occurs-check
- * Level mis-match
* An attempt to unify a concrete type variable with a non-concrete type.
+ We don't need a level-check because the level of an instantiation variable
+ is infinity -- see (UQL3).
(UQL2) What if kappa and ty have different kinds? We simply call the
ordinary unifier and use the coercion to connect the two.
@@ -2345,6 +2369,47 @@ That is the entire point of qlUnify! Wrinkles:
constraint arrows =>, so that (UQL4) applies to them as well.
See Note [Multiplicity in deep subsumption].
+(UQL6) What if `qlUnify` sees a regular (monotyped) unification variable, `alpha`, rather
+ than an instantiation variable `kappa`? Thus (alpha ~ ty). It's tempting to just
+ unify it, but the regular, on-the-fly unifier has lots of careful checks (levels,
+ concreteness etc). In regular unification it doesn't matter whether we unify on-the-fly
+ or later -- it's just an efficiency issue -- but we must maintain all the careful invariants.
+
+ The simplest thing is to say that `qlUnify` unifies /only/ instantiation variables
+ (see calls to `isQLInstTyVar`). We could be more ambitious, perhaps, in future, but
+ beware of accidentally causing obscure /user-visible/ consequences (#26543),
+ e.g. via Note [Occurs check and coercions].
+
+ Remember, though, that the /only/ reason for QuickLoook is to unify instantiation variables
+ with polytypes; anything else is optional, and will be picked up with later "ordinary"
+ typechecking. So unifing only instantiation variables is enough.
+
+(UQL7) If `qlUnify` sees a concrete `kaapa`, thus
+ kappa[conc] ~ some-type
+ we just give up. It's tiresome to do all the checks for concreteness; and concrete
+ type variables are never unified with polytypes, so it's fine to leave it for the
+ regular unifier.
+
+(UQL8) If we see (Maybe kappa ~ alpha), where `kappa` is an instantiation variable
+ and `alpha` is a regular unification variable, we know that `kappa` must be a
+ monotype, so we can use `qlMonoTcType` to monomorphise it. This is important,
+ because rule APP-LIGHTNING in the QuickLook paper only fires when there are no free
+ instantiation variables. Unifying `kappa` with a monotype makes it more possible
+ for APP-LIGHTNING to fire.
+
+ Same if we see (Maybe kappa ~ F tya) where F is a type function, becuase the result
+ of a type function is always a monotype.
+
+(UQL9) If we have (Maybe kappa ~ alpha) and we have /already/ unified alpha:=Maybe beta,
+ then it's more efficient to look through that unification and decompose, so that
+ we get (kappa ~ alpha). That way we don't have to monomorphise `kappa`. So we
+ "look through" all meta-tyvars, not just the instantiation vars.
+
+(UQL10) For the occurs check in `qlUnify` we simply use `anyFreeVarsOfType`. For
+ coercions that looks at the free vars of the coercion; but for coercion /holes/
+ all it can do is look at the free vars of the hole's type.
+ See Note [Occurs check and coercions] in GHC.Tc.Utils.Unify
+
Sadly discarded design alternative
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It is very tempting to use `unifyType` rather than `qlUnify`, killing off the
=====================================
compiler/GHC/Tc/Solver/Equality.hs
=====================================
@@ -2390,7 +2390,7 @@ checkTypeEqNoUnification ev eq_rel lhs rhs =
given_pred = mkNomEqPred fam_app new_ty
given_term = evCoercion $ mkNomReflCo new_ty -- See Detail (4) of Note
- -- See Detail (7) of the Note
+ -- See Detail (7) of Note [Type equality cycles: implementation]
cb_loc = updateCtLocOrigin (ctEvLoc ev) CycleBreakerOrigin
----------------------
@@ -2648,15 +2648,15 @@ turn controls the behaviour of the workhorse: GHC.Tc.Utils.Unify.checkTyEqRhs.
The details depend on whether we're working with a Given or a Wanted.
-Given
------
+Note [Type equality cycles: Givens]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We emit a new Given, [G] F a ~ cbv, equating the type family application
to our new cbv. This is actually done by `BreakGiven` in the `given_flags`
in `checkTypeEqNoUnification`.
-Note its orientation: The type family ends up on the left; see
-Note [Orienting TyFamLHS/TyFamLHS]. No special treatment for
-CycleBreakerTvs is necessary. This scenario is now easily soluble, by using
+Note its orientation: The type family ends up on the left;
+see Note [Orienting TyFamLHS/TyFamLHS]. No special treatment for
+GivenCycleBreakerTvs is necessary. This scenario is now easily soluble, by using
the first Given to rewrite the Wanted, which can now be solved.
(The first Given actually also rewrites the second one, giving
@@ -2677,10 +2677,11 @@ to
Note that
* `cbv` is a fresh cycle breaker variable.
-* `cbv` is a meta-tyvar, but it is completely untouchable.
+* `cbv` is a meta-tyvar, of flavour GivenCycleBreakerTv, but it is
+ completely untouchable.
* We track the cycle-breaker variables in inert_cycle_breakers in InertSet
-* We eventually fill in the cycle-breakers, with `cbv := F lhs`.
- No one else fills in CycleBreakerTvs!
+* We eventually fill in the cycle-breakers, with `cbv := F lhs`,
+ via `restoreTyVarCycles`. No one else fills in GivenCycleBreakerTvs!
* The evidence for the new `F lhs ~ cbv` constraint is Refl, because we know
this fill-in is ultimately going to happen.
* In `inert_cycle_breakers`, we remember the (cbv, F lhs) pair; that is, we
@@ -2690,8 +2691,8 @@ Note that
* This fill-in is done when solving is complete, by restoreTyVarCycles
in nestImplicTcS and runTcSWithEvBinds.
-Wanted
-------
+Note [Type equality cycles: Wanteds]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
First, we do not cycle-break unless the LHS is a unifiable type variable
See Note [Don't cycle-break Wanteds when not unifying].
@@ -2750,9 +2751,9 @@ to unify the cbvs:
[W] AllEqF '[Bool] alpha
Without the logic detailed in this Note, we're stuck here, as AllEqF cannot
-reduce and alpha cannot unify. Let's instead apply our cycle-breaker approach,
-just as described above. We thus invent cbv1 and cbv2 and unify
-alpha := cbv1 -> cbv2, yielding (after zonking)
+reduce and alpha cannot unify (occurs check). Let's instead apply our
+cycle-breaker approach, just as described above. We thus invent cbv1 and cbv2
+and unify alpha := (cbv1 : cbv2), yielding (after zonking)
[W] Head (cbv1 : cbv2) ~ cbv1
[W] Tail (cbv1 : cbv2) ~ cbv2
@@ -2790,8 +2791,8 @@ Note that we need to unify the cbvs here; if we did not, there would be
no way to solve those constraints. That's why the cycle-breakers are
ordinary TauTvs.
-How all this is implemented
----------------------------
+Note [Type equality cycles: implementation]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We implement all this via the `TEFA_Break` constructor of `TyEqFamApp`,
itself stored in the `tef_fam_app` field of `TyEqFlags`, which controls
the behaviour of `GHC.Tc.Utils.Unify.checkTyEqRhs`. The `TEFA_Break`
@@ -2918,7 +2919,7 @@ More details:
CycleBreakerOrigin. This works for both Givens and Wanteds, as we need the
logic in the W case for e.g. typecheck/should_fail/T17139. Because this
logic needs to work for Wanteds, too, we cannot simply look for a
- CycleBreakerTv on the left: Wanteds don't use them.
+ GivenCycleBreakerTv on the left: Wanteds don't use them.
**********************************************************************
=====================================
compiler/GHC/Tc/Solver/InertSet.hs
=====================================
@@ -304,7 +304,7 @@ instance Outputable WorkList where
********************************************************************* -}
type CycleBreakerVarStack = NE.NonEmpty (Bag (TcTyVar, TcType))
- -- ^ a stack of (CycleBreakerTv, original family applications) lists
+ -- ^ A stack of (GivenCycleBreakerTv, original family applications) lists
-- first element in the stack corresponds to current implication;
-- later elements correspond to outer implications
-- used to undo the cycle-breaking needed to handle
@@ -324,6 +324,8 @@ data InertSet
-- See `resetInertCans`
, inert_cycle_breakers :: CycleBreakerVarStack
+ -- The equalities for /given/ cycle breakers, GivenCycleBreakerTvs
+ -- See Note [
, inert_famapp_cache :: FunEqMap Reduction
-- Just a hash-cons cache for use when reducing family applications
@@ -1955,7 +1957,8 @@ prohibitedSuperClassSolve given_loc wanted_loc
{- *********************************************************************
* *
- Cycle breakers
+ Given Cycle breakers
+ See Note [Type equality cycles: Givens] in GHC.Tc.Solver.Equality
* *
********************************************************************* -}
=====================================
compiler/GHC/Tc/Solver/Monad.hs
=====================================
@@ -2325,7 +2325,7 @@ wrapUnifier rws loc role do_unifications
-}
-- | Fill in CycleBreakerTvs with the variables they stand for.
--- See Note [Type equality cycles] in GHC.Tc.Solver.Equality
+-- See Note [Type equality cycles: Givens] in GHC.Tc.Solver.Equality
restoreTyVarCycles :: InertSet -> TcM ()
restoreTyVarCycles is
= TcM.liftZonkM
=====================================
compiler/GHC/Tc/Utils/TcMType.hs
=====================================
@@ -726,11 +726,11 @@ cloneMetaTyVarName name
metaInfoToTyVarName :: MetaInfo -> FastString
metaInfoToTyVarName meta_info =
case meta_info of
- TauTv -> fsLit "t"
- TyVarTv -> fsLit "a"
- RuntimeUnkTv -> fsLit "r"
- CycleBreakerTv -> fsLit "b"
- ConcreteTv {} -> fsLit "c"
+ TauTv -> fsLit "t"
+ TyVarTv -> fsLit "a"
+ RuntimeUnkTv -> fsLit "r"
+ GivenCycleBreakerTv -> fsLit "b"
+ ConcreteTv {} -> fsLit "c"
newAnonMetaTyVar :: MetaInfo -> Kind -> TcM TcTyVar
newAnonMetaTyVar mi = newNamedAnonMetaTyVar (metaInfoToTyVarName mi) mi
@@ -824,7 +824,7 @@ cloneAnonMetaTyVar info tv kind
-- in GHC.Tc.Solver.Equality.
newCycleBreakerTyVar :: TcKind -> TcM TcTyVar
newCycleBreakerTyVar kind
- = do { details <- newMetaDetails CycleBreakerTv
+ = do { details <- newMetaDetails GivenCycleBreakerTv
; name <- newMetaTyVarName (fsLit "cbv")
; return (mkTcTyVar name kind details) }
=====================================
compiler/GHC/Tc/Utils/TcType.hs
=====================================
@@ -688,9 +688,10 @@ data MetaInfo
| RuntimeUnkTv -- ^ A unification variable used in the GHCi debugger.
-- It /is/ allowed to unify with a polytype, unlike TauTv
- | CycleBreakerTv -- ^ Used to fix occurs-check problems in Givens
- -- See Note [Type equality cycles] in
- -- GHC.Tc.Solver.Equality
+ | GivenCycleBreakerTv -- ^ Used to fix occurs-check problems in Givens
+ -- Never used for Wanteds
+ -- See Note [Type equality cycles] in
+ -- GHC.Tc.Solver.Equality
| ConcreteTv ConcreteTvOrigin
-- ^ A unification variable that can only be unified
@@ -705,11 +706,11 @@ instance Outputable MetaDetails where
ppr (Indirect ty) = text "Indirect" <+> ppr ty
instance Outputable MetaInfo where
- ppr TauTv = text "tau"
- ppr TyVarTv = text "tyv"
- ppr RuntimeUnkTv = text "rutv"
- ppr CycleBreakerTv = text "cbv"
- ppr (ConcreteTv {}) = text "conc"
+ ppr TauTv = text "tau"
+ ppr TyVarTv = text "tyv"
+ ppr RuntimeUnkTv = text "rutv"
+ ppr GivenCycleBreakerTv = text "cbv"
+ ppr (ConcreteTv {}) = text "conc"
-- | What caused us to create a 'ConcreteTv' metavariable?
-- See Note [ConcreteTv] in GHC.Tc.Utils.Concrete.
@@ -1299,7 +1300,7 @@ isRuntimeUnkTyVar tv
isCycleBreakerTyVar tv
| isTyVar tv -- See Note [Coercion variables in free variable lists]
- , MetaTv { mtv_info = CycleBreakerTv } <- tcTyVarDetails tv
+ , MetaTv { mtv_info = GivenCycleBreakerTv } <- tcTyVarDetails tv
= True
| otherwise
@@ -1350,8 +1351,8 @@ metaTyVarInfo tv
isTouchableInfo :: MetaInfo -> Bool
isTouchableInfo info
- | CycleBreakerTv <- info = False
- | otherwise = True
+ | GivenCycleBreakerTv <- info = False
+ | otherwise = True
metaTyVarTcLevel :: TcTyVar -> TcLevel
metaTyVarTcLevel tv
=====================================
compiler/GHC/Tc/Utils/Unify.hs
=====================================
@@ -103,7 +103,6 @@ import GHC.Types.Id( idType )
import GHC.Types.Var as Var
import GHC.Types.Var.Set
import GHC.Types.Var.Env
-import GHC.Types.Var.FV
import GHC.Types.Basic
import GHC.Types.Unique.Set (nonDetEltsUniqSet)
@@ -120,7 +119,6 @@ import GHC.Data.Maybe (firstJusts)
import Control.Monad
import Data.Functor.Identity (Identity(..))
import qualified Data.List.NonEmpty as NE
-import Data.Monoid as DM ( Any(..) )
import qualified Data.Semigroup as S ( (<>) )
import Data.Traversable (for)
@@ -3143,6 +3141,7 @@ uUnfilledVar2 env@(UE { u_defer = def_eq_ref, u_given_eq_lvl = given_eq_lvl })
do { traceTc "uUnfilledVar2 not ok" $
vcat [ text "tv1:" <+> ppr tv1
, text "ty2:" <+> ppr ty2
+ , text "given_eq_lvl:" <+> ppr given_eq_lvl
, text "simple-unify-chk:" <+> ppr (simpleUnifyCheck UC_OnTheFly given_eq_lvl tv1 ty2)
]
-- Occurs check or an untouchable: just defer
@@ -3236,16 +3235,17 @@ lhsPriority tv
-> 5 -- Eliminate instantiation variables first
| otherwise
-> case info of
- CycleBreakerTv -> 0
- TyVarTv -> 1
- ConcreteTv {} -> 2
- TauTv -> 3
- RuntimeUnkTv -> 4
+ GivenCycleBreakerTv -> 0
+ TyVarTv -> 1
+ ConcreteTv {} -> 2
+ TauTv -> 3
+ RuntimeUnkTv -> 4
{- Note [Unification preconditions]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Question: given a homogeneous equality (alpha ~# ty), when is it OK to
unify alpha := ty?
+
(This note only applies to /homogeneous/ equalities, in which both
sides have the same kind.)
@@ -3285,7 +3285,7 @@ There are five reasons not to unify:
structured type. So if 'ty' is a structured type, such as (Maybe x),
don't unify.
- * CycleBreakerTv: never unified, except by restoreTyVarCycles.
+ * GivenCycleBreakerTv: never unified, except by restoreGivenCycles.
4. (CONCRETE) A ConcreteTv can only unify with a concrete type,
by definition.
@@ -3354,6 +3354,14 @@ Needless to say, all there are wrinkles:
GHC.Tc.Solver.floatEqualities, around Nov 2020. It's much easier
to unify in-place, with no floating.
+* (COERCIONS) What if there are coercions in the RHS? E.g.
+ alpha ~ (ty |> co)
+ or alpha ~ (ty co)
+ We only recurse into the `coercionType` of `co` rather than `co` itself.
+ Why? Mainly because `co` might be a coercion hole, in which case we /can't/
+ recurse into the coercion that will eventually fill the hole. This came
+ up in #26543.
+
Note [TyVar/TyVar orientation]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
See also Note [Fundeps with instances, and equality orientation]
@@ -3382,7 +3390,7 @@ So we look for a positive reason to swap, using a three-step test:
touchable and can be unified.
Tie-breaking rules for MetaTvs:
- - CycleBreakerTv: This is essentially a stand-in for another type;
+ - GivenCycleBreakerTv: This is essentially a stand-in for another type;
it's untouchable and should have the same priority as a skolem: 0.
- TyVarTv: These can unify only with another tyvar, but we can't unify
@@ -3588,7 +3596,6 @@ matchExpectedFunKind hs_ty n k = go n k
data UnifyCheckCaller
= UC_OnTheFly -- Called from the on-the-fly unifier
- | UC_QuickLook -- Called from Quick Look
| UC_Solver -- Called from constraint solver
-- | The result type of 'simpleUnifyCheck'.
@@ -3619,40 +3626,22 @@ simpleUnifyCheck :: UnifyCheckCaller -> TcLevel -> TcTyVar -> TcType -> SimpleUn
-- unification might still be OK, but it'll take more work to do
-- (use the full 'checkTyEqRhs').
--
--- It returns SUC_CannotUnify for
--- * Touchability failure
--- * Top level-shape failure
---
--- It returns SUC_NotSure for
--- * Occurs check: lhs_tv occurs in rhs_ty
--- * Type families unless fam_ok=True
--- * Foralls unless
--- lhs_tv is RuntimeUnk (used by GHCi debugger)
--- or is a QL instantiation variable
--- * Concreteness: lhs_tv is concrete and rhs is a non-concrete type
--- * Skolem-escape: a level-check for type variables, to avoid skolem escape
+-- See Note [simpleUnifyCheck]
--
-- This function is pretty heavily used, so it's optimised not to allocate
simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
- | not $ touchabilityTest given_eq_lvl lhs_tv
- = SUC_CannotUnify
- | not $ checkTopShape lhs_info rhs
- = SUC_CannotUnify
- | rhs_is_ok rhs
- = SUC_CanUnify
- | otherwise
- = SUC_NotSure
+ | not $ touchabilityTest given_eq_lvl lhs_tv = SUC_CannotUnify
+ | not $ checkTopShape lhs_info rhs = SUC_CannotUnify
+ | not (rhs_is_bad rhs) = SUC_CanUnify
+ | otherwise = SUC_NotSure
where
- lhs_info = metaTyVarInfo lhs_tv
-
- !(occ_in_ty, occ_in_co) = mkOccFolders (tyVarName lhs_tv)
-
+ lhs_info = metaTyVarInfo lhs_tv
lhs_tv_lvl = tcTyVarLevel lhs_tv
lhs_tv_is_concrete = isConcreteTyVar lhs_tv
+ lhs_tv_nm = tyVarName lhs_tv
- forall_ok = case caller of
- UC_QuickLook -> isQLInstTyVar lhs_tv
- _ -> isRuntimeUnkTyVar lhs_tv
+ -- See Note [RuntimeUnkTv] in GHC.Runtime.Heap.Inspect, esp (RUT1).
+ forall_ok = isRuntimeUnkTyVar lhs_tv
-- This fam_ok thing relates to a very specific perf problem
-- See Note [Prevent unification with type families]
@@ -3661,58 +3650,78 @@ simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
-- see if it bites us)
fam_ok = case caller of
UC_Solver -> True
- UC_QuickLook -> True
UC_OnTheFly -> False
- rhs_is_ok (TyVarTy tv)
- | lhs_tv == tv = False
- | tcTyVarLevel tv `strictlyDeeperThan` lhs_tv_lvl = False
- | lhs_tv_is_concrete, not (isConcreteTyVar tv) = False
- | occ_in_ty $! (tyVarKind tv) = False
- | otherwise = True
-
- rhs_is_ok (FunTy {ft_af = af, ft_mult = w, ft_arg = a, ft_res = r})
- | not forall_ok, isInvisibleFunArg af = False
- | otherwise = rhs_is_ok w && rhs_is_ok a && rhs_is_ok r
-
- rhs_is_ok (TyConApp tc tys)
- | lhs_tv_is_concrete, not (isConcreteTyCon tc) = False
- | not forall_ok, not (isTauTyCon tc) = False
- | not fam_ok, not (isFamFreeTyCon tc) = False
- | otherwise = all rhs_is_ok tys
-
- rhs_is_ok (ForAllTy (Bndr tv _) ty)
- | forall_ok = rhs_is_ok (tyVarKind tv) && (tv == lhs_tv || rhs_is_ok ty)
- | otherwise = False
-
- rhs_is_ok (AppTy t1 t2) = rhs_is_ok t1 && rhs_is_ok t2
- rhs_is_ok (CastTy ty co) = not (occ_in_co co) && rhs_is_ok ty
- rhs_is_ok (CoercionTy co) = not (occ_in_co co)
- rhs_is_ok (LitTy {}) = True
-
-
-mkOccFolders :: Name -> (TcType -> Bool, TcCoercion -> Bool)
--- These functions return True
--- * if lhs_tv occurs (incl deeply, in the kind of variable)
--- * if there is a coercion hole
--- No expansion of type synonyms
-mkOccFolders lhs_tv = ( getAny . runFVTop . check_ty
- , getAny . runFVTop . check_co)
- where
- check_ty :: Type -> FV BoundVars Any
- !(check_ty, _, check_co, _) = foldTyCo occ_folder
-
- occ_folder :: TyCoFolder (FV BoundVars Any)
- occ_folder = TyCoFolder { tcf_view = noView -- Don't expand synonyms
- , tcf_tyvar = do_tcv, tcf_covar = do_tcv
- , tcf_lit = mempty, tcf_hole = do_hole
- , tcf_tycobinder = addBndrFV }
+ rhs_tcv_is_bad tcv
+ -- c.f. checkTyVar, the TEFTyVar case
+ | isCoVar tcv = occ_check
+ | tcTyVarLevel tcv `strictlyDeeperThan` lhs_tv_lvl = True
+ | lhs_tv_is_concrete, not (isConcreteTyVar tcv) = True
+ | occ_check = True
+ | otherwise = False
+ where
+ occ_check = simpleOccursCheck lhs_tv_nm tcv
+
+ rhs_is_bad (TyVarTy tv) = rhs_tcv_is_bad tv
+
+ rhs_is_bad (FunTy {ft_af = af, ft_mult = w, ft_arg = a, ft_res = r})
+ | not forall_ok, isInvisibleFunArg af = True
+ | otherwise = rhs_is_bad w || rhs_is_bad a || rhs_is_bad r
+
+ rhs_is_bad (TyConApp tc tys)
+ | lhs_tv_is_concrete, not (isConcreteTyCon tc) = True
+ | not forall_ok, not (isTauTyCon tc) = True
+ | not fam_ok, not (isFamFreeTyCon tc) = True
+ | otherwise = any rhs_is_bad tys
+
+ rhs_is_bad (ForAllTy (Bndr tv _) ty)
+ | not forall_ok = True
+ | rhs_is_bad (tyVarKind tv) = True
+ | tv /= lhs_tv, rhs_is_bad ty = True
+ | otherwise = False
+
+ rhs_is_bad (AppTy t1 t2) = rhs_is_bad t1 || rhs_is_bad t2
+ rhs_is_bad (CastTy ty co) = co_is_bad co || rhs_is_bad ty
+ rhs_is_bad (CoercionTy co) = co_is_bad co
+ rhs_is_bad (LitTy {}) = False
+
+ -- Coercions: see Note [Occurs check and coercions]
+ co_is_bad co = anyFreeVarsOfCo rhs_tcv_is_bad co
+
+{- Note [simpleUnifyCheck]
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+The function `simpleUnifyCheck` is a simple, /fast/ check for unifying (tv ~ rhs).
+It can return a definite decision (SUC_CannotUnify of SUC_CanUnify), or uncertainty
+(SUC_NotSure). In the latter case we will later use `checkTyEqRhs` to resolve.
+In particular:
+
+ It returns SUC_CannotUnify for
+ * Touchability failure
+ * Top level-shape failure
+
+ It returns SUC_NotSure for
+ * Occurs check: lhs_tv occurs in rhs_ty
+ * Type families unless fam_ok=True
+ * Foralls unless
+ lhs_tv is RuntimeUnk (used by GHCi debugger)
+ or is a QL instantiation variable
+ * Concreteness: lhs_tv is concrete and rhs is a non-concrete type
+ * Skolem-escape: a level-check for type variables, to avoid skolem escape
+
+Note [Occurs check and coercions]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+What if we have
+ alpha ~ Int |> (CoHole ch :: Type ~ blah)
- do_tcv v = (MkFV $ \ bvs ->
- Any (not (v `elemVarSet` bvs) && tyVarName v == lhs_tv))
- `mappend` check_ty (varType v)
+Clearly we want to check that `alpha` doesn't appear free in `blah`. But even if
+it doesn't, what if `ch` is later filled in with a coercion mentioning `alpha`?
+Tht would be bad: we'd get an infinite structure.
- do_hole _hole = MkFV $ \ _bvs -> DM.Any True -- Reject coercion holes
+The Right Thing to do is to check for a loop when (later) filling in `ch`.
+We don't do that right now, and it's very obscure. But in `simpleUnifyCheck`
+we just look in the free vars of the /kind/ of the hole, which is what
+`anyFreeVarsOfCo` does.
+-}
{- *********************************************************************
* *
@@ -4086,13 +4095,14 @@ wantConcreteCheck = \case
-- where @cbv@ is a fresh loop-breaker tyvar (for Given), or
-- just a fresh 'TauTv' (for Wanted)
famAppBreaker :: FamAppBreaker a -> TcType -> TcM (PuResult a Reduction)
-famAppBreaker BreakGiven fam_app
+famAppBreaker BreakGiven fam_app -- Givens
= do { new_tv <- TcM.newCycleBreakerTyVar (typeKind fam_app)
; return (PuOK (unitBag (new_tv, fam_app))
(mkReflRedn Nominal (mkTyVarTy new_tv))) }
-- Why reflexive? See Detail (4) of Note [Type equality cycles]
-- in GHC.Tc.Solver.Equality
-famAppBreaker (BreakWanted ev lhs_tv) fam_app
+
+famAppBreaker (BreakWanted ev lhs_tv) fam_app -- Wanteds
-- Occurs check or skolem escape; so flatten
= do { reason <- checkPromoteFreeVars cteInsolubleOccurs
(tyVarName lhs_tv) lhs_tv_lvl
@@ -4236,10 +4246,16 @@ the RHS by
Its specific behaviour is governed by the `TyEqFlags` that are passed
to it; see Note [TyEqFlags].
-Note, however, that `checkTyEqRhs` specifically does /not/ check for:
- * Touchability of the LHS (in the case of a unification variable)
- * Shape of the LHS (e.g. we can't unify Int with a TyVarTv)
-These things are checked by `simpleUnifyCheck`.
+Wrinkles
+
+(CTER1) Note, however, that `checkTyEqRhs` specifically does /not/ check for:
+ * Touchability of the LHS (in the case of a unification variable)
+ * Shape of the LHS (e.g. we can't unify Int with a TyVarTv)
+ These things are checked by `simpleUnifyCheck`.
+
+(CTER2) Also `checkTyEqRhs` does not need to worry about RuntimeUnkTvs, because
+ the solver is never called on constraints involving them. See (RUT1) in
+ Note [RuntimeUnkTv] in GHC.Runtime.Heap.Inspect.
-}
@@ -4297,6 +4313,7 @@ check_ty_eq_rhs :: forall m a
=> TyEqFlags m a
-> TcType -- Already zonked
-> m (PuResult a Reduction)
+{-# INLINEABLE check_ty_eq_rhs #-}
check_ty_eq_rhs flags ty
= case ty of
LitTy {} -> return $ okCheckRefl ty
@@ -4327,7 +4344,7 @@ check_ty_eq_rhs flags ty
; return (mkReflCoRedn Nominal <$> co_res) }
ForAllTy {} -> return $ PuFail impredicativeProblem -- Not allowed (TyEq:F)
-{-# INLINEABLE check_ty_eq_rhs #-}
+ -- No need to worry about RuntimeUnkTv
-------------------
checkCo :: Monad m => TyEqFlags m a -> Coercion -> m (PuResult a Coercion)
@@ -4351,7 +4368,10 @@ checkCo flags co =
-- Occurs check (can promote)
| OC_Check lhs_tv occ_prob <- occ
, LC_Promote { lc_lvlp = lhs_tv_lvl } <- lc
- -> do { reason <- checkPromoteFreeVars occ_prob lhs_tv lhs_tv_lvl (tyCoVarsOfCo co)
+ -> do { reason <- checkPromoteFreeVars occ_prob lhs_tv lhs_tv_lvl $
+ tyCoVarsOfCo co
+ -- See Note [Occurs check and coercions]
+ -- and Note [checkCo] (2)
; return $
if cterHasNoProblem reason
then pure co
@@ -4648,7 +4668,7 @@ checkTyVar flags occ_tv
-- so the only reason for a filled meta-tyvar is promotion
else do_rhs_checks
- [ simpleOccursCheck occ occ_tv
+ [ tyVarOccursCheck occ occ_tv
, tyVarLevelCheck lc occ_tv
, tyVarConcretenessCheck conc occ_tv ]
}
@@ -4706,17 +4726,24 @@ promotionDone occ_tv _ (CC_Promote {}) = isFilledMetaTyVar occ_tv
promotionDone _ _ _ = return False
---------------------
-simpleOccursCheck :: OccursCheck -> TcTyVar -> TyVarCheckResult m
+tyVarOccursCheck :: OccursCheck -> TcTyCoVar -> TyVarCheckResult m
-- ^ The "interpreter" for 'OccursCheck'
-simpleOccursCheck OC_None _
- = TyVarCheck_Success
-simpleOccursCheck (OC_Check lhs_tv occ_prob) occ_tv
- | lhs_tv == tyVarName occ_tv || check_kind (tyVarKind occ_tv)
- = TyVarCheck_Error (cteProblem occ_prob)
- | otherwise
+-- Check for an occurrence of lhs_tv in occ_tv /or/ its kind
+tyVarOccursCheck OC_None _
= TyVarCheck_Success
+tyVarOccursCheck (OC_Check { occurs_tv_name = lhs_tv, occurs_problem = prob }) occ_tcv
+ | simpleOccursCheck lhs_tv occ_tcv = TyVarCheck_Error (cteProblem prob)
+ | otherwise = TyVarCheck_Success
+
+simpleOccursCheck :: Name -> TyCoVar -> Bool
+-- Check for an occurrence of lhs_tv in occ_tv /or/ its kind
+-- True <=> occurs check fires
+simpleOccursCheck lhs_tv occ_tcv
+ = is_bad occ_tcv || anyFreeVarsOfType is_bad (varType occ_tcv)
+ -- anyFreeVarsOfType looks at the /deep/ free vars of the type
where
- (check_kind, _) = mkOccFolders lhs_tv
+ is_bad :: TyCoVar -> Bool
+ is_bad occ_tcv = lhs_tv == tyVarName occ_tcv
-------------------------
tyVarLevelCheck :: LevelCheck m -> TcTyVar -> TyVarCheckResult m
@@ -4836,7 +4863,7 @@ checkTopShape info xi
RuntimeUnk -> True
MetaTv { mtv_info = TyVarTv } -> True
_ -> False
- CycleBreakerTv -> False -- We never unify these
+ GivenCycleBreakerTv -> False -- We never unify these
_ -> True
--------------------------------------------------------------------------------
@@ -5149,14 +5176,14 @@ mightEqualLater inert_set given_pred given_loc wanted_pred wanted_loc
-- True for TauTv and TyVarTv (and RuntimeUnkTv) meta-tyvars
-- (as they can be unified)
- -- and also for CycleBreakerTvs that mentions meta-tyvars
+ -- and also for GivenCycleBreakerTvs that mentions meta-tyvars
mentions_meta_ty_var :: TyVar -> Bool
mentions_meta_ty_var tv
| isMetaTyVar tv
= case metaTyVarInfo tv of
-- See Examples 8 and 9 in the Note
- CycleBreakerTv -> anyFreeVarsOfType mentions_meta_ty_var
- (lookupCycleBreakerVar tv inert_set)
+ GivenCycleBreakerTv -> anyFreeVarsOfType mentions_meta_ty_var
+ (lookupCycleBreakerVar tv inert_set)
_ -> True
| otherwise
= False
@@ -5199,7 +5226,7 @@ We want to solve w1 using g1 and g2. The presence of g3 is irrelevant, because
we cannot unify 'f[tau:1] v[tau:1]' and 'g[sk:1] x[sk:3]' due to skolem escape.
-}
--- | Return the type family application a CycleBreakerTv maps to.
+-- | Return the type family application a GivenCycleBreakerTv maps to.
lookupCycleBreakerVar :: TcTyVar -- ^ cbv, must be a CycleBreakerTv
-> InertSet
-> TcType -- ^ type family application the cbv maps to
=====================================
compiler/GHC/Types/Var/FV.hs
=====================================
@@ -9,7 +9,7 @@ module GHC.Types.Var.FV (
runFVSelective, runFVSelectiveList, runFVSelectiveSet,
InterestingVarFun,
- addBndrFV, addBndrsFV, addBndrSelectiveFV, addBndrsSelectiveFV,
+ updEnvFV, addBndrFV, addBndrsFV, addBndrSelectiveFV, addBndrsSelectiveFV,
emptyFV, -- or `mempty`
unionFV, -- or `mappend`
mapUnionFV
@@ -138,7 +138,7 @@ type BoundVars = TyCoVarSet
type VarSetFV = FV BoundVars (EndoOS TyCoVarSet)
type DVarSetFV = FV BoundVars (EndoOS DTyCoVarSet)
-type SelectiveDFV = FV (InterestingVarFun, BoundVars) (EndoOS DVarSet)
+type SelectiveDFV = FV (BoundVars, InterestingVarFun) (EndoOS DVarSet)
-- VarSetFV: collects a VarSet
-- DVarSetFV: collects a DVarSet (deterministic)
-- SelectiveDFV: selectively collects a DVarSet
@@ -158,9 +158,14 @@ pattern MkFV :: (env -> acc) -> FV env acc
{-# COMPLETE MkFV #-}
pattern MkFV f <- MkFV' f
where
- MkFV f = MkFV' (oneShot f)
+ MkFV f = MkFV' (oneShot (\env -> env `seq` f env))
-- oneShot: this is the core of the one-shot trick!
-- Note [The one-shot state monad trick] in GHC.Utils.Monad.
+ --
+ -- seq: this is /important/: it makes the function strict.
+ -- When the `env` is a pair (as it is in anyFreeVarsOfType), that means
+ -- we get good worker/wrapper splitting. Without it we get extra boxed
+ -- pairs allocated by `anyFreeVarsOfType`.
instance Semigroup a => Semigroup (FV env a) where
(<>) = unionFV
@@ -174,28 +179,28 @@ emptyFV = MkFV (\_ -> mempty)
unionFV :: Semigroup a => FV env a -> FV env a -> FV env a
unionFV (MkFV' f1) (MkFV' f2) = MkFV (\env -> f1 env <> f2 env)
-upd_bndrs_fv :: (env -> env) -> FV env a -> FV env a
-{-# INLINE addBndrFV #-}
-upd_bndrs_fv upd f = MkFV (\bvs -> runFV f $! upd bvs)
+updEnvFV :: (env -> env) -> FV env a -> FV env a
+updEnvFV upd f = MkFV (\bvs -> runFV f $! upd bvs)
-- Strict application to avoid making a thunk
addBndrFV :: TyCoVar -> FV BoundVars a -> FV BoundVars a
-addBndrFV tcv = upd_bndrs_fv (\bvs -> extendVarSet bvs tcv)
+{-# INLINE addBndrFV #-}
+addBndrFV tcv = updEnvFV (\bvs -> extendVarSet bvs tcv)
addBndrsFV :: [Var] -> FV BoundVars a -> FV BoundVars a
-addBndrsFV tcvs = upd_bndrs_fv (\bvs -> extendVarSetList bvs tcvs)
+addBndrsFV tcvs = updEnvFV (\bvs -> extendVarSetList bvs tcvs)
-addBndrSelectiveFV :: TyCoVar -> FV (f, BoundVars) a -> FV (f, BoundVars) a
+addBndrSelectiveFV :: TyCoVar -> FV (BoundVars, f) a -> FV (BoundVars, f) a
addBndrSelectiveFV tcv
- = upd_bndrs_fv (\(f,bvs) -> let !bvs' = extendVarSet bvs tcv
+ = updEnvFV (\(bvs,f) -> let !bvs' = extendVarSet bvs tcv
-- Strict let to avoid thunks
- in (f,bvs'))
+ in (bvs',f))
-addBndrsSelectiveFV :: [Var] -> FV (f, BoundVars) a -> FV (f, BoundVars) a
+addBndrsSelectiveFV :: [Var] -> FV (BoundVars, f) a -> FV (BoundVars, f) a
addBndrsSelectiveFV bs
- = upd_bndrs_fv (\(f,bvs) -> let !bvs' = extendVarSetList bvs bs
+ = updEnvFV (\(bvs,f) -> let !bvs' = extendVarSetList bvs bs
-- Strict let to avoid thunks
- in (f,bvs'))
+ in (bvs',f))
mapUnionFV :: (Foldable t, Monoid acc)
=> (a -> FV env acc) -> t a -> FV env acc
@@ -221,7 +226,7 @@ runTyCoVarsDSet f = runFVAcc f emptyDVarSet
runFVSelective :: InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective interesting f
- = runEndoOS (runFV f (interesting, emptyVarSet)) emptyDVarSet
+ = runEndoOS (runFV f (emptyVarSet, interesting)) emptyDVarSet
runFVSelectiveList :: InterestingVarFun -> SelectiveDFV -> [Var]
runFVSelectiveList interesting f = dVarSetElems (runFVSelective interesting f)
=====================================
testsuite/tests/default/default-fail05.stderr
=====================================
@@ -1,45 +1,45 @@
default-fail05.hs:11:10: error: [GHC-39999]
- • Ambiguous type variable ‘f0’ arising from a use of ‘toList’
- prevents the constraint ‘(Foldable f0)’ from being solved.
- Probable fix: use a type annotation to specify what ‘f0’ should be.
+ • Ambiguous type variable ‘t0’ arising from a use of ‘toList’
+ prevents the constraint ‘(Foldable t0)’ from being solved.
+ Probable fix: use a type annotation to specify what ‘t0’ should be.
Potentially matching instances:
instance Foldable (Either a)
-- Defined in ‘GHC.Internal.Data.Foldable’
instance Foldable Maybe -- Defined in ‘GHC.Internal.Data.Foldable’
...plus three others
- ...plus 26 instances involving out-of-scope types
+ ...plus 11 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘($)’, namely ‘toList’
In the first argument of ‘print’, namely ‘(toList $ pure 21)’
In a stmt of a 'do' block: print (toList $ pure 21)
default-fail05.hs:11:19: error: [GHC-39999]
- • Ambiguous type variable ‘f0’ arising from a use of ‘pure’
- prevents the constraint ‘(Applicative f0)’ from being solved.
- Probable fix: use a type annotation to specify what ‘f0’ should be.
+ • Ambiguous type variable ‘t0’ arising from a use of ‘pure’
+ prevents the constraint ‘(Applicative t0)’ from being solved.
+ Probable fix: use a type annotation to specify what ‘t0’ should be.
Potentially matching instances:
instance Applicative IO -- Defined in ‘GHC.Internal.Base’
instance Applicative Maybe -- Defined in ‘GHC.Internal.Base’
...plus six others
- ...plus 9 instances involving out-of-scope types
+ ...plus five instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the second argument of ‘($)’, namely ‘pure 21’
In the first argument of ‘print’, namely ‘(toList $ pure 21)’
In a stmt of a 'do' block: print (toList $ pure 21)
default-fail05.hs:12:3: error: [GHC-39999]
- • Ambiguous type variable ‘t0’ arising from a use of ‘traverse’
- prevents the constraint ‘(Traversable t0)’ from being solved.
+ • Ambiguous type variable ‘t1’ arising from a use of ‘traverse’
+ prevents the constraint ‘(Traversable t1)’ from being solved.
Relevant bindings include
- main :: IO (t0 ()) (bound at default-fail05.hs:10:1)
- Probable fix: use a type annotation to specify what ‘t0’ should be.
+ main :: IO (t1 ()) (bound at default-fail05.hs:10:1)
+ Probable fix: use a type annotation to specify what ‘t1’ should be.
Potentially matching instances:
instance Traversable (Either a)
-- Defined in ‘GHC.Internal.Data.Traversable’
instance Traversable Maybe
-- Defined in ‘GHC.Internal.Data.Traversable’
...plus three others
- ...plus 28 instances involving out-of-scope types
+ ...plus 13 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In a stmt of a 'do' block: traverse print (pure 42)
In the expression:
@@ -51,16 +51,16 @@ default-fail05.hs:12:3: error: [GHC-39999]
traverse print (pure 42)
default-fail05.hs:12:19: error: [GHC-39999]
- • Ambiguous type variable ‘t0’ arising from a use of ‘pure’
- prevents the constraint ‘(Applicative t0)’ from being solved.
+ • Ambiguous type variable ‘t1’ arising from a use of ‘pure’
+ prevents the constraint ‘(Applicative t1)’ from being solved.
Relevant bindings include
- main :: IO (t0 ()) (bound at default-fail05.hs:10:1)
- Probable fix: use a type annotation to specify what ‘t0’ should be.
+ main :: IO (t1 ()) (bound at default-fail05.hs:10:1)
+ Probable fix: use a type annotation to specify what ‘t1’ should be.
Potentially matching instances:
instance Applicative IO -- Defined in ‘GHC.Internal.Base’
instance Applicative Maybe -- Defined in ‘GHC.Internal.Base’
...plus six others
- ...plus 9 instances involving out-of-scope types
+ ...plus five instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the second argument of ‘traverse’, namely ‘(pure 42)’
In a stmt of a 'do' block: traverse print (pure 42)
=====================================
testsuite/tests/ghci.debugger/scripts/T14628.stderr
=====================================
@@ -1,9 +1,9 @@
-
-<interactive>:4:17: error: [GHC-25897]
+<interactive>:4:7: error: [GHC-25897]
• Couldn't match type ‘m’ with ‘(,) a0’
- Expected: StateT Int ((,) a0) ()
- Actual: StateT Int m ()
+ Expected: (a0, ((), Int))
+ Actual: m ((), Int)
‘m’ is an interactive-debugger skolem
- • In the first argument of ‘runStateT’, namely ‘_result’
- In the second argument of ‘($)’, namely ‘runStateT _result 0’
+ • In the second argument of ‘($)’, namely ‘runStateT _result 0’
In the expression: snd $ runStateT _result 0
+ In an equation for ‘it’: it = snd $ runStateT _result 0
+
=====================================
testsuite/tests/impredicative/T27917.hs
=====================================
@@ -0,0 +1,29 @@
+{-# LANGUAGE ImpredicativeTypes #-}
+module T27917 where
+
+pair :: forall p q. Maybe p -> q -> (p, q)
+pair _ _ = undefined
+
+ids :: [forall a. a -> a]
+ids = []
+
+use :: forall s. s -> ()
+use _ = ()
+
+isMaybe :: Maybe a -> ()
+isMaybe (Just {}) = ()
+
+-- #27917 reported that foo1 was accepted but foo2 was rejected!
+foo1 = \y -> (isMaybe y, use (pair y ids))
+foo2 = \y -> (use (pair y ids), isMaybe y)
+
+{- y :: Maybe alpha
+ pair @k1 @k2 y ids
+
+Maybe k1 ~ Maybe alpha
+k2 ~ [ forall a. a->a ]
+
+use @ss (pair ...)
+
+ss ~ (k1, [forall a. a->a])
+-}
=====================================
testsuite/tests/impredicative/all.T
=====================================
@@ -22,3 +22,4 @@ test('T17332', normal, compile_fail, [''])
test('expr-sig', normal, compile, [''])
test('Dict', normal, compile, [''])
test('T24676', normal, compile, [''])
+test('T27917', normal, compile, [''])
=====================================
testsuite/tests/indexed-types/should_fail/T1897b.stderr
=====================================
@@ -1,14 +1,14 @@
T1897b.hs:16:1: error: [GHC-83865]
- • Couldn't match type: Depend b0
- with: Depend b
- Expected: t (Depend b) -> Bool
- Actual: t (Depend b0) -> Bool
+ • Couldn't match type: Depend a0
+ with: Depend a
+ Expected: t (Depend a) -> Bool
+ Actual: t (Depend a0) -> Bool
Note: ‘Depend’ is a non-injective type family.
- The type variable ‘b0’ is ambiguous
+ The type variable ‘a0’ is ambiguous
• In the ambiguity check for the inferred type for ‘isValid’
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
When checking the inferred type
- isValid :: forall {t :: * -> *} {b}.
- (Foldable t, Bug b) =>
- t (Depend b) -> Bool
+ isValid :: forall {t :: * -> *} {a}.
+ (Foldable t, Bug a) =>
+ t (Depend a) -> Bool
=====================================
testsuite/tests/typecheck/should_compile/LocalGivenEqs.hs
=====================================
@@ -22,7 +22,7 @@ it mentions b in its RHS. It thus triggers Note [Type equality cycles]
in GHC.Tc.Solver.Equality. That Note says we change the situation to
[G] b ~ cbv GhcPs
[G] Body b ~ cbv
-for some fresh CycleBreakerTv cbv. Now, our original equality looks to be let-like,
+for some fresh GivenCycleBreakerTv cbv. Now, our original equality looks to be let-like,
but the new cbv equality is *not* let-like -- note that the variable is on the RHS.
The solution is to consider any equality whose free variables are all at the current
level to not stop equalities from floating. These are called *local*. Because both
=====================================
testsuite/tests/typecheck/should_compile/T26543_orig.hs
=====================================
@@ -0,0 +1,36 @@
+-- This test is from the Description of #26543
+
+{-# LANGUAGE GHC2024, TypeAbstractions, AllowAmbiguousTypes, NoImplicitPrelude,
+ TypeFamilies, UndecidableInstances #-}
+module T26543_orig where
+
+import Data.Kind
+import Control.Applicative (Applicative(..))
+import Prelude (type (~), ($))
+
+type CAT k = k -> k -> Type
+
+type family (~>) :: CAT k
+type family Ob (a :: k) :: Constraint
+type family UN (w :: j -> k) (wa :: k) :: j
+
+class HasBinaryProducts k where
+ type (a :: k) && (b :: k) :: k
+ withObProd :: (Ob (a :: k), Ob b) => ((Ob (a && b)) => r) -> r
+ (&&&) :: ((a :: k) ~> x) -> (a ~> y) -> (a ~> (x && y))
+
+data AP (f :: Type -> Type) k = A k
+type instance UN A (A k) = k
+
+type Ap :: CAT (AP f k)
+data Ap a b where
+ Ap :: forall {k} a b f. (Ob a, Ob b) => f (a ~> b) -> Ap (A a :: AP f k) (A b)
+
+type instance (~>) = Ap
+type instance Ob a = (a ~ A (UN A a), Ob (UN A a))
+
+instance (Applicative f, HasBinaryProducts k) => HasBinaryProducts (AP f k) where
+ type a && b = A (UN A a && UN A b)
+ withObProd @(A a) @(A b) r = withObProd @k @a @b r
+ -- (&&&) :: Ap (a :: AP f k) x -> Ap a y -> Ap a (x && y)
+ Ap @_ @x f &&& Ap @_ @y g = withObProd @k @x @y $ Ap (liftA2 (&&&) f g)
=====================================
testsuite/tests/typecheck/should_compile/all.T
=====================================
@@ -971,3 +971,5 @@ test('LazyFieldAnnotations', normal, compile, [''])
test('T27557', normal, compile, [''])
test('T27664', normal, compile, [''])
test('T27916', normal, compile, [''])
+test('T26543_orig', normal, compile, [''])
+
=====================================
testsuite/tests/typecheck/should_fail/T22707.stderr
=====================================
@@ -1,38 +1,11 @@
-T22707.hs:6:59: error: [GHC-25897]
- • Couldn't match expected type ‘i1’
- with actual type ‘m0
- GHC.Internal.Types.LiftedRep GHC.Internal.Types.LiftedRep b0’
- ‘i1’ is a rigid type variable bound by
- the type signature for:
- t1 :: forall i2 o i1 a. Cont (i2 -> o) i1 a -> Cont o i2 (a -> i1)
- at T22707.hs:5:1-47
- • In the expression: evalCont (t1 c) >>== \ ati1 -> return ati1 xa
- In the second argument of ‘($)’, namely
- ‘\ xa -> evalCont (t1 c) >>== \ ati1 -> return ati1 xa’
- In the second argument of ‘runCont’, namely
- ‘(ati1tti2 $ \ xa -> evalCont (t1 c) >>== \ ati1 -> return ...)’
- • Relevant bindings include
- ati1tti2 :: (a -> i1) -> i2 (bound at T22707.hs:6:16)
- c :: Cont (i2 -> o) i1 a (bound at T22707.hs:6:4)
- t1 :: Cont (i2 -> o) i1 a -> Cont o i2 (a -> i1)
- (bound at T22707.hs:6:1)
-
T22707.hs:6:72: error: [GHC-25897]
- • Couldn't match type ‘o’
- with ‘m0
- GHC.Internal.Types.LiftedRep
- GHC.Internal.Types.LiftedRep
- (m0 GHC.Internal.Types.LiftedRep GHC.Internal.Types.LiftedRep b0)’
+ • Couldn't match type ‘i2’ with ‘a -> i1’
Expected: Cont
- ((a -> i1)
- -> m0
- GHC.Internal.Types.LiftedRep
- GHC.Internal.Types.LiftedRep
- (m0 GHC.Internal.Types.LiftedRep GHC.Internal.Types.LiftedRep b0))
+ ((a -> i1) -> m0 (a -> i1) (a -> i1) (m0 (a -> i1) (a -> i1) b0))
i1
a
Actual: Cont (i2 -> o) i1 a
- ‘o’ is a rigid type variable bound by
+ ‘i2’ is a rigid type variable bound by
the type signature for:
t1 :: forall i2 o i1 a. Cont (i2 -> o) i1 a -> Cont o i2 (a -> i1)
at T22707.hs:5:1-47
@@ -40,6 +13,9 @@ T22707.hs:6:72: error: [GHC-25897]
In the first argument of ‘evalCont’, namely ‘(t1 c)’
In the first argument of ‘(>>==)’, namely ‘evalCont (t1 c)’
• Relevant bindings include
+ xa :: a (bound at T22707.hs:6:53)
+ ati1tti2 :: (a -> m0 (a -> i1) (a -> i1) b0) -> a -> i1
+ (bound at T22707.hs:6:16)
c :: Cont (i2 -> o) i1 a (bound at T22707.hs:6:4)
t1 :: Cont (i2 -> o) i1 a -> Cont o i2 (a -> i1)
(bound at T22707.hs:6:1)
=====================================
testsuite/tests/typecheck/should_fail/T26543a.hs
=====================================
@@ -0,0 +1,18 @@
+{-# LANGUAGE ImpredicativeTypes, PolyKinds #-}
+
+-- Should be rejected via an occurs-check in the kinds,
+-- but was erroneously accpeted with ImpredicativeTypes
+-- See !16566
+
+module T26543a where
+
+import Data.Kind
+import Data.Proxy
+
+h :: forall k (m :: k -> Type) (a :: k). (Proxy k, Proxy (m a))
+h = (Proxy, Proxy)
+
+use2 :: forall t. (Proxy t, Proxy t) -> ()
+use2 _ = ()
+
+test = use2 h
=====================================
testsuite/tests/typecheck/should_fail/T26543a.stderr
=====================================
@@ -0,0 +1,10 @@
+T26543a.hs:18:13: error: [GHC-27958]
+ • Couldn't match type ‘t0’ with ‘m0 a0’
+ Expected: (Proxy t0, Proxy t0)
+ Actual: (Proxy t0, Proxy (m0 a0))
+ Type variable kinds:
+ m0 :: t0 -> *
+ a0 :: t0
+ • In the first argument of ‘use2’, namely ‘h’
+ In the expression: use2 h
+ In an equation for ‘test’: test = use2 h
=====================================
testsuite/tests/typecheck/should_fail/all.T
=====================================
@@ -767,4 +767,4 @@ test('T27210', normal, compile_fail, [''])
test('T26532', normal, compile_fail, [''])
test('T27731', normal, compile_fail, [''])
test('T27844', normal, compile_fail, [''])
-
+test('T26543a', normal, compile_fail, [''])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/4f00da25a20998133e19af1cfd1382…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/4f00da25a20998133e19af1cfd1382…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
Simon Peyton Jones pushed to branch wip/T27910 at Glasgow Haskell Compiler / GHC
Commits:
90f4a34f by Simon Peyton Jones at 2026-10-14T10:08:11+01:00
Wibbles
- - - - -
2 changed files:
- compiler/GHC/Core.hs
- compiler/GHC/Core/Utils.hs
Changes:
=====================================
compiler/GHC/Core.hs
=====================================
@@ -490,7 +490,7 @@ This is a well-kinded type, in an environment-based scheme. But `typeKind`
will panic, because the kind of the body of a forall must not mention
the forall'd variable (and the inner forall's body appears to do so).
-It might be possible to make `exprType` and `typeKind`` more complicated, so that
+It might be possible to make `exprType` and `typeKind` more complicated, so that
they do renaming on the fly. But instead we impose
INVARIANT (NoTypeShadowing):
=====================================
compiler/GHC/Core/Utils.hs
=====================================
@@ -2207,6 +2207,8 @@ fun_always_ok _ = True
-- | A special version of 'exprOkForSpeculation' used during
-- Note [Speculative evaluation]. When the predicate arg `fun_ok` returns False
-- for `b`, then `b` is never considered ok-for-spec.
+--
+-- This is the whole reason for the fun_ok arg to expr_ok.
exprOkForSpecEval :: (Id -> Bool) -> CoreExpr -> Bool
exprOkForSpecEval fun_ok = expr_ok fun_ok primOpOkForSpeculation
@@ -2258,7 +2260,7 @@ expr_ok fun_ok primop_ok other_expr
-----------------------------
app_ok :: (Id -> Bool) -> (PrimOp -> Bool) -> Id -> [CoreArg] -> Bool
-app_ok fun_ok primop_ok fun args
+app_ok fun_ok primop_ok fun val_args
| not (fun_ok fun)
= False -- This code path is only taken for exprOkForSpecEval
-- See Note [Speculative evaluation]
@@ -2282,17 +2284,18 @@ app_ok fun_ok primop_ok fun args
ClassOpId _ is_terminating_result
| is_terminating_result -- See Note [exprOkForSpeculation and type classes]
- -> assertPpr (n_val_args == 1) (ppr fun $$ ppr args) $
+ -> assertPpr (n_val_args == 1) (ppr fun $$ ppr val_args) $
True
-- assert: terminating result type => can't be applied;
-- c.f the _other case below
PrimOpId op _
| primOpIsDiv op
- , Lit divisor <- last args
+ , Lit divisor <- last val_args
-- there can be 2 args (most div primops) or 3 args
-- (WordQuotRem2Op), hence the use of last/init
- -> not (isZeroLit divisor) && all (expr_ok fun_ok primop_ok) (init args)
+ -> not (isZeroLit divisor) &&
+ all ok_for_spec (init val_args)
-- Special case for dividing operations that fail
-- In general they are NOT ok-for-speculation
-- (which primop_ok will catch), but they ARE OK
@@ -2301,12 +2304,14 @@ app_ok fun_ok primop_ok fun args
-- can get rid of a thunk in an inner loop
| otherwise -> primop_ok op && args_ok
+ -- Assumes that the primop does not evaluate its
+ -- definitely-lifted arguments
_other -- Unlifted and terminating types;
-- Also c.f. the Var case of exprIsHNF
| isTerminatingType fun_ty -- See Note [exprOkForSpeculation and type classes]
|| definitelyUnliftedType fun_ty
- -> assertPpr (n_val_args == 0) (ppr fun $$ ppr args)
+ -> assertPpr (n_val_args == 0) (ppr fun $$ ppr val_args)
True -- Both terminating types (e.g. Eq a), and unlifted types (e.g. Int#)
-- are non-functions and so will have no value args. The assert is
-- just to check this.
@@ -2322,37 +2327,40 @@ app_ok fun_ok primop_ok fun args
-- for evaluated-ness of the fun;
-- see Note [exprOkForSpeculation and evaluated variables]
where
- fun_ty = idType fun
- n_val_args = valArgCount args
- (arg_tys, _) = splitPiTys fun_ty -- arg_tys are /uninstantiated/
- -- See (EOKS1)
+ ok_for_spec = expr_ok fun_ok primop_ok -- Used for recursive calls
+
+ fun_ty = idType fun
+ n_val_args = length val_args
+ val_arg_tys = uninstantiatedValArgTys fun_ty
+ -- val_arg_tys are /uninstantiated/; see (EOKS1)
-- Even if a function call itself is OK, any unlifted
-- args are still evaluated eagerly and must be checked
-- arg_ok bndr arg = field_ok bndr NotMarkedStrict arg
-- See Note [exprOkForSpeculation] (a-c)
- args_ok = all2Prefix arg_ok arg_tys args
- arg_ok :: PiTyVarBinder -> CoreExpr -> Bool
- arg_ok (Named _) _ = True
- arg_ok (Anon ty _) arg = exprIsCheap arg || definitelyLiftedType (scaledThing ty)
+ args_ok = all2Prefix arg_ok val_arg_tys val_args
+ arg_ok :: Type -> CoreExpr -> Bool
+ arg_ok arg_ty arg = ok_for_spec arg || definitelyLiftedType arg_ty
-- Used for strict DataCon worker arguments
-- See (SFC1) of Note [Strict fields in Core]
-- See Note [exprOkForSpeculation] (a-c)
- fields_ok str_marks = all3Prefix field_ok arg_tys str_marks args
- field_ok :: PiTyVarBinder -> StrictnessMark -> CoreExpr -> Bool
- field_ok (Named _) _ _ = True
- field_ok (Anon ty _) str arg
- = case typeLevity_maybe (scaledThing ty) of
+ fields_ok str_marks = all3Prefix field_ok val_arg_tys str_marks val_args
+ field_ok :: Type -> StrictnessMark -> CoreExpr -> Bool
+ field_ok arg_ty str arg
+ = case typeLevity_maybe arg_ty of
Just Lifted -> str == NotMarkedStrict
- Just Unlifted -> exprIsCheap arg
- Nothing -> exprIsCheap arg && str == NotMarkedStrict
- -- The Nothing case happens for
+ Just Unlifted -> ok_for_spec arg
+ Nothing | str == NotMarkedStrict
+ , definitelyLiftedType (exprType arg)
+ -> True
+ | otherwise
+ -> ok_for_spec arg
+ -- The Nothing case: consider
-- (#,#) @rep1 @rep2 @ty1 @ty2 arg1 arg2
-- We don't /instantiate/ the type (though we could) so we don't know
- -- if lifted or not, so we conservatively check both conditions. This
- -- gives a conservative answer False for (# null xs, null ys #), but
- -- these unboxed tuples are usually ANF'd so it doesn't reallly matter.
+ -- if the argument type is lifted or not. So we look at the type of
+ -- the /actual/ argument (it's usually a variable) instead.
-----------------------------
altsAreExhaustive :: [Alt b] -> Bool
@@ -2405,8 +2413,8 @@ Is the call (K e1 e2 e3) ok-for-speculation? Lok at the arguments e1..e3,
and their types. The call is ok-for-speculation if each argument satisfies
`field_ok`, namely:
(a) Arg type is definitely lifted => arg is not marked strict
- (b) Arg type is definitely unlifted => (exprIsCheap arg) is True
- (c) Otherwise => not marked strict && exprIsCheap
+ (b) Arg type is definitely unlifted => (exprOkForSpeculation arg) is True
+ (c) Otherwise => not marked strict && exprOkForSpeculation
For (a), an arbitrary /lazy/ argument is ok provided we don't have a bang.
For a strict argument you might wonder if we could use exprIsHNF, but see
@@ -2414,7 +2422,7 @@ Note [exprOkForSpeculation and evaluated variables]. So for strict arguments
we just say "no".
For (b) a variable, or casted variable is fine. But not a call, say (f x).
-Hence `exprisCheap`.
+Hence `exprOkForSpeculation`.
For (c) we don't know if we are in case (a) or (b) so we conservatively 'and'
them. See (EOKS1) for how we can get this situation.
@@ -2426,8 +2434,8 @@ them. See (EOKS1) for how we can get this situation.
If we have a call
(#,#) @LiftedRep @IntRep @Int @Int# 3 4#
then we could see accurate liftedness by instantiation, but we don't try.
- Typically the argument is a variable, in which case `exprIsCheap` does a good job.
-
+ Instead we get the actual type from (exprType arg). That could be expensive,
+ but it really only happens for unboxed tuples.
Note [exprOkForSpeculation and type classes]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2713,8 +2721,7 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e
-- PAP: Check unlifted val_args
args_hnf = all2Prefix check_arg val_arg_tys val_args
- fun_ty = idType id
- val_arg_tys = mapMaybe anonPiTyBinderType_maybe (collectPiTyBinders fun_ty)
+ val_arg_tys = uninstantiatedValArgTys (idType id)
-- val_arg_tys = map exprType val_args, but much less costly.
-- The obvious definition regresses T16577 by 30% so we don't do it.
@@ -2739,6 +2746,12 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e
| otherwise
= Nothing
+uninstantiatedValArgTys :: Type -> [Type]
+-- Returns the value argument types.
+-- E.g. uninstantiatedValArgTys (forall a. a -> Int -> T a)
+-- returns [a, Int]
+uninstantiatedValArgTys fun_ty
+ = [ scaledThing ty | Anon ty _ <- collectPiTyBinders fun_ty ]
{-
Note [exprIsHNF Tick]
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/90f4a34f42139dec0e72792d5499773…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/90f4a34f42139dec0e72792d5499773…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/T27910] 12 commits: Remove the Eq and Ord instances for ForAllTyFlag
by Simon Peyton Jones (@simonpj) 14 Oct '26
by Simon Peyton Jones (@simonpj) 14 Oct '26
14 Oct '26
Simon Peyton Jones pushed to branch wip/T27910 at Glasgow Haskell Compiler / GHC
Commits:
d07a5176 by Simon Peyton Jones at 2026-10-13T18:10:39-04:00
Remove the Eq and Ord instances for ForAllTyFlag
In Note [Comparing visibility] in GHC.Core.TyCo.Compare we read that
"we always compare ForAllTyFlags with `eqForAllVis` or `cmpForAllVis`"
But alas there was an Eq and Ord instance for ForAllTyFlag, which made it
impossible to check this claim. So I made these changes:
* Move the definition of ForAllTyFlag and its related functions from
L.H.S.Specificity (where it did not belong, since L.H.S didn't use it)
into GHC.Types.Var.
* Remove the Eq and Ord instances for ForAllTyFlag
* Deal with the fallout, by using `eqForAllVis` or related functions.
* In interface files we use IfaceType as a key for a de-deduplication map
See Note [Deduplication during iface binary serialisation]
in GHC.Iface.Binary. For this we need /exact/ comparison, so we use a
newtype wrapper for IfaceForAllTyFlag, and give use `cmpForAllTyFlagExactly`
to implement its Ord instance. Tiresome but easy.
* Rename `toIfaceForAllBndrs` to `toIfaceForAllTyBndrs`
- - - - -
ea5c054d by Simon Peyton Jones at 2026-10-13T18:10:39-04:00
Refactor type and coercion substitution
This MR refactors type and coercion substitution so that they use
TyCoMapper, rather than hand-rolled code. Fundamentally it's a simple
idea, but I had to do quite a bit of refactoring to make it fly.
I got Claude to help me this this refactoring, but I paid careful
attention to every line of code.
Main changes:
* New module GHC.Core.TyCo.Make which defines TyCoMapper, and a bunch of other
simple "smart constructors" for types and coercions. See
GHC.Core.TyCo.Rep Note [The Type-related module hierarchy], esp (TRM1)
* The new TyCoMapper has new fields `tcm_tcapp_ty` and `tcm_tcapp_co` to handle
type-constructor applications. These replace `tcm_tycon`.
* Rewrite GHC.Core.TyCo.Subst to use TyCoMapper
* Rewrite GHC.Core.Type.expandTypeSynonyms to use TyCoMapper.
* Implement `seqType` and `seqCo` with TyCoFolder. To to this I had to add
a `tcf_lit` field to TyCoFolder.
* A new module GHC.Utils.StrictIdentity to define a strict identity monad,
which itself is used for
substTyCoMapper :: TyCoMapper Subst StrictIdentity
* I removed an apparently-unused instance for Binary Levity
Refactoring `seq` functions
* In GHC.Core.Seq, redefine `seqOneShot`, `seqRuleInfo`, and friends to return
`SeqResult`, a new type defined in GHC.Types.Baseic, rather than `()`. We
can define a Monoid instance for `SeqResult` that does the right thing.
* This has knock-on effects across the compiler.
- - - - -
34165e23 by Simon Peyton Jones at 2026-10-13T18:10:39-04:00
Refactor TyCoMapper to take a single monad; add StrictReaderT
TyCoMapper used to be parameterised over both an environment and a
monad, with fields like
tcm_tyvar :: env -> TyVar -> m Type
Now it is parameterised over just the monad:
tcm_tyvar :: TyVar -> m Type
If the mapping needs an environment, `m` is a reader monad over that
environment. Many uses (e.g. zonkTcTypeMapper, swizzleMapper) need no
environment at all, and used to pass a dummy (). The binder field
becomes
tcm_tycobinder :: forall r. TyCoVar -> ForAllTyFlag
-> (TyCoVar -> m r) -> m r
and runs the continuation in the extended environment. mapTyCoX is gone;
only mapTyCo remains.
To make this efficient, the reader monad must use the one-shot trick and
be strict in the environment. So:
* New module GHC.Utils.Monad.StrictReader defines
newtype StrictReaderT r m a
with hand-written instances. The builder of the pattern synonym
`StrictReaderT` is a smart constructor that adds both `oneShot` and a
bang on the environment, so every StrictReaderT computation is strict
in the environment. See Note [Instances for StrictReaderT].
* ZonkT becomes type ZonkT = StrictReaderT ZonkEnv
ZonkM becomes type ZonkM = StrictReaderT ZonkGblEnv IO
replacing their own hand-written instances.
* Substitution uses
type SubstM = StrictReaderT Subst StrictIdentity
substTyCoMapper :: TyCoMapper SubstM
and similarly expandTypeSynonymMapper.
* zonk_tycomapper :: TyCoMapper (ZonkT TcM) is now much simpler.
* swizzleMapper uses StrictIdentity rather than Identity.
* Module renaming, so that the strict monads are all peers in GHC.Utils.Monad:
GHC.Utils.StrictIdentity ==> GHC.Utils.Monad.StrictIdentity
GHC.Utils.Monad.State.Strict ==> GHC.Utils.Monad.StrictState
alongside the new GHC.Utils.Monad.StrictReader.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
c0299e15 by Simon Jakobi at 2026-10-13T18:11:23-04:00
testsuite: Line-buffer the driver's stdout
Python block-buffers stdout when it is a pipe. This is the case for
example in CI. As a result there is often no indication of progress at
all until the run ends.
To fix this, we configure it explicitly to use line buffering instead.
stderr is reconfigured too, for consistency: Python line-buffers it by
default since 3.9.
The type-ignores are for mypy, which types these handles as TextIO — an
interface without reconfigure.
Fixes #12934.
Assisted-by: Claude Fable 5
- - - - -
9b5beaa2 by Florian Ragwitz at 2026-10-13T18:12:05-04:00
hpc: Improve coverage semantics for instance methods and instances
Users care about which of their explicitly written methods are or aren't
covered, not whether all of the inherited methods are covered as well. Those
should be covered where they are implemented.
We therefore skip generating top-level boxes for inherited methods which will
appear as uncovered with no clear indication as to why, and no way to fix other
than exercising code the user has not written.
Previously, top-level boxes for those inherited methods would use the instance
head as their source span. Now that those boxes are gone, we can use that
source span for another purpose:
We introduce a new top-level box for each class instance with its instance head
as the source span, and use that to indicate whether or not an instance has
been covered at all.
We do this by making any method of the instance, inherited/generated or
otherwise, tick the box for the instance head. Excluded from this are
methodless classes, which could never be covered using the current mechanism.
This could be improved upon further.
These changes apply to ticking for program coverage only, and don't affect
source notes. Additionally, we update the scope of record selector ticking to
be limited to program coverage as well, as the oversight to also extend it to
source notes has previously caused bugs and because record selector ticking is
coverage-specific.
Fixes #17155, #15932.
- - - - -
836ec50a by Simon Peyton Jones at 2026-10-13T18:12:54-04:00
Refactor fundeps again
This MR fixes #27828; there was a bad scoping error in `trim_qtvs`.
The main changes are
* Refactor `trim_qtvs` completely to eliminate the bug.
* Remove a redundant `apartnessCheck` from `mkTopOpenFamEqFDs`.
* Lots of improvements to the Notes in FunDeps.hs
I also did a couple of pure refactors:
* I realised that we were carefully caching `cab_incomps` in each
`CoAxBranch`, to make it more efficient to reduce a type family application,
but we were not doing the same thing when computing relevance for functional
dependencies; see Note [Relevance] in GHC.Tc.Solver.FunDeps. So I added
`cab_overlaps`, which is now used in `noEarlierMatch`.
* Use a proper data types, `AmIUnifying` and `OuterKindsFlag`, for two
previously-Boolean arguments to `tc_unify_tys`. This is a pure refactor.
- - - - -
889ef371 by Simon Peyton Jones at 2026-10-13T18:13:36-04:00
Refactor and simplify solver's unification code
Ticket #27844 showed that GHC's constraint solver could miss an
occurs check, and thereby build an infinite type.
This patch significantly simplifes the code in
GHC.Tc.Solver.Equality.canEqCanLHSFinish_try_unification
GHC.Tc.Solver.Equality.canEqCanLHSFinish_no_unification
It is now significantly simpler, and requires much less subtle
reasoning to understand.
Main changes
* If `canEqCanLHSFinish_try_unification` fails to unify, it now /always/ calls
`canEqCanLHSFinish_no_unification` rather than trying to finish the job itself.
* `GHC.Tc.Solver.Monad.checkTypeEq` is moved from Monad into Equality, and renamed
to `GHC.Tc.Solver.Equality.checkTypeEqNoUnification`.
* `canEqCanLHSFinish_no_unification` calls `checkTypeEqNoUnification` to test
the invariants for an inert equality. Before this MR there was a special case for
`do_not_prevent_rewriting`, but that has gone; instead it's just a consequence of
the flags passed to `checkTyEqRhs`, documented in the `PuOK` case of
`canEqCanLHSFinish_no_unification`.
* There is only one bit of "swap back" code, in `finish_no_unify` in
`canEqCanLHSFinish_try_unification. Previously this code was duplicated.
* `kickOutAfterFillingCoercionHole` should account for equalities with a
TyFamLHS. See (KOC5) in Note [Kick out after filling a coercion hole]
* No need to gather coercion holes in the `PuOK` case of `canEqCanLHSFinish_no_unification`.
See documentation with that code.
- - - - -
f9193e53 by Simon Peyton Jones at 2026-10-13T18:13:37-04:00
One line fix to fallout from
...#27731, !16564, simplifying Tc.Errors machinery
There was an over-eager assert!
- - - - -
cdd56b7a by Florian Ragwitz at 2026-10-13T18:14:19-04:00
Fix several sphinx errors and warnings from rendering the release notes
The extension references which are removed here are for extension which were
already previously undocumented, so just removing them seems fine for now.
- - - - -
4f00da25 by Florian Ragwitz at 2026-10-13T18:14:19-04:00
Link to QualifiedStrings documentation
- - - - -
0a55828e by Simon Peyton Jones at 2026-10-14T09:10:11+01:00
Improve exprOkForSpeculation
Fixes #27910, which exposed an infelicity in exprOkForSpeculation
The new story is carefully described in new Note [exprOkForSpeculation],
esp (a-c) and (EOKS1).
- - - - -
72ee660b by Simon Peyton Jones at 2026-10-14T09:54:30+01:00
Wibbles
- - - - -
131 changed files:
- changelog.d/26616
- changelog.d/T27329
- + changelog.d/T27828
- + changelog.d/T27844
- changelog.d/boxing-family
- + changelog.d/hpc-classinsts
- changelog.d/tcplugin_init.md
- changelog.d/unit-index
- compiler/GHC/Builtin/WiredIn/Prim.hs
- compiler/GHC/Builtin/WiredIn/Types/Box.hs
- compiler/GHC/Cmm/GenericOpt.hs
- compiler/GHC/Cmm/UniqueRenamer.hs
- compiler/GHC/CmmToAsm/Dwarf/Types.hs
- compiler/GHC/CmmToAsm/Monad.hs
- compiler/GHC/CmmToAsm/Reg/Graph/Spill.hs
- compiler/GHC/CmmToAsm/Reg/Graph/SpillClean.hs
- compiler/GHC/CmmToAsm/Reg/Graph/SpillCost.hs
- compiler/GHC/CmmToAsm/Reg/Graph/Stats.hs
- compiler/GHC/CmmToAsm/Reg/Linear/State.hs
- compiler/GHC/CmmToAsm/Reg/Linear/Stats.hs
- compiler/GHC/CmmToAsm/Reg/Liveness.hs
- compiler/GHC/CmmToAsm/Wasm/Asm.hs
- compiler/GHC/CmmToAsm/Wasm/Types.hs
- compiler/GHC/CmmToC.hs
- compiler/GHC/Core.hs
- compiler/GHC/Core/Coercion.hs
- compiler/GHC/Core/Coercion.hs-boot
- compiler/GHC/Core/Coercion/Axiom.hs
- compiler/GHC/Core/Coercion/Opt.hs
- compiler/GHC/Core/DataCon.hs
- compiler/GHC/Core/FamInstEnv.hs
- compiler/GHC/Core/Lint.hs
- compiler/GHC/Core/Map/Type.hs
- compiler/GHC/Core/Opt/Arity.hs
- compiler/GHC/Core/Opt/CallerCC.hs
- compiler/GHC/Core/Opt/Exitify.hs
- compiler/GHC/Core/Opt/Specialise.hs
- compiler/GHC/Core/Reduction.hs
- compiler/GHC/Core/Seq.hs
- compiler/GHC/Core/TyCo/Compare.hs
- compiler/GHC/Core/TyCo/FVs.hs
- + compiler/GHC/Core/TyCo/Make.hs
- + compiler/GHC/Core/TyCo/Make.hs-boot
- compiler/GHC/Core/TyCo/Ppr.hs
- compiler/GHC/Core/TyCo/Rep.hs
- compiler/GHC/Core/TyCo/Rep.hs-boot
- compiler/GHC/Core/TyCo/Subst.hs
- compiler/GHC/Core/Type.hs
- compiler/GHC/Core/Type.hs-boot
- compiler/GHC/Core/Unify.hs
- compiler/GHC/Core/Utils.hs
- compiler/GHC/CoreToIface.hs
- compiler/GHC/CoreToIface.hs-boot
- compiler/GHC/Driver/Downsweep.hs
- compiler/GHC/Driver/Main/Compile.hs
- compiler/GHC/HsToCore.hs
- compiler/GHC/HsToCore/Ticks.hs
- compiler/GHC/Iface/Decl.hs
- compiler/GHC/Iface/Ext/Types.hs
- compiler/GHC/Iface/Ext/Utils.hs
- compiler/GHC/Iface/Syntax.hs
- compiler/GHC/Iface/Type.hs
- compiler/GHC/Iface/Type.hs-boot
- compiler/GHC/IfaceToCore.hs
- compiler/GHC/Runtime/Heap/Inspect.hs
- compiler/GHC/Stg/BcPrep.hs
- compiler/GHC/Stg/Debug.hs
- compiler/GHC/Stg/EnforceEpt/Rewrite.hs
- compiler/GHC/Stg/EnforceEpt/TagSig.hs
- compiler/GHC/Stg/Subst.hs
- compiler/GHC/Tc/Deriv/Functor.hs
- compiler/GHC/Tc/Errors.hs
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Gen/App.hs
- compiler/GHC/Tc/Instance/FunDeps.hs
- compiler/GHC/Tc/Solver/Equality.hs
- compiler/GHC/Tc/Solver/FunDeps.hs
- compiler/GHC/Tc/Solver/InertSet.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/TyCl.hs
- compiler/GHC/Tc/TyCl/Instance.hs
- compiler/GHC/Tc/Types/Constraint.hs
- compiler/GHC/Tc/Utils/Unify.hs
- compiler/GHC/Tc/Zonk/Env.hs
- compiler/GHC/Tc/Zonk/Monad.hs
- compiler/GHC/Tc/Zonk/TcType.hs
- compiler/GHC/Tc/Zonk/Type.hs
- compiler/GHC/Types/Arity.hs
- compiler/GHC/Types/Basic.hs
- compiler/GHC/Types/Cpr.hs
- compiler/GHC/Types/Demand.hs
- compiler/GHC/Types/Id/Info.hs
- compiler/GHC/Types/Name/Env.hs
- compiler/GHC/Types/Name/Occurrence.hs
- compiler/GHC/Types/Unique/DSM.hs
- compiler/GHC/Types/Unique/FM.hs
- compiler/GHC/Types/Unique/Map.hs
- compiler/GHC/Types/Unique/Set.hs
- compiler/GHC/Types/Unique/Supply.hs
- compiler/GHC/Types/Var.hs
- compiler/GHC/Types/Var.hs-boot
- compiler/GHC/Types/Var/Set.hs
- compiler/GHC/Utils/Binary.hs
- + compiler/GHC/Utils/Monad/StrictIdentity.hs
- + compiler/GHC/Utils/Monad/StrictReader.hs
- compiler/GHC/Utils/Monad/State/Strict.hs → compiler/GHC/Utils/Monad/StrictState.hs
- compiler/GHC/Utils/Outputable.hs
- compiler/Language/Haskell/Syntax/Specificity.hs
- compiler/ghc.cabal.in
- docs/users_guide/exts/literals.rst
- testsuite/driver/runtests.py
- testsuite/tests/count-deps/CountDepsParser.stdout
- + testsuite/tests/hpc/instmeths/instmeths.hs
- + testsuite/tests/hpc/instmeths/instmeths.stdout
- + testsuite/tests/hpc/instmeths/test.T
- + testsuite/tests/indexed-types/should_compile/T27828.hs
- + testsuite/tests/indexed-types/should_compile/T27828a.hs
- + testsuite/tests/indexed-types/should_compile/T27828b.hs
- testsuite/tests/indexed-types/should_compile/all.T
- testsuite/tests/indexed-types/should_fail/T9662.stderr
- testsuite/tests/simplCore/should_compile/T22375.stderr
- testsuite/tests/simplCore/should_compile/T22375DataFamily.stderr
- + testsuite/tests/simplCore/should_compile/T27910.hs
- + testsuite/tests/simplCore/should_compile/T27910.stderr
- testsuite/tests/simplCore/should_compile/all.T
- testsuite/tests/typecheck/should_fail/T12785b.stderr
- + testsuite/tests/typecheck/should_fail/T27844.hs
- + testsuite/tests/typecheck/should_fail/T27844.stderr
- testsuite/tests/typecheck/should_fail/all.T
- utils/check-exact/Utils.hs
- utils/haddock/haddock-api/src/Haddock/Backends/Hyperlinker/Utils.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/336b87e20e8df777c38eab5361ffc4…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/336b87e20e8df777c38eab5361ffc4…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/fendor/27836] 11 commits: Remove the Eq and Ord instances for ForAllTyFlag
by Hannes Siebenhandl (@fendor) 14 Oct '26
by Hannes Siebenhandl (@fendor) 14 Oct '26
14 Oct '26
Hannes Siebenhandl pushed to branch wip/fendor/27836 at Glasgow Haskell Compiler / GHC
Commits:
d07a5176 by Simon Peyton Jones at 2026-10-13T18:10:39-04:00
Remove the Eq and Ord instances for ForAllTyFlag
In Note [Comparing visibility] in GHC.Core.TyCo.Compare we read that
"we always compare ForAllTyFlags with `eqForAllVis` or `cmpForAllVis`"
But alas there was an Eq and Ord instance for ForAllTyFlag, which made it
impossible to check this claim. So I made these changes:
* Move the definition of ForAllTyFlag and its related functions from
L.H.S.Specificity (where it did not belong, since L.H.S didn't use it)
into GHC.Types.Var.
* Remove the Eq and Ord instances for ForAllTyFlag
* Deal with the fallout, by using `eqForAllVis` or related functions.
* In interface files we use IfaceType as a key for a de-deduplication map
See Note [Deduplication during iface binary serialisation]
in GHC.Iface.Binary. For this we need /exact/ comparison, so we use a
newtype wrapper for IfaceForAllTyFlag, and give use `cmpForAllTyFlagExactly`
to implement its Ord instance. Tiresome but easy.
* Rename `toIfaceForAllBndrs` to `toIfaceForAllTyBndrs`
- - - - -
ea5c054d by Simon Peyton Jones at 2026-10-13T18:10:39-04:00
Refactor type and coercion substitution
This MR refactors type and coercion substitution so that they use
TyCoMapper, rather than hand-rolled code. Fundamentally it's a simple
idea, but I had to do quite a bit of refactoring to make it fly.
I got Claude to help me this this refactoring, but I paid careful
attention to every line of code.
Main changes:
* New module GHC.Core.TyCo.Make which defines TyCoMapper, and a bunch of other
simple "smart constructors" for types and coercions. See
GHC.Core.TyCo.Rep Note [The Type-related module hierarchy], esp (TRM1)
* The new TyCoMapper has new fields `tcm_tcapp_ty` and `tcm_tcapp_co` to handle
type-constructor applications. These replace `tcm_tycon`.
* Rewrite GHC.Core.TyCo.Subst to use TyCoMapper
* Rewrite GHC.Core.Type.expandTypeSynonyms to use TyCoMapper.
* Implement `seqType` and `seqCo` with TyCoFolder. To to this I had to add
a `tcf_lit` field to TyCoFolder.
* A new module GHC.Utils.StrictIdentity to define a strict identity monad,
which itself is used for
substTyCoMapper :: TyCoMapper Subst StrictIdentity
* I removed an apparently-unused instance for Binary Levity
Refactoring `seq` functions
* In GHC.Core.Seq, redefine `seqOneShot`, `seqRuleInfo`, and friends to return
`SeqResult`, a new type defined in GHC.Types.Baseic, rather than `()`. We
can define a Monoid instance for `SeqResult` that does the right thing.
* This has knock-on effects across the compiler.
- - - - -
34165e23 by Simon Peyton Jones at 2026-10-13T18:10:39-04:00
Refactor TyCoMapper to take a single monad; add StrictReaderT
TyCoMapper used to be parameterised over both an environment and a
monad, with fields like
tcm_tyvar :: env -> TyVar -> m Type
Now it is parameterised over just the monad:
tcm_tyvar :: TyVar -> m Type
If the mapping needs an environment, `m` is a reader monad over that
environment. Many uses (e.g. zonkTcTypeMapper, swizzleMapper) need no
environment at all, and used to pass a dummy (). The binder field
becomes
tcm_tycobinder :: forall r. TyCoVar -> ForAllTyFlag
-> (TyCoVar -> m r) -> m r
and runs the continuation in the extended environment. mapTyCoX is gone;
only mapTyCo remains.
To make this efficient, the reader monad must use the one-shot trick and
be strict in the environment. So:
* New module GHC.Utils.Monad.StrictReader defines
newtype StrictReaderT r m a
with hand-written instances. The builder of the pattern synonym
`StrictReaderT` is a smart constructor that adds both `oneShot` and a
bang on the environment, so every StrictReaderT computation is strict
in the environment. See Note [Instances for StrictReaderT].
* ZonkT becomes type ZonkT = StrictReaderT ZonkEnv
ZonkM becomes type ZonkM = StrictReaderT ZonkGblEnv IO
replacing their own hand-written instances.
* Substitution uses
type SubstM = StrictReaderT Subst StrictIdentity
substTyCoMapper :: TyCoMapper SubstM
and similarly expandTypeSynonymMapper.
* zonk_tycomapper :: TyCoMapper (ZonkT TcM) is now much simpler.
* swizzleMapper uses StrictIdentity rather than Identity.
* Module renaming, so that the strict monads are all peers in GHC.Utils.Monad:
GHC.Utils.StrictIdentity ==> GHC.Utils.Monad.StrictIdentity
GHC.Utils.Monad.State.Strict ==> GHC.Utils.Monad.StrictState
alongside the new GHC.Utils.Monad.StrictReader.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
c0299e15 by Simon Jakobi at 2026-10-13T18:11:23-04:00
testsuite: Line-buffer the driver's stdout
Python block-buffers stdout when it is a pipe. This is the case for
example in CI. As a result there is often no indication of progress at
all until the run ends.
To fix this, we configure it explicitly to use line buffering instead.
stderr is reconfigured too, for consistency: Python line-buffers it by
default since 3.9.
The type-ignores are for mypy, which types these handles as TextIO — an
interface without reconfigure.
Fixes #12934.
Assisted-by: Claude Fable 5
- - - - -
9b5beaa2 by Florian Ragwitz at 2026-10-13T18:12:05-04:00
hpc: Improve coverage semantics for instance methods and instances
Users care about which of their explicitly written methods are or aren't
covered, not whether all of the inherited methods are covered as well. Those
should be covered where they are implemented.
We therefore skip generating top-level boxes for inherited methods which will
appear as uncovered with no clear indication as to why, and no way to fix other
than exercising code the user has not written.
Previously, top-level boxes for those inherited methods would use the instance
head as their source span. Now that those boxes are gone, we can use that
source span for another purpose:
We introduce a new top-level box for each class instance with its instance head
as the source span, and use that to indicate whether or not an instance has
been covered at all.
We do this by making any method of the instance, inherited/generated or
otherwise, tick the box for the instance head. Excluded from this are
methodless classes, which could never be covered using the current mechanism.
This could be improved upon further.
These changes apply to ticking for program coverage only, and don't affect
source notes. Additionally, we update the scope of record selector ticking to
be limited to program coverage as well, as the oversight to also extend it to
source notes has previously caused bugs and because record selector ticking is
coverage-specific.
Fixes #17155, #15932.
- - - - -
836ec50a by Simon Peyton Jones at 2026-10-13T18:12:54-04:00
Refactor fundeps again
This MR fixes #27828; there was a bad scoping error in `trim_qtvs`.
The main changes are
* Refactor `trim_qtvs` completely to eliminate the bug.
* Remove a redundant `apartnessCheck` from `mkTopOpenFamEqFDs`.
* Lots of improvements to the Notes in FunDeps.hs
I also did a couple of pure refactors:
* I realised that we were carefully caching `cab_incomps` in each
`CoAxBranch`, to make it more efficient to reduce a type family application,
but we were not doing the same thing when computing relevance for functional
dependencies; see Note [Relevance] in GHC.Tc.Solver.FunDeps. So I added
`cab_overlaps`, which is now used in `noEarlierMatch`.
* Use a proper data types, `AmIUnifying` and `OuterKindsFlag`, for two
previously-Boolean arguments to `tc_unify_tys`. This is a pure refactor.
- - - - -
889ef371 by Simon Peyton Jones at 2026-10-13T18:13:36-04:00
Refactor and simplify solver's unification code
Ticket #27844 showed that GHC's constraint solver could miss an
occurs check, and thereby build an infinite type.
This patch significantly simplifes the code in
GHC.Tc.Solver.Equality.canEqCanLHSFinish_try_unification
GHC.Tc.Solver.Equality.canEqCanLHSFinish_no_unification
It is now significantly simpler, and requires much less subtle
reasoning to understand.
Main changes
* If `canEqCanLHSFinish_try_unification` fails to unify, it now /always/ calls
`canEqCanLHSFinish_no_unification` rather than trying to finish the job itself.
* `GHC.Tc.Solver.Monad.checkTypeEq` is moved from Monad into Equality, and renamed
to `GHC.Tc.Solver.Equality.checkTypeEqNoUnification`.
* `canEqCanLHSFinish_no_unification` calls `checkTypeEqNoUnification` to test
the invariants for an inert equality. Before this MR there was a special case for
`do_not_prevent_rewriting`, but that has gone; instead it's just a consequence of
the flags passed to `checkTyEqRhs`, documented in the `PuOK` case of
`canEqCanLHSFinish_no_unification`.
* There is only one bit of "swap back" code, in `finish_no_unify` in
`canEqCanLHSFinish_try_unification. Previously this code was duplicated.
* `kickOutAfterFillingCoercionHole` should account for equalities with a
TyFamLHS. See (KOC5) in Note [Kick out after filling a coercion hole]
* No need to gather coercion holes in the `PuOK` case of `canEqCanLHSFinish_no_unification`.
See documentation with that code.
- - - - -
f9193e53 by Simon Peyton Jones at 2026-10-13T18:13:37-04:00
One line fix to fallout from
...#27731, !16564, simplifying Tc.Errors machinery
There was an over-eager assert!
- - - - -
cdd56b7a by Florian Ragwitz at 2026-10-13T18:14:19-04:00
Fix several sphinx errors and warnings from rendering the release notes
The extension references which are removed here are for extension which were
already previously undocumented, so just removing them seems fine for now.
- - - - -
4f00da25 by Florian Ragwitz at 2026-10-13T18:14:19-04:00
Link to QualifiedStrings documentation
- - - - -
b0621399 by Hannes Siebenhandl at 2026-10-14T09:23:53+02:00
Fix #27836, normalise unit database path in ExternalUnitDatabases
Add a regression test, proofing that all variations:
- local-unit-db/my-test.conf
- ./local-unit-db/my-test.conf
- ./local-unit-db/my-test.conf/
are parsed only once.
- - - - -
137 changed files:
- changelog.d/26616
- changelog.d/T27329
- + changelog.d/T27828
- + changelog.d/T27844
- changelog.d/boxing-family
- + changelog.d/hpc-classinsts
- changelog.d/tcplugin_init.md
- changelog.d/unit-index
- compiler/GHC/Builtin/WiredIn/Prim.hs
- compiler/GHC/Builtin/WiredIn/Types/Box.hs
- compiler/GHC/Cmm/GenericOpt.hs
- compiler/GHC/Cmm/UniqueRenamer.hs
- compiler/GHC/CmmToAsm/Dwarf/Types.hs
- compiler/GHC/CmmToAsm/Monad.hs
- compiler/GHC/CmmToAsm/Reg/Graph/Spill.hs
- compiler/GHC/CmmToAsm/Reg/Graph/SpillClean.hs
- compiler/GHC/CmmToAsm/Reg/Graph/SpillCost.hs
- compiler/GHC/CmmToAsm/Reg/Graph/Stats.hs
- compiler/GHC/CmmToAsm/Reg/Linear/State.hs
- compiler/GHC/CmmToAsm/Reg/Linear/Stats.hs
- compiler/GHC/CmmToAsm/Reg/Liveness.hs
- compiler/GHC/CmmToAsm/Wasm/Asm.hs
- compiler/GHC/CmmToAsm/Wasm/Types.hs
- compiler/GHC/CmmToC.hs
- compiler/GHC/Core/Coercion.hs
- compiler/GHC/Core/Coercion.hs-boot
- compiler/GHC/Core/Coercion/Axiom.hs
- compiler/GHC/Core/Coercion/Opt.hs
- compiler/GHC/Core/DataCon.hs
- compiler/GHC/Core/FamInstEnv.hs
- compiler/GHC/Core/Lint.hs
- compiler/GHC/Core/Map/Type.hs
- compiler/GHC/Core/Opt/Arity.hs
- compiler/GHC/Core/Opt/CallerCC.hs
- compiler/GHC/Core/Opt/Exitify.hs
- compiler/GHC/Core/Opt/Specialise.hs
- compiler/GHC/Core/Reduction.hs
- compiler/GHC/Core/Seq.hs
- compiler/GHC/Core/TyCo/Compare.hs
- compiler/GHC/Core/TyCo/FVs.hs
- + compiler/GHC/Core/TyCo/Make.hs
- + compiler/GHC/Core/TyCo/Make.hs-boot
- compiler/GHC/Core/TyCo/Ppr.hs
- compiler/GHC/Core/TyCo/Rep.hs
- compiler/GHC/Core/TyCo/Rep.hs-boot
- compiler/GHC/Core/TyCo/Subst.hs
- compiler/GHC/Core/Type.hs
- compiler/GHC/Core/Type.hs-boot
- compiler/GHC/Core/Unify.hs
- compiler/GHC/CoreToIface.hs
- compiler/GHC/CoreToIface.hs-boot
- compiler/GHC/Data/OsPath.hs
- compiler/GHC/Driver/Downsweep.hs
- compiler/GHC/Driver/Main/Compile.hs
- compiler/GHC/HsToCore.hs
- compiler/GHC/HsToCore/Ticks.hs
- compiler/GHC/Iface/Decl.hs
- compiler/GHC/Iface/Ext/Types.hs
- compiler/GHC/Iface/Ext/Utils.hs
- compiler/GHC/Iface/Syntax.hs
- compiler/GHC/Iface/Type.hs
- compiler/GHC/Iface/Type.hs-boot
- compiler/GHC/IfaceToCore.hs
- compiler/GHC/Runtime/Heap/Inspect.hs
- compiler/GHC/Stg/BcPrep.hs
- compiler/GHC/Stg/Debug.hs
- compiler/GHC/Stg/EnforceEpt/Rewrite.hs
- compiler/GHC/Stg/EnforceEpt/TagSig.hs
- compiler/GHC/Stg/Subst.hs
- compiler/GHC/Tc/Deriv/Functor.hs
- compiler/GHC/Tc/Errors.hs
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Gen/App.hs
- compiler/GHC/Tc/Instance/FunDeps.hs
- compiler/GHC/Tc/Solver/Equality.hs
- compiler/GHC/Tc/Solver/FunDeps.hs
- compiler/GHC/Tc/Solver/InertSet.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/TyCl.hs
- compiler/GHC/Tc/TyCl/Instance.hs
- compiler/GHC/Tc/Types/Constraint.hs
- compiler/GHC/Tc/Utils/Unify.hs
- compiler/GHC/Tc/Zonk/Env.hs
- compiler/GHC/Tc/Zonk/Monad.hs
- compiler/GHC/Tc/Zonk/TcType.hs
- compiler/GHC/Tc/Zonk/Type.hs
- compiler/GHC/Types/Arity.hs
- compiler/GHC/Types/Basic.hs
- compiler/GHC/Types/Cpr.hs
- compiler/GHC/Types/Demand.hs
- compiler/GHC/Types/Id/Info.hs
- compiler/GHC/Types/Name/Env.hs
- compiler/GHC/Types/Name/Occurrence.hs
- compiler/GHC/Types/Unique/DSM.hs
- compiler/GHC/Types/Unique/FM.hs
- compiler/GHC/Types/Unique/Map.hs
- compiler/GHC/Types/Unique/Set.hs
- compiler/GHC/Types/Unique/Supply.hs
- compiler/GHC/Types/Var.hs
- compiler/GHC/Types/Var.hs-boot
- compiler/GHC/Types/Var/Set.hs
- compiler/GHC/Unit/External/Database.hs
- compiler/GHC/Utils/Binary.hs
- + compiler/GHC/Utils/Monad/StrictIdentity.hs
- + compiler/GHC/Utils/Monad/StrictReader.hs
- compiler/GHC/Utils/Monad/State/Strict.hs → compiler/GHC/Utils/Monad/StrictState.hs
- compiler/GHC/Utils/Outputable.hs
- compiler/Language/Haskell/Syntax/Specificity.hs
- compiler/ghc.cabal.in
- docs/users_guide/exts/literals.rst
- testsuite/driver/runtests.py
- testsuite/tests/count-deps/CountDepsParser.stdout
- + testsuite/tests/driver/T27836/A.hs
- + testsuite/tests/driver/T27836/B.hs
- + testsuite/tests/driver/T27836/C.hs
- + testsuite/tests/driver/T27836/Makefile
- + testsuite/tests/driver/T27836/T27836.stdout
- + testsuite/tests/driver/T27836/all.T
- + testsuite/tests/driver/T27836/unitA
- + testsuite/tests/driver/T27836/unitB
- + testsuite/tests/driver/T27836/unitC
- + testsuite/tests/hpc/instmeths/instmeths.hs
- + testsuite/tests/hpc/instmeths/instmeths.stdout
- + testsuite/tests/hpc/instmeths/test.T
- + testsuite/tests/indexed-types/should_compile/T27828.hs
- + testsuite/tests/indexed-types/should_compile/T27828a.hs
- + testsuite/tests/indexed-types/should_compile/T27828b.hs
- testsuite/tests/indexed-types/should_compile/all.T
- testsuite/tests/indexed-types/should_fail/T9662.stderr
- testsuite/tests/simplCore/should_compile/T22375.stderr
- testsuite/tests/simplCore/should_compile/T22375DataFamily.stderr
- testsuite/tests/typecheck/should_fail/T12785b.stderr
- + testsuite/tests/typecheck/should_fail/T27844.hs
- + testsuite/tests/typecheck/should_fail/T27844.stderr
- testsuite/tests/typecheck/should_fail/all.T
- utils/check-exact/Utils.hs
- utils/haddock/haddock-api/src/Haddock/Backends/Hyperlinker/Utils.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/602eda7673783862b9da5f8ab0df2c…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/602eda7673783862b9da5f8ab0df2c…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/marge_bot_batch_merge_job] 14 commits: Remove the Eq and Ord instances for ForAllTyFlag
by Marge Bot (@marge-bot) 14 Oct '26
by Marge Bot (@marge-bot) 14 Oct '26
14 Oct '26
Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC
Commits:
d07a5176 by Simon Peyton Jones at 2026-10-13T18:10:39-04:00
Remove the Eq and Ord instances for ForAllTyFlag
In Note [Comparing visibility] in GHC.Core.TyCo.Compare we read that
"we always compare ForAllTyFlags with `eqForAllVis` or `cmpForAllVis`"
But alas there was an Eq and Ord instance for ForAllTyFlag, which made it
impossible to check this claim. So I made these changes:
* Move the definition of ForAllTyFlag and its related functions from
L.H.S.Specificity (where it did not belong, since L.H.S didn't use it)
into GHC.Types.Var.
* Remove the Eq and Ord instances for ForAllTyFlag
* Deal with the fallout, by using `eqForAllVis` or related functions.
* In interface files we use IfaceType as a key for a de-deduplication map
See Note [Deduplication during iface binary serialisation]
in GHC.Iface.Binary. For this we need /exact/ comparison, so we use a
newtype wrapper for IfaceForAllTyFlag, and give use `cmpForAllTyFlagExactly`
to implement its Ord instance. Tiresome but easy.
* Rename `toIfaceForAllBndrs` to `toIfaceForAllTyBndrs`
- - - - -
ea5c054d by Simon Peyton Jones at 2026-10-13T18:10:39-04:00
Refactor type and coercion substitution
This MR refactors type and coercion substitution so that they use
TyCoMapper, rather than hand-rolled code. Fundamentally it's a simple
idea, but I had to do quite a bit of refactoring to make it fly.
I got Claude to help me this this refactoring, but I paid careful
attention to every line of code.
Main changes:
* New module GHC.Core.TyCo.Make which defines TyCoMapper, and a bunch of other
simple "smart constructors" for types and coercions. See
GHC.Core.TyCo.Rep Note [The Type-related module hierarchy], esp (TRM1)
* The new TyCoMapper has new fields `tcm_tcapp_ty` and `tcm_tcapp_co` to handle
type-constructor applications. These replace `tcm_tycon`.
* Rewrite GHC.Core.TyCo.Subst to use TyCoMapper
* Rewrite GHC.Core.Type.expandTypeSynonyms to use TyCoMapper.
* Implement `seqType` and `seqCo` with TyCoFolder. To to this I had to add
a `tcf_lit` field to TyCoFolder.
* A new module GHC.Utils.StrictIdentity to define a strict identity monad,
which itself is used for
substTyCoMapper :: TyCoMapper Subst StrictIdentity
* I removed an apparently-unused instance for Binary Levity
Refactoring `seq` functions
* In GHC.Core.Seq, redefine `seqOneShot`, `seqRuleInfo`, and friends to return
`SeqResult`, a new type defined in GHC.Types.Baseic, rather than `()`. We
can define a Monoid instance for `SeqResult` that does the right thing.
* This has knock-on effects across the compiler.
- - - - -
34165e23 by Simon Peyton Jones at 2026-10-13T18:10:39-04:00
Refactor TyCoMapper to take a single monad; add StrictReaderT
TyCoMapper used to be parameterised over both an environment and a
monad, with fields like
tcm_tyvar :: env -> TyVar -> m Type
Now it is parameterised over just the monad:
tcm_tyvar :: TyVar -> m Type
If the mapping needs an environment, `m` is a reader monad over that
environment. Many uses (e.g. zonkTcTypeMapper, swizzleMapper) need no
environment at all, and used to pass a dummy (). The binder field
becomes
tcm_tycobinder :: forall r. TyCoVar -> ForAllTyFlag
-> (TyCoVar -> m r) -> m r
and runs the continuation in the extended environment. mapTyCoX is gone;
only mapTyCo remains.
To make this efficient, the reader monad must use the one-shot trick and
be strict in the environment. So:
* New module GHC.Utils.Monad.StrictReader defines
newtype StrictReaderT r m a
with hand-written instances. The builder of the pattern synonym
`StrictReaderT` is a smart constructor that adds both `oneShot` and a
bang on the environment, so every StrictReaderT computation is strict
in the environment. See Note [Instances for StrictReaderT].
* ZonkT becomes type ZonkT = StrictReaderT ZonkEnv
ZonkM becomes type ZonkM = StrictReaderT ZonkGblEnv IO
replacing their own hand-written instances.
* Substitution uses
type SubstM = StrictReaderT Subst StrictIdentity
substTyCoMapper :: TyCoMapper SubstM
and similarly expandTypeSynonymMapper.
* zonk_tycomapper :: TyCoMapper (ZonkT TcM) is now much simpler.
* swizzleMapper uses StrictIdentity rather than Identity.
* Module renaming, so that the strict monads are all peers in GHC.Utils.Monad:
GHC.Utils.StrictIdentity ==> GHC.Utils.Monad.StrictIdentity
GHC.Utils.Monad.State.Strict ==> GHC.Utils.Monad.StrictState
alongside the new GHC.Utils.Monad.StrictReader.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
c0299e15 by Simon Jakobi at 2026-10-13T18:11:23-04:00
testsuite: Line-buffer the driver's stdout
Python block-buffers stdout when it is a pipe. This is the case for
example in CI. As a result there is often no indication of progress at
all until the run ends.
To fix this, we configure it explicitly to use line buffering instead.
stderr is reconfigured too, for consistency: Python line-buffers it by
default since 3.9.
The type-ignores are for mypy, which types these handles as TextIO — an
interface without reconfigure.
Fixes #12934.
Assisted-by: Claude Fable 5
- - - - -
9b5beaa2 by Florian Ragwitz at 2026-10-13T18:12:05-04:00
hpc: Improve coverage semantics for instance methods and instances
Users care about which of their explicitly written methods are or aren't
covered, not whether all of the inherited methods are covered as well. Those
should be covered where they are implemented.
We therefore skip generating top-level boxes for inherited methods which will
appear as uncovered with no clear indication as to why, and no way to fix other
than exercising code the user has not written.
Previously, top-level boxes for those inherited methods would use the instance
head as their source span. Now that those boxes are gone, we can use that
source span for another purpose:
We introduce a new top-level box for each class instance with its instance head
as the source span, and use that to indicate whether or not an instance has
been covered at all.
We do this by making any method of the instance, inherited/generated or
otherwise, tick the box for the instance head. Excluded from this are
methodless classes, which could never be covered using the current mechanism.
This could be improved upon further.
These changes apply to ticking for program coverage only, and don't affect
source notes. Additionally, we update the scope of record selector ticking to
be limited to program coverage as well, as the oversight to also extend it to
source notes has previously caused bugs and because record selector ticking is
coverage-specific.
Fixes #17155, #15932.
- - - - -
836ec50a by Simon Peyton Jones at 2026-10-13T18:12:54-04:00
Refactor fundeps again
This MR fixes #27828; there was a bad scoping error in `trim_qtvs`.
The main changes are
* Refactor `trim_qtvs` completely to eliminate the bug.
* Remove a redundant `apartnessCheck` from `mkTopOpenFamEqFDs`.
* Lots of improvements to the Notes in FunDeps.hs
I also did a couple of pure refactors:
* I realised that we were carefully caching `cab_incomps` in each
`CoAxBranch`, to make it more efficient to reduce a type family application,
but we were not doing the same thing when computing relevance for functional
dependencies; see Note [Relevance] in GHC.Tc.Solver.FunDeps. So I added
`cab_overlaps`, which is now used in `noEarlierMatch`.
* Use a proper data types, `AmIUnifying` and `OuterKindsFlag`, for two
previously-Boolean arguments to `tc_unify_tys`. This is a pure refactor.
- - - - -
889ef371 by Simon Peyton Jones at 2026-10-13T18:13:36-04:00
Refactor and simplify solver's unification code
Ticket #27844 showed that GHC's constraint solver could miss an
occurs check, and thereby build an infinite type.
This patch significantly simplifes the code in
GHC.Tc.Solver.Equality.canEqCanLHSFinish_try_unification
GHC.Tc.Solver.Equality.canEqCanLHSFinish_no_unification
It is now significantly simpler, and requires much less subtle
reasoning to understand.
Main changes
* If `canEqCanLHSFinish_try_unification` fails to unify, it now /always/ calls
`canEqCanLHSFinish_no_unification` rather than trying to finish the job itself.
* `GHC.Tc.Solver.Monad.checkTypeEq` is moved from Monad into Equality, and renamed
to `GHC.Tc.Solver.Equality.checkTypeEqNoUnification`.
* `canEqCanLHSFinish_no_unification` calls `checkTypeEqNoUnification` to test
the invariants for an inert equality. Before this MR there was a special case for
`do_not_prevent_rewriting`, but that has gone; instead it's just a consequence of
the flags passed to `checkTyEqRhs`, documented in the `PuOK` case of
`canEqCanLHSFinish_no_unification`.
* There is only one bit of "swap back" code, in `finish_no_unify` in
`canEqCanLHSFinish_try_unification. Previously this code was duplicated.
* `kickOutAfterFillingCoercionHole` should account for equalities with a
TyFamLHS. See (KOC5) in Note [Kick out after filling a coercion hole]
* No need to gather coercion holes in the `PuOK` case of `canEqCanLHSFinish_no_unification`.
See documentation with that code.
- - - - -
f9193e53 by Simon Peyton Jones at 2026-10-13T18:13:37-04:00
One line fix to fallout from
...#27731, !16564, simplifying Tc.Errors machinery
There was an over-eager assert!
- - - - -
cdd56b7a by Florian Ragwitz at 2026-10-13T18:14:19-04:00
Fix several sphinx errors and warnings from rendering the release notes
The extension references which are removed here are for extension which were
already previously undocumented, so just removing them seems fine for now.
- - - - -
4f00da25 by Florian Ragwitz at 2026-10-13T18:14:19-04:00
Link to QualifiedStrings documentation
- - - - -
97adaf89 by Simon Peyton Jones at 2026-10-14T00:07:27+01:00
Line up simpleUnifyCheck and check_ty_eq_rhs
Even after #26543 was allegedly fixed, the original repo case in the
Description continued to fail. The reason was that the QuickLook
unifier, `qlUnify`, used `simpleUnifyCheck` for checking unification
invariants; and `simpleUnifyCheck` conservatively rejected any RHS
with a coercion hole in it. The upshot was that QuickLook was not
as clever as it should be, wrongly failing to do an impredicative
instantiation.
Interestingly `check_ty_eq_rhs`, which does the same job during
constraint solving, was more liberal: it just looked at the free vars
of the coercion, and allowed coercion holes.
This MR lines them up, adding some careful notes. See
Note [simpleUnifyCheck]
Note [Occurs check and coercions]
Note [Unification preconditions] esp (COERCIONS)
Things are better than before, but I am still uncomfortable about the
possibilty that a hole might be filled with a coercion that mentions
the LHS type variable; for now I have left this discomfort documented
in Note [Occurs check and coercions].
Main changes
* `qlUnify` now only unifies /instantiation variables/ not regular unification
variables. This makes QuickLook more predictable. See (UQL6) in
Note [QuickLook unification].
* `qlUnify` no longer needs to call `simpleUnifyCheck`, so we can get rid of
`UC_QuickLook` and its code in `simpleUnifyCheck`.
* I found I could get rid of `mkOccFolders` in favour of `anyFreeVarsOfType`.
Hooray.
* Improve documentation of CycleBreakerTv, and rename it to GivenCycleBreakerTv
because they are only uesd for Givens!
* I accepted a 9% worsening in compile time allocation on T5030
because the newly-consistent story triggered #27934.
Other changes
* I refactored `atvFolder` so that it pairs the "interesting-var" function
with the in-scope set, to avoid building a closure with a free variable.
Now we get nice tight loops for `anyFreeVarsOfType` and friends.
See Note [Any-free-var folder].
Metric Increase:
T5030
- - - - -
5f7b3047 by Simon Peyton Jones at 2026-10-14T00:07:27+01:00
Fix order-dependence in QuickLook
Ticket #27917 pointed out that QuickLook was order-dependent
-- very bad!
The solution is in `qlUnify`: spot when we are unifying a type with a
definite monotype, and then monomorphise any free instantiation
variables of that type.
See (UQL8) and (UQL9) in Note [QuickLook unification].
- - - - -
c391c70c by Simon Peyton Jones at 2026-10-14T00:07:28+01:00
Add some comments about RuntimeUnk
This commit just adds comments explaining why we don't need to worry about
RuntimeUnkTv in the constraint solver.
It also tightens up docmentation of earlier (overlapping) commits in
this MR.
- - - - -
079b1aa9 by Andrea Vezzosi at 2026-10-13T23:24:46-04:00
add runGhcWithSignalHandlers and remove handling from runGhc[T]
it only ever makes sense to call `withSignalHandlers` from `main`, otherwise
signals can be too easily swallowed without notification to the rest of your
process.
This commit takes withSignalHandlers out of `runGhc/runGhcT` and provides
`runGhcWithSignalHandlers` for the old behaviour.
- - - - -
156 changed files:
- changelog.d/26616
- + changelog.d/T26543
- changelog.d/T27329
- + changelog.d/T27828
- + changelog.d/T27844
- + changelog.d/T4162
- changelog.d/boxing-family
- + changelog.d/hpc-classinsts
- changelog.d/tcplugin_init.md
- changelog.d/unit-index
- compiler/GHC.hs
- compiler/GHC/Builtin/WiredIn/Prim.hs
- compiler/GHC/Builtin/WiredIn/Types/Box.hs
- compiler/GHC/Cmm/GenericOpt.hs
- compiler/GHC/Cmm/UniqueRenamer.hs
- compiler/GHC/CmmToAsm/Dwarf/Types.hs
- compiler/GHC/CmmToAsm/Monad.hs
- compiler/GHC/CmmToAsm/Reg/Graph/Spill.hs
- compiler/GHC/CmmToAsm/Reg/Graph/SpillClean.hs
- compiler/GHC/CmmToAsm/Reg/Graph/SpillCost.hs
- compiler/GHC/CmmToAsm/Reg/Graph/Stats.hs
- compiler/GHC/CmmToAsm/Reg/Linear/State.hs
- compiler/GHC/CmmToAsm/Reg/Linear/Stats.hs
- compiler/GHC/CmmToAsm/Reg/Liveness.hs
- compiler/GHC/CmmToAsm/Wasm/Asm.hs
- compiler/GHC/CmmToAsm/Wasm/Types.hs
- compiler/GHC/CmmToC.hs
- compiler/GHC/Core/Coercion.hs
- compiler/GHC/Core/Coercion.hs-boot
- compiler/GHC/Core/Coercion/Axiom.hs
- compiler/GHC/Core/Coercion/Opt.hs
- compiler/GHC/Core/DataCon.hs
- compiler/GHC/Core/FVs.hs
- compiler/GHC/Core/FamInstEnv.hs
- compiler/GHC/Core/Lint.hs
- compiler/GHC/Core/Map/Type.hs
- compiler/GHC/Core/Opt/Arity.hs
- compiler/GHC/Core/Opt/CallerCC.hs
- compiler/GHC/Core/Opt/Exitify.hs
- compiler/GHC/Core/Opt/Specialise.hs
- compiler/GHC/Core/Reduction.hs
- compiler/GHC/Core/Seq.hs
- compiler/GHC/Core/TyCo/Compare.hs
- compiler/GHC/Core/TyCo/FVs.hs
- + compiler/GHC/Core/TyCo/Make.hs
- + compiler/GHC/Core/TyCo/Make.hs-boot
- compiler/GHC/Core/TyCo/Ppr.hs
- compiler/GHC/Core/TyCo/Rep.hs
- compiler/GHC/Core/TyCo/Rep.hs-boot
- compiler/GHC/Core/TyCo/Subst.hs
- compiler/GHC/Core/Type.hs
- compiler/GHC/Core/Type.hs-boot
- compiler/GHC/Core/Unify.hs
- compiler/GHC/CoreToIface.hs
- compiler/GHC/CoreToIface.hs-boot
- compiler/GHC/Driver/Downsweep.hs
- compiler/GHC/Driver/Main/Compile.hs
- compiler/GHC/HsToCore.hs
- compiler/GHC/HsToCore/Ticks.hs
- compiler/GHC/Iface/Decl.hs
- compiler/GHC/Iface/Ext/Types.hs
- compiler/GHC/Iface/Ext/Utils.hs
- compiler/GHC/Iface/Syntax.hs
- compiler/GHC/Iface/Type.hs
- compiler/GHC/Iface/Type.hs-boot
- compiler/GHC/IfaceToCore.hs
- compiler/GHC/Runtime/Heap/Inspect.hs
- compiler/GHC/Stg/BcPrep.hs
- compiler/GHC/Stg/Debug.hs
- compiler/GHC/Stg/EnforceEpt/Rewrite.hs
- compiler/GHC/Stg/EnforceEpt/TagSig.hs
- compiler/GHC/Stg/Subst.hs
- compiler/GHC/Tc/Deriv/Functor.hs
- compiler/GHC/Tc/Errors.hs
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Gen/App.hs
- compiler/GHC/Tc/Instance/FunDeps.hs
- compiler/GHC/Tc/Solver/Equality.hs
- compiler/GHC/Tc/Solver/FunDeps.hs
- compiler/GHC/Tc/Solver/InertSet.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/TyCl.hs
- compiler/GHC/Tc/TyCl/Instance.hs
- compiler/GHC/Tc/Types/Constraint.hs
- compiler/GHC/Tc/Utils/TcMType.hs
- compiler/GHC/Tc/Utils/TcType.hs
- compiler/GHC/Tc/Utils/Unify.hs
- compiler/GHC/Tc/Zonk/Env.hs
- compiler/GHC/Tc/Zonk/Monad.hs
- compiler/GHC/Tc/Zonk/TcType.hs
- compiler/GHC/Tc/Zonk/Type.hs
- compiler/GHC/Types/Arity.hs
- compiler/GHC/Types/Basic.hs
- compiler/GHC/Types/Cpr.hs
- compiler/GHC/Types/Demand.hs
- compiler/GHC/Types/Id/Info.hs
- compiler/GHC/Types/Name/Env.hs
- compiler/GHC/Types/Name/Occurrence.hs
- compiler/GHC/Types/Unique/DSM.hs
- compiler/GHC/Types/Unique/FM.hs
- compiler/GHC/Types/Unique/Map.hs
- compiler/GHC/Types/Unique/Set.hs
- compiler/GHC/Types/Unique/Supply.hs
- compiler/GHC/Types/Var.hs
- compiler/GHC/Types/Var.hs-boot
- compiler/GHC/Types/Var/FV.hs
- compiler/GHC/Types/Var/Set.hs
- compiler/GHC/Utils/Binary.hs
- + compiler/GHC/Utils/Monad/StrictIdentity.hs
- + compiler/GHC/Utils/Monad/StrictReader.hs
- compiler/GHC/Utils/Monad/State/Strict.hs → compiler/GHC/Utils/Monad/StrictState.hs
- compiler/GHC/Utils/Outputable.hs
- compiler/GHC/Utils/Panic.hs
- compiler/Language/Haskell/Syntax/Specificity.hs
- compiler/ghc.cabal.in
- docs/users_guide/exts/literals.rst
- ghc/Main.hs
- linters/lint-codes/LintCodes/Static.hs
- testsuite/driver/runtests.py
- testsuite/tests/count-deps/CountDepsParser.stdout
- testsuite/tests/default/default-fail05.stderr
- + testsuite/tests/ghc-api/T4162.hs
- + testsuite/tests/ghc-api/T4162.stdout
- testsuite/tests/ghc-api/all.T
- testsuite/tests/ghci.debugger/scripts/T14628.stderr
- + testsuite/tests/hpc/instmeths/instmeths.hs
- + testsuite/tests/hpc/instmeths/instmeths.stdout
- + testsuite/tests/hpc/instmeths/test.T
- + testsuite/tests/impredicative/T27917.hs
- testsuite/tests/impredicative/all.T
- + testsuite/tests/indexed-types/should_compile/T27828.hs
- + testsuite/tests/indexed-types/should_compile/T27828a.hs
- + testsuite/tests/indexed-types/should_compile/T27828b.hs
- testsuite/tests/indexed-types/should_compile/all.T
- testsuite/tests/indexed-types/should_fail/T1897b.stderr
- testsuite/tests/indexed-types/should_fail/T9662.stderr
- testsuite/tests/simplCore/should_compile/T22375.stderr
- testsuite/tests/simplCore/should_compile/T22375DataFamily.stderr
- testsuite/tests/typecheck/should_compile/LocalGivenEqs.hs
- + testsuite/tests/typecheck/should_compile/T26543_orig.hs
- testsuite/tests/typecheck/should_compile/all.T
- testsuite/tests/typecheck/should_fail/T12785b.stderr
- testsuite/tests/typecheck/should_fail/T22707.stderr
- + testsuite/tests/typecheck/should_fail/T26543a.hs
- + testsuite/tests/typecheck/should_fail/T26543a.stderr
- + testsuite/tests/typecheck/should_fail/T27844.hs
- + testsuite/tests/typecheck/should_fail/T27844.stderr
- testsuite/tests/typecheck/should_fail/all.T
- utils/check-exact/Parsers.hs
- utils/check-exact/Utils.hs
- utils/check-ppr/Main.hs
- utils/count-deps/Main.hs
- utils/dump-decls/Main.hs
- utils/haddock/haddock-api/src/Haddock.hs
- utils/haddock/haddock-api/src/Haddock/Backends/Hyperlinker/Utils.hs
- utils/haddock/haddock-api/test/Haddock/Backends/Hyperlinker/ParserSpec.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/63c22157e46a8cb74770f0be78e996…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/63c22157e46a8cb74770f0be78e996…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0