Skip to content

Commit

Permalink
Remove orphan Show instance for Foreign.C.Error.Errno
Browse files Browse the repository at this point in the history
This orphan instance largely exists to auto-derive a `Show` instance for
`FsError`. Exposing an orphan instance for a type from `base` is not very nice,
however. As such, we've removed the orphan instance, and we've written the `Show
FsError` instance by hand.

Note that this is a breaking change for both `fs-api` and `fs-sim`, since the
orphan instance is propagated from `fs-api` through `fs-sim`.
  • Loading branch information
jorisdral committed Apr 23, 2024
1 parent d8974b9 commit f4ea482
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions fs-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Breaking

* New `primitive ^>=0.9` dependency
* Remove orphan `Show` instance for `Foreign.C.Error.Errno`.

### Non-breaking

Expand Down
36 changes: 34 additions & 2 deletions fs-api/src/System/FS/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}

-- For Show Errno and Condense SeekMode instances
Expand Down Expand Up @@ -55,6 +56,7 @@ import Foreign.C.Error (Errno (..))
import qualified Foreign.C.Error as C
import GHC.Generics (Generic)
import qualified GHC.IO.Exception as GHC
import GHC.Show (showCommaSpace)
import System.FilePath
import System.IO (SeekMode (..))
import qualified System.IO.Error as IO
Expand Down Expand Up @@ -224,9 +226,39 @@ data FsError = FsError {
-- would not have thrown an error for these calls.
, fsLimitation :: Bool
}
deriving Show

deriving instance Show Errno
-- This is a custom instance and not an auto-derive one, since 'Errno' does not
-- have a 'Show' instance, and we don't want to provide an orphan instance for
-- this @base@ type.
instance Show FsError where
showsPrec n fserr = showParen (n >= 11) $
showString "FsError {"
. showString "fsErrorType = " . shows fsErrorType . showCommaSpace
. showString "fsErrorPath = " . shows fsErrorPath . showCommaSpace
. showString "fsErrorString = " . shows fsErrorString . showCommaSpace
. showString "fsErrorNo = " . showsFsErrNo fsErrorNo . showCommaSpace
. showString "fsErrorStack = " . shows fsErrorStack . showCommaSpace
. showString "fsLimitation = " . shows fsLimitation
. showString "}"
where
-- Quite a bit of boilerplate, but it should ensure that we won't silently
-- change/forget to change the Show instance when fields are
-- changed/re-ordered/added.
FsError {
fsErrorType = fsErrorType :: FsErrorType
, fsErrorPath = fsErrorPath :: FsErrorPath
, fsErrorString = fsErrorString :: String
, fsErrorNo = fsErrorNo :: Maybe Errno
, fsErrorStack = fsErrorStack :: PrettyCallStack
, fsLimitation = fsLimitation :: Bool
} = fserr
_coveredAllCases = case fserr of
FsError (_ :: FsErrorType) (_ :: FsErrorPath) (_ :: String)
(_ :: Maybe Errno) (_ :: PrettyCallStack) (_ :: Bool) -> ()

showsFsErrNo Nothing = showString "Nothing"
showsFsErrNo (Just (Errno e)) = showString "Just "
. showParen True (showString "Errno " . shows e)

data FsErrorType
= FsIllegalOperation
Expand Down
4 changes: 4 additions & 0 deletions fs-sim/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Revision history for fs-sim

## next release -- ????-??-??

* Orphan `Show` instance for `Foreign.C.Error.Errno` removed by `fs-api`.

## 0.2.1.1 -- 2023-10-30

### Patch
Expand Down

0 comments on commit f4ea482

Please sign in to comment.