Commit f547b444 authored by Ryan Scott's avatar Ryan Scott Committed by Ben Gamari
Browse files

Eliminate some unsafeCoerce#s with deriving strategies

Currently, `Foreign.C.Types`, `Foreign.Ptr`, and `System.Posix.Types`
define `Read` and `Show` instances for the newtypes in those modules by
using `unsafeCoerce#`. We can clean up this hack by using the `newtype`
deriving strategy.

Reviewers: hvr, austin, bgamari

Reviewed By: bgamari

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D2556
parent b0d53a83
{-# LANGUAGE CPP #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, GeneralizedNewtypeDeriving,
StandaloneDeriving #-}
{-# OPTIONS_GHC -Wno-unused-binds #-}
-- XXX -Wno-unused-binds stops us warning about unused constructors,
-- but really we should just remove them if we don't want them
......
{-# LANGUAGE CPP #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, GeneralizedNewtypeDeriving,
StandaloneDeriving #-}
-----------------------------------------------------------------------------
-- |
......
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP
, NoImplicitPrelude
, MagicHash
, GeneralizedNewtypeDeriving
#-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
-----------------------------------------------------------------------------
-- |
......
......@@ -21,34 +21,19 @@
#define FLOATING_CLASSES Fractional,Floating,RealFrac,RealFloat
#define ARITHMETIC_TYPE(T,B) \
newtype T = T B deriving (ARITHMETIC_CLASSES); \
INSTANCE_READ(T,B); \
INSTANCE_SHOW(T,B);
newtype T = T B deriving (ARITHMETIC_CLASSES) \
deriving newtype (Read, Show);
#define INTEGRAL_TYPE(T,B) \
newtype T = T B deriving (ARITHMETIC_CLASSES, INTEGRAL_CLASSES); \
INSTANCE_READ(T,B); \
INSTANCE_SHOW(T,B);
newtype T = T B deriving (ARITHMETIC_CLASSES, INTEGRAL_CLASSES) \
deriving newtype (Read, Show);
#define INTEGRAL_TYPE_WITH_CTYPE(T,THE_CTYPE,B) \
newtype {-# CTYPE "THE_CTYPE" #-} T = T B deriving (ARITHMETIC_CLASSES, INTEGRAL_CLASSES); \
INSTANCE_READ(T,B); \
INSTANCE_SHOW(T,B);
newtype {-# CTYPE "THE_CTYPE" #-} T = T B deriving (ARITHMETIC_CLASSES, INTEGRAL_CLASSES) \
deriving newtype (Read, Show);
#define FLOATING_TYPE(T,B) \
newtype T = T B deriving (ARITHMETIC_CLASSES, FLOATING_CLASSES); \
INSTANCE_READ(T,B); \
INSTANCE_SHOW(T,B);
#define INSTANCE_READ(T,B) \
instance Read T where { \
readsPrec = unsafeCoerce# (readsPrec :: Int -> ReadS B); \
readList = unsafeCoerce# (readList :: ReadS [B]); }
#define INSTANCE_SHOW(T,B) \
instance Show T where { \
showsPrec = unsafeCoerce# (showsPrec :: Int -> B -> ShowS); \
show = unsafeCoerce# (show :: B -> String); \
showList = unsafeCoerce# (showList :: [B] -> ShowS); }
newtype T = T B deriving (ARITHMETIC_CLASSES, FLOATING_CLASSES) \
deriving newtype (Read, Show);
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment