Skip to main content

HMock Compatibility (Part 2)

I decided to go ahead and try to solve the error that I get when trying to build HMock using GHC 8.6 and 8.8, as described in my previous blog entry.

/tmp/stack-b8726112c6310f6a/HMock-0.5.0.0/src/Test/HMock/Internal/State.hs:202:7: error:
    • Couldn't match type ‘MockT m’ with ‘ReaderT (MockState m) m’
        arising from the coercion of the method ‘unliftio-core-0.1.2.0:Control.Monad.IO.Unlift.askUnliftIO’
          from type ‘ReaderT
                       (MockState m)
                       m
                       (unliftio-core-0.1.2.0:Control.Monad.IO.Unlift.UnliftIO
                          (ReaderT (MockState m) m))’
            to type ‘MockT
                       m
                       (unliftio-core-0.1.2.0:Control.Monad.IO.Unlift.UnliftIO (MockT m))’
    • When deriving the instance for (MonadUnliftIO (MockT m))
    |
202 |       MonadUnliftIO
    |       ^^^^^^^^^^^^^

In the definition of the MockT newtype, an instance for MonadUnliftIO is automatically derived, but the above coercion error only happens when using Stack. I have no idea why this error occurs when using Stack but not when using Cabal, but a quick fix is to implement the instance manually instead of automatically deriving it.

instance MonadUnliftIO m => MonadUnliftIO (MockT m) where
  withRunInIO inner = MockT $ withRunInIO $ \run -> inner (run . unMockT)

With this change, the package now builds with Stack using GHC 8.6 as well as GHC 8.8, and all tests pass. I created a GitHub issue to see if Chris knows how to fix the coercion error and offer to submit a pull request with this fix if not.

GHC 8.6

The Stack configuration for using HMock 0.5.0.0 with GHC 8.6.5 is as follows:

resolver: lts-14.27

packages:
  - .

extra-deps:
  - binary-0.8.6.0
  - constraints-0.13
  - containers-0.6.5.1
  - doctest-exitcode-stdio-0.0
  - doctest-lib-0.1
  - explainable-predicates-0.1.2.0
  - extra-1.7.10
  - HMock-0.5.0.0
  - regex-base-0.94.0.1
  - regex-tdfa-1.3.1.1
  - syb-0.7.2.1
  - text-1.2.3.1
  - type-equality-1
  - unliftio-0.2.20

GHC 8.8

The Stack configuration for using HMock 0.5.0.0 with GHC 8.8.4 is as follows:

resolver: lts-16.31

packages:
  - .

extra-deps:
  - constraints-0.13
  - doctest-exitcode-stdio-0.0
  - doctest-lib-0.1
  - explainable-predicates-0.1.2.0
  - HMock-0.5.0.0
  - syb-0.7.2.1
  - unliftio-0.2.20