{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module GHC.Tc.Deriv ( tcDeriving, DerivInfo(..) ) where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Hs
import GHC.Driver.Session
import GHC.Tc.Utils.Monad
import GHC.Tc.Instance.Family
import GHC.Tc.Types.Origin
import GHC.Core.Predicate
import GHC.Tc.Deriv.Infer
import GHC.Tc.Deriv.Utils
import GHC.Tc.TyCl.Class( instDeclCtxt3, tcATDefault )
import GHC.Tc.Utils.Env
import GHC.Tc.Deriv.Generate
import GHC.Tc.Validity( allDistinctTyVars, checkValidInstHead )
import GHC.Core.InstEnv
import GHC.Tc.Utils.Instantiate
import GHC.Core.FamInstEnv
import GHC.Tc.Gen.HsType
import GHC.Core.TyCo.Rep
import GHC.Core.TyCo.Ppr ( pprTyVars )
import GHC.Rename.Bind
import GHC.Rename.Env
import GHC.Rename.Module ( addTcgDUs )
import GHC.Rename.Utils
import GHC.Core.Unify( tcUnifyTy )
import GHC.Core.Class
import GHC.Core.Type
import GHC.Utils.Error
import GHC.Core.DataCon
import GHC.Data.Maybe
import GHC.Types.Name.Reader
import GHC.Types.Name
import GHC.Types.Name.Set as NameSet
import GHC.Core.TyCon
import GHC.Tc.Utils.TcType
import GHC.Types.Var as Var
import GHC.Types.Var.Env
import GHC.Types.Var.Set
import GHC.Builtin.Names
import GHC.Types.SrcLoc
import GHC.Utils.Misc
import GHC.Utils.Outputable as Outputable
import GHC.Utils.Panic
import GHC.Utils.Logger
import GHC.Data.FastString
import GHC.Data.Bag
import GHC.Utils.FV as FV (fvVarList, unionFV, mkFVs)
import qualified GHC.LanguageExtensions as LangExt
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader
import Data.List (partition, find)
data EarlyDerivSpec = InferTheta (DerivSpec [ThetaOrigin])
| GivenTheta (DerivSpec ThetaType)
splitEarlyDerivSpec :: [EarlyDerivSpec]
-> ([DerivSpec [ThetaOrigin]], [DerivSpec ThetaType])
splitEarlyDerivSpec :: [EarlyDerivSpec]
-> ([DerivSpec [ThetaOrigin]], [DerivSpec [PredType]])
splitEarlyDerivSpec [] = ([],[])
splitEarlyDerivSpec (InferTheta DerivSpec [ThetaOrigin]
spec : [EarlyDerivSpec]
specs) =
case [EarlyDerivSpec]
-> ([DerivSpec [ThetaOrigin]], [DerivSpec [PredType]])
splitEarlyDerivSpec [EarlyDerivSpec]
specs of ([DerivSpec [ThetaOrigin]]
is, [DerivSpec [PredType]]
gs) -> (DerivSpec [ThetaOrigin]
spec forall a. a -> [a] -> [a]
: [DerivSpec [ThetaOrigin]]
is, [DerivSpec [PredType]]
gs)
splitEarlyDerivSpec (GivenTheta DerivSpec [PredType]
spec : [EarlyDerivSpec]
specs) =
case [EarlyDerivSpec]
-> ([DerivSpec [ThetaOrigin]], [DerivSpec [PredType]])
splitEarlyDerivSpec [EarlyDerivSpec]
specs of ([DerivSpec [ThetaOrigin]]
is, [DerivSpec [PredType]]
gs) -> ([DerivSpec [ThetaOrigin]]
is, DerivSpec [PredType]
spec forall a. a -> [a] -> [a]
: [DerivSpec [PredType]]
gs)
instance Outputable EarlyDerivSpec where
ppr :: EarlyDerivSpec -> SDoc
ppr (InferTheta DerivSpec [ThetaOrigin]
spec) = forall a. Outputable a => a -> SDoc
ppr DerivSpec [ThetaOrigin]
spec SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"(Infer)"
ppr (GivenTheta DerivSpec [PredType]
spec) = forall a. Outputable a => a -> SDoc
ppr DerivSpec [PredType]
spec SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"(Given)"
data DerivInfo = DerivInfo { DerivInfo -> TyCon
di_rep_tc :: TyCon
, DerivInfo -> [(Name, TyVar)]
di_scoped_tvs :: ![(Name,TyVar)]
, DerivInfo -> [LHsDerivingClause GhcRn]
di_clauses :: [LHsDerivingClause GhcRn]
, DerivInfo -> SDoc
di_ctxt :: SDoc
}
tcDeriving :: [DerivInfo]
-> [LDerivDecl GhcRn]
-> TcM (TcGblEnv, Bag (InstInfo GhcRn), HsValBinds GhcRn)
tcDeriving :: [DerivInfo]
-> [LDerivDecl GhcRn]
-> TcM (TcGblEnv, Bag (InstInfo GhcRn), HsValBinds GhcRn)
tcDeriving [DerivInfo]
deriv_infos [LDerivDecl GhcRn]
deriv_decls
= forall r. TcRn r -> TcRn r -> TcRn r
recoverM (do { TcGblEnv
g <- forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
; forall (m :: * -> *) a. Monad m => a -> m a
return (TcGblEnv
g, forall a. Bag a
emptyBag, forall (a :: Pass) (b :: Pass).
HsValBindsLR (GhcPass a) (GhcPass b)
emptyValBindsOut)}) forall a b. (a -> b) -> a -> b
$
do {
[EarlyDerivSpec]
early_specs <- [DerivInfo] -> [LDerivDecl GhcRn] -> TcM [EarlyDerivSpec]
makeDerivSpecs [DerivInfo]
deriv_infos [LDerivDecl GhcRn]
deriv_decls
; String -> SDoc -> TcRn ()
traceTc String
"tcDeriving" (forall a. Outputable a => a -> SDoc
ppr [EarlyDerivSpec]
early_specs)
; let ([DerivSpec [ThetaOrigin]]
infer_specs, [DerivSpec [PredType]]
given_specs) = [EarlyDerivSpec]
-> ([DerivSpec [ThetaOrigin]], [DerivSpec [PredType]])
splitEarlyDerivSpec [EarlyDerivSpec]
early_specs
; [([PredType] -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])]
insts1 <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall theta.
DerivSpec theta
-> TcM ([PredType] -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])
genInst [DerivSpec [PredType]]
given_specs
; [([PredType] -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])]
insts2 <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall theta.
DerivSpec theta
-> TcM ([PredType] -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])
genInst [DerivSpec [ThetaOrigin]]
infer_specs
; DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
; Logger
logger <- forall (m :: * -> *). HasLogger m => m Logger
getLogger
; let ([[PredType] -> TcM (InstInfo GhcPs)]
_, [BagDerivStuff]
deriv_stuff, [[Name]]
fvs) = forall a b c. [(a, b, c)] -> ([a], [b], [c])
unzip3 ([([PredType] -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])]
insts1 forall a. [a] -> [a] -> [a]
++ [([PredType] -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])]
insts2)
; SrcSpan
loc <- TcRn SrcSpan
getSrcSpanM
; let (Bag (LHsBind GhcPs, LSig GhcPs)
binds, Bag FamInst
famInsts) = DynFlags
-> SrcSpan
-> BagDerivStuff
-> (Bag (LHsBind GhcPs, LSig GhcPs), Bag FamInst)
genAuxBinds DynFlags
dflags SrcSpan
loc
(forall a. [Bag a] -> Bag a
unionManyBags [BagDerivStuff]
deriv_stuff)
; let mk_inst_infos1 :: [[PredType] -> TcM (InstInfo GhcPs)]
mk_inst_infos1 = forall a b. (a -> b) -> [a] -> [b]
map forall a b c. (a, b, c) -> a
fstOf3 [([PredType] -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])]
insts1
; [InstInfo GhcPs]
inst_infos1 <- [[PredType] -> TcM (InstInfo GhcPs)]
-> [DerivSpec [PredType]] -> TcM [InstInfo GhcPs]
apply_inst_infos [[PredType] -> TcM (InstInfo GhcPs)]
mk_inst_infos1 [DerivSpec [PredType]]
given_specs
; forall a. [FamInst] -> TcM a -> TcM a
tcExtendLocalFamInstEnv (forall a. Bag a -> [a]
bagToList Bag FamInst
famInsts) forall a b. (a -> b) -> a -> b
$
do {
; [DerivSpec [PredType]]
final_specs <- forall a. [ClsInst] -> TcM a -> TcM a
extendLocalInstEnv (forall a b. (a -> b) -> [a] -> [b]
map forall a. InstInfo a -> ClsInst
iSpec [InstInfo GhcPs]
inst_infos1) forall a b. (a -> b) -> a -> b
$
[DerivSpec [ThetaOrigin]] -> TcM [DerivSpec [PredType]]
simplifyInstanceContexts [DerivSpec [ThetaOrigin]]
infer_specs
; let mk_inst_infos2 :: [[PredType] -> TcM (InstInfo GhcPs)]
mk_inst_infos2 = forall a b. (a -> b) -> [a] -> [b]
map forall a b c. (a, b, c) -> a
fstOf3 [([PredType] -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])]
insts2
; [InstInfo GhcPs]
inst_infos2 <- [[PredType] -> TcM (InstInfo GhcPs)]
-> [DerivSpec [PredType]] -> TcM [InstInfo GhcPs]
apply_inst_infos [[PredType] -> TcM (InstInfo GhcPs)]
mk_inst_infos2 [DerivSpec [PredType]]
final_specs
; let inst_infos :: [InstInfo GhcPs]
inst_infos = [InstInfo GhcPs]
inst_infos1 forall a. [a] -> [a] -> [a]
++ [InstInfo GhcPs]
inst_infos2
; (Bag (InstInfo GhcRn)
inst_info, HsValBinds GhcRn
rn_binds, DefUses
rn_dus) <- [InstInfo GhcPs]
-> Bag (LHsBind GhcPs, LSig GhcPs)
-> TcM (Bag (InstInfo GhcRn), HsValBinds GhcRn, DefUses)
renameDeriv [InstInfo GhcPs]
inst_infos Bag (LHsBind GhcPs, LSig GhcPs)
binds
; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall a. Bag a -> Bool
isEmptyBag Bag (InstInfo GhcRn)
inst_info) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Logger
-> DynFlags -> DumpFlag -> String -> DumpFormat -> SDoc -> IO ()
dumpIfSet_dyn Logger
logger DynFlags
dflags DumpFlag
Opt_D_dump_deriv String
"Derived instances"
DumpFormat
FormatHaskell
(Bag (InstInfo GhcRn) -> HsValBinds GhcRn -> Bag FamInst -> SDoc
ddump_deriving Bag (InstInfo GhcRn)
inst_info HsValBinds GhcRn
rn_binds Bag FamInst
famInsts))
; TcGblEnv
gbl_env <- forall a. [ClsInst] -> TcM a -> TcM a
tcExtendLocalInstEnv (forall a b. (a -> b) -> [a] -> [b]
map forall a. InstInfo a -> ClsInst
iSpec (forall a. Bag a -> [a]
bagToList Bag (InstInfo GhcRn)
inst_info))
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
; let all_dus :: DefUses
all_dus = DefUses
rn_dus DefUses -> DefUses -> DefUses
`plusDU` Uses -> DefUses
usesOnly ([Name] -> Uses
NameSet.mkFVs forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Name]]
fvs)
; forall (m :: * -> *) a. Monad m => a -> m a
return (TcGblEnv -> DefUses -> TcGblEnv
addTcgDUs TcGblEnv
gbl_env DefUses
all_dus, Bag (InstInfo GhcRn)
inst_info, HsValBinds GhcRn
rn_binds) } }
where
ddump_deriving :: Bag (InstInfo GhcRn) -> HsValBinds GhcRn
-> Bag FamInst
-> SDoc
ddump_deriving :: Bag (InstInfo GhcRn) -> HsValBinds GhcRn -> Bag FamInst -> SDoc
ddump_deriving Bag (InstInfo GhcRn)
inst_infos HsValBinds GhcRn
extra_binds Bag FamInst
repFamInsts
= SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"Derived class instances:")
Int
2 ([SDoc] -> SDoc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (\InstInfo GhcRn
i -> forall (a :: Pass).
OutputableBndrId a =>
InstInfo (GhcPass a) -> SDoc
pprInstInfoDetails InstInfo GhcRn
i SDoc -> SDoc -> SDoc
$$ String -> SDoc
text String
"") (forall a. Bag a -> [a]
bagToList Bag (InstInfo GhcRn)
inst_infos))
SDoc -> SDoc -> SDoc
$$ forall a. Outputable a => a -> SDoc
ppr HsValBinds GhcRn
extra_binds)
SDoc -> SDoc -> SDoc
$$ String -> SDoc -> SDoc
hangP String
"Derived type family instances:"
([SDoc] -> SDoc
vcat (forall a b. (a -> b) -> [a] -> [b]
map FamInst -> SDoc
pprRepTy (forall a. Bag a -> [a]
bagToList Bag FamInst
repFamInsts)))
hangP :: String -> SDoc -> SDoc
hangP String
s SDoc
x = String -> SDoc
text String
"" SDoc -> SDoc -> SDoc
$$ SDoc -> Int -> SDoc -> SDoc
hang (PtrString -> SDoc
ptext (String -> PtrString
sLit String
s)) Int
2 SDoc
x
apply_inst_infos :: [ThetaType -> TcM (InstInfo GhcPs)]
-> [DerivSpec ThetaType] -> TcM [InstInfo GhcPs]
apply_inst_infos :: [[PredType] -> TcM (InstInfo GhcPs)]
-> [DerivSpec [PredType]] -> TcM [InstInfo GhcPs]
apply_inst_infos = forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (\[PredType] -> TcM (InstInfo GhcPs)
f DerivSpec [PredType]
ds -> [PredType] -> TcM (InstInfo GhcPs)
f (forall theta. DerivSpec theta -> theta
ds_theta DerivSpec [PredType]
ds))
pprRepTy :: FamInst -> SDoc
pprRepTy :: FamInst -> SDoc
pprRepTy fi :: FamInst
fi@(FamInst { fi_tys :: FamInst -> [PredType]
fi_tys = [PredType]
lhs })
= String -> SDoc
text String
"type" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr (TyCon -> [PredType] -> PredType
mkTyConApp (FamInst -> TyCon
famInstTyCon FamInst
fi) [PredType]
lhs) SDoc -> SDoc -> SDoc
<+>
SDoc
equals SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr PredType
rhs
where rhs :: PredType
rhs = FamInst -> PredType
famInstRHS FamInst
fi
renameDeriv :: [InstInfo GhcPs]
-> Bag (LHsBind GhcPs, LSig GhcPs)
-> TcM (Bag (InstInfo GhcRn), HsValBinds GhcRn, DefUses)
renameDeriv :: [InstInfo GhcPs]
-> Bag (LHsBind GhcPs, LSig GhcPs)
-> TcM (Bag (InstInfo GhcRn), HsValBinds GhcRn, DefUses)
renameDeriv [InstInfo GhcPs]
inst_infos Bag (LHsBind GhcPs, LSig GhcPs)
bagBinds
= forall a. TcRn a -> TcRn a
discardWarnings forall a b. (a -> b) -> a -> b
$
forall gbl lcl a. Extension -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
setXOptM Extension
LangExt.EmptyCase forall a b. (a -> b) -> a -> b
$
forall gbl lcl a. Extension -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
setXOptM Extension
LangExt.ScopedTypeVariables forall a b. (a -> b) -> a -> b
$
forall gbl lcl a. Extension -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
setXOptM Extension
LangExt.KindSignatures forall a b. (a -> b) -> a -> b
$
forall gbl lcl a. Extension -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
setXOptM Extension
LangExt.TypeApplications forall a b. (a -> b) -> a -> b
$
forall gbl lcl a. Extension -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
unsetXOptM Extension
LangExt.RebindableSyntax forall a b. (a -> b) -> a -> b
$
forall gbl lcl a. Extension -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
setXOptM Extension
LangExt.TemplateHaskellQuotes forall a b. (a -> b) -> a -> b
$
do {
; String -> SDoc -> TcRn ()
traceTc String
"rnd" ([SDoc] -> SDoc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (\InstInfo GhcPs
i -> forall (a :: Pass).
OutputableBndrId a =>
InstInfo (GhcPass a) -> SDoc
pprInstInfoDetails InstInfo GhcPs
i SDoc -> SDoc -> SDoc
$$ String -> SDoc
text String
"") [InstInfo GhcPs]
inst_infos))
; (Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
aux_binds, Bag (GenLocated SrcSpanAnnA (Sig GhcPs))
aux_sigs) <- forall (m :: * -> *) a b c.
Monad m =>
(a -> m (b, c)) -> Bag a -> m (Bag b, Bag c)
mapAndUnzipBagM forall (m :: * -> *) a. Monad m => a -> m a
return Bag (LHsBind GhcPs, LSig GhcPs)
bagBinds
; let aux_val_binds :: HsValBindsLR GhcPs GhcPs
aux_val_binds = forall idL idR.
XValBinds idL idR
-> LHsBindsLR idL idR -> [LSig idR] -> HsValBindsLR idL idR
ValBinds AnnSortKey
NoAnnSortKey Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
aux_binds (forall a. Bag a -> [a]
bagToList Bag (GenLocated SrcSpanAnnA (Sig GhcPs))
aux_sigs)
; ([Name]
bndrs, HsValBindsLR GhcRn GhcPs
rn_aux_lhs) <- MiniFixityEnv
-> HsValBindsLR GhcPs GhcPs
-> RnM ([Name], HsValBindsLR GhcRn GhcPs)
rnLocalValBindsLHS forall a. FastStringEnv a
emptyFsEnv HsValBindsLR GhcPs GhcPs
aux_val_binds
; forall a. [Name] -> RnM a -> RnM a
bindLocalNames [Name]
bndrs forall a b. (a -> b) -> a -> b
$
do { (HsValBinds GhcRn
rn_aux, DefUses
dus_aux) <- Uses -> HsValBindsLR GhcRn GhcPs -> RnM (HsValBinds GhcRn, DefUses)
rnLocalValBindsRHS ([Name] -> Uses
mkNameSet [Name]
bndrs) HsValBindsLR GhcRn GhcPs
rn_aux_lhs
; ([InstInfo GhcRn]
rn_inst_infos, [Uses]
fvs_insts) <- forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM InstInfo GhcPs -> TcM (InstInfo GhcRn, Uses)
rn_inst_info [InstInfo GhcPs]
inst_infos
; forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. [a] -> Bag a
listToBag [InstInfo GhcRn]
rn_inst_infos, HsValBinds GhcRn
rn_aux,
DefUses
dus_aux DefUses -> DefUses -> DefUses
`plusDU` Uses -> DefUses
usesOnly ([Uses] -> Uses
plusFVs [Uses]
fvs_insts)) } }
where
rn_inst_info :: InstInfo GhcPs -> TcM (InstInfo GhcRn, FreeVars)
rn_inst_info :: InstInfo GhcPs -> TcM (InstInfo GhcRn, Uses)
rn_inst_info
inst_info :: InstInfo GhcPs
inst_info@(InstInfo { iSpec :: forall a. InstInfo a -> ClsInst
iSpec = ClsInst
inst
, iBinds :: forall a. InstInfo a -> InstBindings a
iBinds = InstBindings
{ ib_binds :: forall a. InstBindings a -> LHsBinds a
ib_binds = LHsBinds GhcPs
binds
, ib_tyvars :: forall a. InstBindings a -> [Name]
ib_tyvars = [Name]
tyvars
, ib_pragmas :: forall a. InstBindings a -> [LSig a]
ib_pragmas = [LSig GhcPs]
sigs
, ib_extensions :: forall a. InstBindings a -> [Extension]
ib_extensions = [Extension]
exts
, ib_derived :: forall a. InstBindings a -> Bool
ib_derived = Bool
sa } })
= do { (Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn))
rn_binds, [GenLocated SrcSpanAnnA (Sig GhcRn)]
rn_sigs, Uses
fvs) <- Bool
-> Name
-> [Name]
-> LHsBinds GhcPs
-> [LSig GhcPs]
-> RnM (LHsBinds GhcRn, [LSig GhcRn], Uses)
rnMethodBinds Bool
False (ClsInst -> Name
is_cls_nm ClsInst
inst)
[Name]
tyvars LHsBinds GhcPs
binds [LSig GhcPs]
sigs
; let binds' :: InstBindings GhcRn
binds' = InstBindings { ib_binds :: LHsBinds GhcRn
ib_binds = Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn))
rn_binds
, ib_tyvars :: [Name]
ib_tyvars = [Name]
tyvars
, ib_pragmas :: [LSig GhcRn]
ib_pragmas = [GenLocated SrcSpanAnnA (Sig GhcRn)]
rn_sigs
, ib_extensions :: [Extension]
ib_extensions = [Extension]
exts
, ib_derived :: Bool
ib_derived = Bool
sa }
; forall (m :: * -> *) a. Monad m => a -> m a
return (InstInfo GhcPs
inst_info { iBinds :: InstBindings GhcRn
iBinds = InstBindings GhcRn
binds' }, Uses
fvs) }
makeDerivSpecs :: [DerivInfo]
-> [LDerivDecl GhcRn]
-> TcM [EarlyDerivSpec]
makeDerivSpecs :: [DerivInfo] -> [LDerivDecl GhcRn] -> TcM [EarlyDerivSpec]
makeDerivSpecs [DerivInfo]
deriv_infos [LDerivDecl GhcRn]
deriv_decls
= do { [[EarlyDerivSpec]]
eqns1 <- forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA
[ TyCon
-> [(Name, TyVar)]
-> Maybe (LDerivStrategy GhcRn)
-> [LHsSigType GhcRn]
-> SDoc
-> TcM [EarlyDerivSpec]
deriveClause TyCon
rep_tc [(Name, TyVar)]
scoped_tvs Maybe (LDerivStrategy GhcRn)
dcs (LDerivClauseTys GhcRn -> [LHsSigType GhcRn]
deriv_clause_preds LDerivClauseTys GhcRn
dct) SDoc
err_ctxt
| DerivInfo { di_rep_tc :: DerivInfo -> TyCon
di_rep_tc = TyCon
rep_tc
, di_scoped_tvs :: DerivInfo -> [(Name, TyVar)]
di_scoped_tvs = [(Name, TyVar)]
scoped_tvs
, di_clauses :: DerivInfo -> [LHsDerivingClause GhcRn]
di_clauses = [LHsDerivingClause GhcRn]
clauses
, di_ctxt :: DerivInfo -> SDoc
di_ctxt = SDoc
err_ctxt } <- [DerivInfo]
deriv_infos
, L SrcSpan
_ (HsDerivingClause { deriv_clause_strategy :: forall pass. HsDerivingClause pass -> Maybe (LDerivStrategy pass)
deriv_clause_strategy = Maybe (LDerivStrategy GhcRn)
dcs
, deriv_clause_tys :: forall pass. HsDerivingClause pass -> LDerivClauseTys pass
deriv_clause_tys = LDerivClauseTys GhcRn
dct })
<- [LHsDerivingClause GhcRn]
clauses
]
; [Maybe EarlyDerivSpec]
eqns2 <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall r. TcRn r -> TcRn r -> TcRn r
recoverM (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing) forall b c a. (b -> c) -> (a -> b) -> a -> c
. LDerivDecl GhcRn
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe EarlyDerivSpec)
deriveStandalone) [LDerivDecl GhcRn]
deriv_decls
; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[EarlyDerivSpec]]
eqns1 forall a. [a] -> [a] -> [a]
++ forall a. [Maybe a] -> [a]
catMaybes [Maybe EarlyDerivSpec]
eqns2 }
where
deriv_clause_preds :: LDerivClauseTys GhcRn -> [LHsSigType GhcRn]
deriv_clause_preds :: LDerivClauseTys GhcRn -> [LHsSigType GhcRn]
deriv_clause_preds (L SrcSpanAnnC
_ DerivClauseTys GhcRn
dct) = case DerivClauseTys GhcRn
dct of
DctSingle XDctSingle GhcRn
_ LHsSigType GhcRn
ty -> [LHsSigType GhcRn
ty]
DctMulti XDctMulti GhcRn
_ [LHsSigType GhcRn]
tys -> [LHsSigType GhcRn]
tys
deriveClause :: TyCon
-> [(Name, TcTyVar)]
-> Maybe (LDerivStrategy GhcRn)
-> [LHsSigType GhcRn] -> SDoc
-> TcM [EarlyDerivSpec]
deriveClause :: TyCon
-> [(Name, TyVar)]
-> Maybe (LDerivStrategy GhcRn)
-> [LHsSigType GhcRn]
-> SDoc
-> TcM [EarlyDerivSpec]
deriveClause TyCon
rep_tc [(Name, TyVar)]
scoped_tvs Maybe (LDerivStrategy GhcRn)
mb_lderiv_strat [LHsSigType GhcRn]
deriv_preds SDoc
err_ctxt
= forall a. SDoc -> TcM a -> TcM a
addErrCtxt SDoc
err_ctxt forall a b. (a -> b) -> a -> b
$ do
String -> SDoc -> TcRn ()
traceTc String
"deriveClause" forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat
[ String -> SDoc
text String
"tvs" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr [TyVar]
tvs
, String -> SDoc
text String
"scoped_tvs" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr [(Name, TyVar)]
scoped_tvs
, String -> SDoc
text String
"tc" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr TyCon
tc
, String -> SDoc
text String
"tys" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr [PredType]
tys
, String -> SDoc
text String
"mb_lderiv_strat" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr Maybe (LDerivStrategy GhcRn)
mb_lderiv_strat ]
forall r. [(Name, TyVar)] -> TcM r -> TcM r
tcExtendNameTyVarEnv [(Name, TyVar)]
scoped_tvs forall a b. (a -> b) -> a -> b
$ do
(Maybe (GenLocated SrcSpan (DerivStrategy GhcTc))
mb_lderiv_strat', [TyVar]
via_tvs) <- Maybe (LDerivStrategy GhcRn)
-> TcM (Maybe (LDerivStrategy GhcTc), [TyVar])
tcDerivStrategy Maybe (LDerivStrategy GhcRn)
mb_lderiv_strat
forall r. [TyVar] -> TcM r -> TcM r
tcExtendTyVarEnv [TyVar]
via_tvs forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM (TyCon
-> [PredType]
-> Maybe (LDerivStrategy GhcTc)
-> [TyVar]
-> LHsSigType GhcRn
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe EarlyDerivSpec)
derivePred TyCon
tc [PredType]
tys Maybe (GenLocated SrcSpan (DerivStrategy GhcTc))
mb_lderiv_strat' [TyVar]
via_tvs) [LHsSigType GhcRn]
deriv_preds
where
tvs :: [TyVar]
tvs = TyCon -> [TyVar]
tyConTyVars TyCon
rep_tc
(TyCon
tc, [PredType]
tys) = case TyCon -> Maybe (TyCon, [PredType], CoAxiom Unbranched)
tyConFamInstSig_maybe TyCon
rep_tc of
Just (TyCon
fam_tc, [PredType]
pats, CoAxiom Unbranched
_) -> (TyCon
fam_tc, [PredType]
pats)
Maybe (TyCon, [PredType], CoAxiom Unbranched)
_ -> (TyCon
rep_tc, [TyVar] -> [PredType]
mkTyVarTys [TyVar]
tvs)
derivePred :: TyCon -> [Type] -> Maybe (LDerivStrategy GhcTc) -> [TyVar]
-> LHsSigType GhcRn -> TcM (Maybe EarlyDerivSpec)
derivePred :: TyCon
-> [PredType]
-> Maybe (LDerivStrategy GhcTc)
-> [TyVar]
-> LHsSigType GhcRn
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe EarlyDerivSpec)
derivePred TyCon
tc [PredType]
tys Maybe (LDerivStrategy GhcTc)
mb_lderiv_strat [TyVar]
via_tvs LHsSigType GhcRn
deriv_pred =
forall r. TcRn r -> TcRn r -> TcRn r
recoverM (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing) forall a b. (a -> b) -> a -> b
$
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA LHsSigType GhcRn
deriv_pred) forall a b. (a -> b) -> a -> b
$ do
String -> SDoc -> TcRn ()
traceTc String
"derivePred" forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat
[ String -> SDoc
text String
"tc" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr TyCon
tc
, String -> SDoc
text String
"tys" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr [PredType]
tys
, String -> SDoc
text String
"deriv_pred" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr LHsSigType GhcRn
deriv_pred
, String -> SDoc
text String
"mb_lderiv_strat" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr Maybe (LDerivStrategy GhcTc)
mb_lderiv_strat
, String -> SDoc
text String
"via_tvs" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr [TyVar]
via_tvs ]
([TyVar]
cls_tvs, Class
cls, [PredType]
cls_tys, [PredType]
cls_arg_kinds) <- LHsSigType GhcRn -> TcM ([TyVar], Class, [PredType], [PredType])
tcHsDeriv LHsSigType GhcRn
deriv_pred
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([PredType]
cls_arg_kinds forall a. [a] -> Int -> Bool
`lengthIsNot` Int
1) forall a b. (a -> b) -> a -> b
$
forall a. SDoc -> TcM a
failWithTc (LHsSigType GhcRn -> SDoc
nonUnaryErr LHsSigType GhcRn
deriv_pred)
let [PredType
cls_arg_kind] = [PredType]
cls_arg_kinds
mb_deriv_strat :: Maybe (DerivStrategy GhcTc)
mb_deriv_strat = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall l e. GenLocated l e -> e
unLoc Maybe (LDerivStrategy GhcTc)
mb_lderiv_strat
if (Class -> Name
className Class
cls forall a. Eq a => a -> a -> Bool
== Name
typeableClassName)
then do TcRn ()
warnUselessTypeable
forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else let deriv_tvs :: [TyVar]
deriv_tvs = [TyVar]
via_tvs forall a. [a] -> [a] -> [a]
++ [TyVar]
cls_tvs in
forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TyCon
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> [TyVar]
-> Class
-> [PredType]
-> PredType
-> TcM EarlyDerivSpec
deriveTyData TyCon
tc [PredType]
tys Maybe (DerivStrategy GhcTc)
mb_deriv_strat
[TyVar]
deriv_tvs Class
cls [PredType]
cls_tys PredType
cls_arg_kind
deriveStandalone :: LDerivDecl GhcRn -> TcM (Maybe EarlyDerivSpec)
deriveStandalone :: LDerivDecl GhcRn
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe EarlyDerivSpec)
deriveStandalone (L SrcSpanAnnA
loc (DerivDecl XCDerivDecl GhcRn
_ LHsSigWcType GhcRn
deriv_ty Maybe (LDerivStrategy GhcRn)
mb_lderiv_strat Maybe (XRec GhcRn OverlapMode)
overlap_mode))
= forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc forall a b. (a -> b) -> a -> b
$
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (LHsSigWcType GhcRn -> SDoc
standaloneCtxt LHsSigWcType GhcRn
deriv_ty) forall a b. (a -> b) -> a -> b
$
do { String -> SDoc -> TcRn ()
traceTc String
"Standalone deriving decl for" (forall a. Outputable a => a -> SDoc
ppr LHsSigWcType GhcRn
deriv_ty)
; let ctxt :: UserTypeCtxt
ctxt = Bool -> UserTypeCtxt
GHC.Tc.Types.Origin.InstDeclCtxt Bool
True
; String -> SDoc -> TcRn ()
traceTc String
"Deriving strategy (standalone deriving)" forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [forall a. Outputable a => a -> SDoc
ppr Maybe (LDerivStrategy GhcRn)
mb_lderiv_strat, forall a. Outputable a => a -> SDoc
ppr LHsSigWcType GhcRn
deriv_ty]
; (Maybe (GenLocated SrcSpan (DerivStrategy GhcTc))
mb_lderiv_strat, [TyVar]
via_tvs) <- Maybe (LDerivStrategy GhcRn)
-> TcM (Maybe (LDerivStrategy GhcTc), [TyVar])
tcDerivStrategy Maybe (LDerivStrategy GhcRn)
mb_lderiv_strat
; ([TyVar]
cls_tvs, DerivContext
deriv_ctxt, Class
cls, [PredType]
inst_tys)
<- forall r. [TyVar] -> TcM r -> TcM r
tcExtendTyVarEnv [TyVar]
via_tvs forall a b. (a -> b) -> a -> b
$
UserTypeCtxt
-> LHsSigWcType GhcRn
-> TcM ([TyVar], DerivContext, Class, [PredType])
tcStandaloneDerivInstType UserTypeCtxt
ctxt LHsSigWcType GhcRn
deriv_ty
; let mb_deriv_strat :: Maybe (DerivStrategy GhcTc)
mb_deriv_strat = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall l e. GenLocated l e -> e
unLoc Maybe (GenLocated SrcSpan (DerivStrategy GhcTc))
mb_lderiv_strat
tvs :: [TyVar]
tvs = [TyVar]
via_tvs forall a. [a] -> [a] -> [a]
++ [TyVar]
cls_tvs
; ([TyVar]
tvs', DerivContext
deriv_ctxt', [PredType]
inst_tys', Maybe (DerivStrategy GhcTc)
mb_deriv_strat') <-
case Maybe (DerivStrategy GhcTc)
mb_deriv_strat of
Just (ViaStrategy XViaStrategy GhcTc
via_ty)
| Just PredType
inst_ty <- forall a. [a] -> Maybe a
lastMaybe [PredType]
inst_tys
-> do
let via_kind :: PredType
via_kind = HasDebugCallStack => PredType -> PredType
tcTypeKind XViaStrategy GhcTc
via_ty
inst_ty_kind :: PredType
inst_ty_kind = HasDebugCallStack => PredType -> PredType
tcTypeKind PredType
inst_ty
mb_match :: Maybe TCvSubst
mb_match = PredType -> PredType -> Maybe TCvSubst
tcUnifyTy PredType
inst_ty_kind PredType
via_kind
Bool -> SDoc -> TcRn ()
checkTc (forall a. Maybe a -> Bool
isJust Maybe TCvSubst
mb_match)
(Class -> PredType -> PredType -> PredType -> SDoc
derivingViaKindErr Class
cls PredType
inst_ty_kind
XViaStrategy GhcTc
via_ty PredType
via_kind)
let Just TCvSubst
kind_subst = Maybe TCvSubst
mb_match
ki_subst_range :: VarSet
ki_subst_range = TCvSubst -> VarSet
getTCvSubstRangeFVs TCvSubst
kind_subst
unmapped_tkvs :: [TyVar]
unmapped_tkvs = forall a. (a -> Bool) -> [a] -> [a]
filter (\TyVar
v -> TyVar
v TyVar -> TCvSubst -> Bool
`notElemTCvSubst` TCvSubst
kind_subst
Bool -> Bool -> Bool
&& Bool -> Bool
not (TyVar
v TyVar -> VarSet -> Bool
`elemVarSet` VarSet
ki_subst_range))
[TyVar]
tvs
(TCvSubst
subst, [TyVar]
_) = HasCallStack => TCvSubst -> [TyVar] -> (TCvSubst, [TyVar])
substTyVarBndrs TCvSubst
kind_subst [TyVar]
unmapped_tkvs
(DerivContext
final_deriv_ctxt, [PredType]
final_deriv_ctxt_tys)
= case DerivContext
deriv_ctxt of
InferContext Maybe SrcSpan
wc -> (Maybe SrcSpan -> DerivContext
InferContext Maybe SrcSpan
wc, [])
SupplyContext [PredType]
theta ->
let final_theta :: [PredType]
final_theta = HasCallStack => TCvSubst -> [PredType] -> [PredType]
substTheta TCvSubst
subst [PredType]
theta
in ([PredType] -> DerivContext
SupplyContext [PredType]
final_theta, [PredType]
final_theta)
final_inst_tys :: [PredType]
final_inst_tys = HasCallStack => TCvSubst -> [PredType] -> [PredType]
substTys TCvSubst
subst [PredType]
inst_tys
final_via_ty :: PredType
final_via_ty = HasCallStack => TCvSubst -> PredType -> PredType
substTy TCvSubst
subst XViaStrategy GhcTc
via_ty
final_tvs :: [TyVar]
final_tvs = [PredType] -> [TyVar]
tyCoVarsOfTypesWellScoped forall a b. (a -> b) -> a -> b
$
[PredType]
final_deriv_ctxt_tys forall a. [a] -> [a] -> [a]
++ [PredType]
final_inst_tys
forall a. [a] -> [a] -> [a]
++ [PredType
final_via_ty]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ( [TyVar]
final_tvs, DerivContext
final_deriv_ctxt, [PredType]
final_inst_tys
, forall a. a -> Maybe a
Just (forall pass. XViaStrategy pass -> DerivStrategy pass
ViaStrategy PredType
final_via_ty) )
Maybe (DerivStrategy GhcTc)
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TyVar]
tvs, DerivContext
deriv_ctxt, [PredType]
inst_tys, Maybe (DerivStrategy GhcTc)
mb_deriv_strat)
; String -> SDoc -> TcRn ()
traceTc String
"Standalone deriving;" forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat
[ String -> SDoc
text String
"tvs':" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr [TyVar]
tvs'
, String -> SDoc
text String
"mb_deriv_strat':" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr Maybe (DerivStrategy GhcTc)
mb_deriv_strat'
, String -> SDoc
text String
"deriv_ctxt':" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr DerivContext
deriv_ctxt'
, String -> SDoc
text String
"cls:" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr Class
cls
, String -> SDoc
text String
"inst_tys':" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr [PredType]
inst_tys' ]
; if Class -> Name
className Class
cls forall a. Eq a => a -> a -> Bool
== Name
typeableClassName
then do TcRn ()
warnUselessTypeable
forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe OverlapMode
-> [TyVar]
-> Class
-> [PredType]
-> DerivContext
-> Maybe (DerivStrategy GhcTc)
-> TcM EarlyDerivSpec
mkEqnHelp (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall l e. GenLocated l e -> e
unLoc Maybe (XRec GhcRn OverlapMode)
overlap_mode)
[TyVar]
tvs' Class
cls [PredType]
inst_tys'
DerivContext
deriv_ctxt' Maybe (DerivStrategy GhcTc)
mb_deriv_strat' }
tcStandaloneDerivInstType
:: UserTypeCtxt -> LHsSigWcType GhcRn
-> TcM ([TyVar], DerivContext, Class, [Type])
tcStandaloneDerivInstType :: UserTypeCtxt
-> LHsSigWcType GhcRn
-> TcM ([TyVar], DerivContext, Class, [PredType])
tcStandaloneDerivInstType UserTypeCtxt
ctxt
(HsWC { hswc_body :: forall pass thing. HsWildCardBndrs pass thing -> thing
hswc_body = deriv_ty :: LHsSigType GhcRn
deriv_ty@(L SrcSpanAnnA
loc (HsSig { sig_bndrs :: forall pass. HsSigType pass -> HsOuterSigTyVarBndrs pass
sig_bndrs = HsOuterSigTyVarBndrs GhcRn
outer_bndrs
, sig_body :: forall pass. HsSigType pass -> LHsType pass
sig_body = LHsType GhcRn
deriv_ty_body }))})
| (Maybe (LHsContext GhcRn)
theta, LHsType GhcRn
rho) <- forall (pass :: Pass).
LHsType (GhcPass pass)
-> (Maybe (LHsContext (GhcPass pass)), LHsType (GhcPass pass))
splitLHsQualTy LHsType GhcRn
deriv_ty_body
, [LHsType GhcRn
wc_pred] <- forall (p :: Pass).
Maybe (LHsContext (GhcPass p)) -> HsContext (GhcPass p)
fromMaybeContext Maybe (LHsContext GhcRn)
theta
, L SrcSpanAnnA
wc_span (HsWildCardTy XWildCardTy GhcRn
_) <- forall (p :: Pass). LHsType (GhcPass p) -> LHsType (GhcPass p)
ignoreParens LHsType GhcRn
wc_pred
= do PredType
dfun_ty <- UserTypeCtxt -> LHsSigType GhcRn -> TcM PredType
tcHsClsInstType UserTypeCtxt
ctxt forall a b. (a -> b) -> a -> b
$ forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc forall a b. (a -> b) -> a -> b
$
HsSig { sig_ext :: XHsSig GhcRn
sig_ext = NoExtField
noExtField
, sig_bndrs :: HsOuterSigTyVarBndrs GhcRn
sig_bndrs = HsOuterSigTyVarBndrs GhcRn
outer_bndrs
, sig_body :: LHsType GhcRn
sig_body = LHsType GhcRn
rho }
let ([TyVar]
tvs, [PredType]
_theta, Class
cls, [PredType]
inst_tys) = PredType -> ([TyVar], [PredType], Class, [PredType])
tcSplitDFunTy PredType
dfun_ty
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TyVar]
tvs, Maybe SrcSpan -> DerivContext
InferContext (forall a. a -> Maybe a
Just (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
wc_span)), Class
cls, [PredType]
inst_tys)
| Bool
otherwise
= do PredType
dfun_ty <- UserTypeCtxt -> LHsSigType GhcRn -> TcM PredType
tcHsClsInstType UserTypeCtxt
ctxt LHsSigType GhcRn
deriv_ty
let ([TyVar]
tvs, [PredType]
theta, Class
cls, [PredType]
inst_tys) = PredType -> ([TyVar], [PredType], Class, [PredType])
tcSplitDFunTy PredType
dfun_ty
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TyVar]
tvs, [PredType] -> DerivContext
SupplyContext [PredType]
theta, Class
cls, [PredType]
inst_tys)
warnUselessTypeable :: TcM ()
warnUselessTypeable :: TcRn ()
warnUselessTypeable
= do { Bool
warn <- forall gbl lcl. WarningFlag -> TcRnIf gbl lcl Bool
woptM WarningFlag
Opt_WarnDerivingTypeable
; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
warn forall a b. (a -> b) -> a -> b
$ WarnReason -> SDoc -> TcRn ()
addWarnTc (WarningFlag -> WarnReason
Reason WarningFlag
Opt_WarnDerivingTypeable)
forall a b. (a -> b) -> a -> b
$ String -> SDoc
text String
"Deriving" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr Name
typeableClassName) SDoc -> SDoc -> SDoc
<+>
String -> SDoc
text String
"has no effect: all types now auto-derive Typeable" }
deriveTyData :: TyCon -> [Type]
-> Maybe (DerivStrategy GhcTc)
-> [TyVar]
-> Class
-> [Type]
-> Kind
-> TcM EarlyDerivSpec
deriveTyData :: TyCon
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> [TyVar]
-> Class
-> [PredType]
-> PredType
-> TcM EarlyDerivSpec
deriveTyData TyCon
tc [PredType]
tc_args Maybe (DerivStrategy GhcTc)
mb_deriv_strat [TyVar]
deriv_tvs Class
cls [PredType]
cls_tys PredType
cls_arg_kind
= do {
let ([Scaled PredType]
arg_kinds, PredType
_) = PredType -> ([Scaled PredType], PredType)
splitFunTys PredType
cls_arg_kind
n_args_to_drop :: Int
n_args_to_drop = forall (t :: * -> *) a. Foldable t => t a -> Int
length [Scaled PredType]
arg_kinds
n_args_to_keep :: Int
n_args_to_keep = forall (t :: * -> *) a. Foldable t => t a -> Int
length [PredType]
tc_args forall a. Num a => a -> a -> a
- Int
n_args_to_drop
([PredType]
tc_args_to_keep, [PredType]
args_to_drop)
= forall a. Int -> [a] -> ([a], [a])
splitAt Int
n_args_to_keep [PredType]
tc_args
inst_ty_kind :: PredType
inst_ty_kind = HasDebugCallStack => PredType -> PredType
tcTypeKind (TyCon -> [PredType] -> PredType
mkTyConApp TyCon
tc [PredType]
tc_args_to_keep)
mb_match :: Maybe TCvSubst
mb_match = PredType -> PredType -> Maybe TCvSubst
tcUnifyTy PredType
inst_ty_kind PredType
cls_arg_kind
enough_args :: Bool
enough_args = Int
n_args_to_keep forall a. Ord a => a -> a -> Bool
>= Int
0
; Bool -> SDoc -> TcRn ()
checkTc (Bool
enough_args Bool -> Bool -> Bool
&& forall a. Maybe a -> Bool
isJust Maybe TCvSubst
mb_match)
(TyCon -> Class -> [PredType] -> PredType -> Bool -> SDoc
derivingKindErr TyCon
tc Class
cls [PredType]
cls_tys PredType
cls_arg_kind Bool
enough_args)
; let
deriv_strat_tys :: Maybe (DerivStrategy GhcTc) -> [Type]
deriv_strat_tys :: Maybe (DerivStrategy GhcTc) -> [PredType]
deriv_strat_tys = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (forall p (pass :: Pass) r.
(p ~ GhcPass pass) =>
r -> (XViaStrategy p -> r) -> DerivStrategy p -> r
foldDerivStrategy [] (forall a. a -> [a] -> [a]
:[]))
propagate_subst :: TCvSubst
-> [TyVar]
-> [PredType]
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> ([TyVar], [PredType], [PredType], Maybe (DerivStrategy GhcTc))
propagate_subst TCvSubst
kind_subst [TyVar]
tkvs' [PredType]
cls_tys' [PredType]
tc_args' Maybe (DerivStrategy GhcTc)
mb_deriv_strat'
= ([TyVar]
final_tkvs, [PredType]
final_cls_tys, [PredType]
final_tc_args, Maybe (DerivStrategy GhcTc)
final_mb_deriv_strat)
where
ki_subst_range :: VarSet
ki_subst_range = TCvSubst -> VarSet
getTCvSubstRangeFVs TCvSubst
kind_subst
unmapped_tkvs :: [TyVar]
unmapped_tkvs = forall a. (a -> Bool) -> [a] -> [a]
filter (\TyVar
v -> TyVar
v TyVar -> TCvSubst -> Bool
`notElemTCvSubst` TCvSubst
kind_subst
Bool -> Bool -> Bool
&& Bool -> Bool
not (TyVar
v TyVar -> VarSet -> Bool
`elemVarSet` VarSet
ki_subst_range))
[TyVar]
tkvs'
(TCvSubst
subst, [TyVar]
_) = HasCallStack => TCvSubst -> [TyVar] -> (TCvSubst, [TyVar])
substTyVarBndrs TCvSubst
kind_subst [TyVar]
unmapped_tkvs
final_tc_args :: [PredType]
final_tc_args = HasCallStack => TCvSubst -> [PredType] -> [PredType]
substTys TCvSubst
subst [PredType]
tc_args'
final_cls_tys :: [PredType]
final_cls_tys = HasCallStack => TCvSubst -> [PredType] -> [PredType]
substTys TCvSubst
subst [PredType]
cls_tys'
final_mb_deriv_strat :: Maybe (DerivStrategy GhcTc)
final_mb_deriv_strat = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall p (pass :: Pass).
(p ~ GhcPass pass) =>
(XViaStrategy p -> XViaStrategy p)
-> DerivStrategy p -> DerivStrategy p
mapDerivStrategy (HasCallStack => TCvSubst -> PredType -> PredType
substTy TCvSubst
subst))
Maybe (DerivStrategy GhcTc)
mb_deriv_strat'
final_tkvs :: [TyVar]
final_tkvs = [PredType] -> [TyVar]
tyCoVarsOfTypesWellScoped forall a b. (a -> b) -> a -> b
$
[PredType]
final_cls_tys forall a. [a] -> [a] -> [a]
++ [PredType]
final_tc_args
forall a. [a] -> [a] -> [a]
++ Maybe (DerivStrategy GhcTc) -> [PredType]
deriv_strat_tys Maybe (DerivStrategy GhcTc)
final_mb_deriv_strat
; let tkvs :: [TyVar]
tkvs = [TyVar] -> [TyVar]
scopedSort forall a b. (a -> b) -> a -> b
$ FV -> [TyVar]
fvVarList forall a b. (a -> b) -> a -> b
$
FV -> FV -> FV
unionFV ([PredType] -> FV
tyCoFVsOfTypes [PredType]
tc_args_to_keep)
([TyVar] -> FV
FV.mkFVs [TyVar]
deriv_tvs)
Just TCvSubst
kind_subst = Maybe TCvSubst
mb_match
([TyVar]
tkvs', [PredType]
cls_tys', [PredType]
tc_args', Maybe (DerivStrategy GhcTc)
mb_deriv_strat')
= TCvSubst
-> [TyVar]
-> [PredType]
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> ([TyVar], [PredType], [PredType], Maybe (DerivStrategy GhcTc))
propagate_subst TCvSubst
kind_subst [TyVar]
tkvs [PredType]
cls_tys
[PredType]
tc_args_to_keep Maybe (DerivStrategy GhcTc)
mb_deriv_strat
; ([TyVar]
final_tkvs, [PredType]
final_cls_tys, [PredType]
final_tc_args, Maybe (DerivStrategy GhcTc)
final_mb_deriv_strat) <-
case Maybe (DerivStrategy GhcTc)
mb_deriv_strat' of
Just (ViaStrategy XViaStrategy GhcTc
via_ty) -> do
let via_kind :: PredType
via_kind = HasDebugCallStack => PredType -> PredType
tcTypeKind XViaStrategy GhcTc
via_ty
inst_ty_kind :: PredType
inst_ty_kind
= HasDebugCallStack => PredType -> PredType
tcTypeKind (TyCon -> [PredType] -> PredType
mkTyConApp TyCon
tc [PredType]
tc_args')
via_match :: Maybe TCvSubst
via_match = PredType -> PredType -> Maybe TCvSubst
tcUnifyTy PredType
inst_ty_kind PredType
via_kind
Bool -> SDoc -> TcRn ()
checkTc (forall a. Maybe a -> Bool
isJust Maybe TCvSubst
via_match)
(Class -> PredType -> PredType -> PredType -> SDoc
derivingViaKindErr Class
cls PredType
inst_ty_kind XViaStrategy GhcTc
via_ty PredType
via_kind)
let Just TCvSubst
via_subst = Maybe TCvSubst
via_match
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ TCvSubst
-> [TyVar]
-> [PredType]
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> ([TyVar], [PredType], [PredType], Maybe (DerivStrategy GhcTc))
propagate_subst TCvSubst
via_subst [TyVar]
tkvs' [PredType]
cls_tys'
[PredType]
tc_args' Maybe (DerivStrategy GhcTc)
mb_deriv_strat'
Maybe (DerivStrategy GhcTc)
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TyVar]
tkvs', [PredType]
cls_tys', [PredType]
tc_args', Maybe (DerivStrategy GhcTc)
mb_deriv_strat')
; String -> SDoc -> TcRn ()
traceTc String
"deriveTyData 1" forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat
[ forall a. Outputable a => a -> SDoc
ppr Maybe (DerivStrategy GhcTc)
final_mb_deriv_strat, [TyVar] -> SDoc
pprTyVars [TyVar]
deriv_tvs, forall a. Outputable a => a -> SDoc
ppr TyCon
tc, forall a. Outputable a => a -> SDoc
ppr [PredType]
tc_args
, [TyVar] -> SDoc
pprTyVars ([PredType] -> [TyVar]
tyCoVarsOfTypesList [PredType]
tc_args)
, forall a. Outputable a => a -> SDoc
ppr Int
n_args_to_keep, forall a. Outputable a => a -> SDoc
ppr Int
n_args_to_drop
, forall a. Outputable a => a -> SDoc
ppr PredType
inst_ty_kind, forall a. Outputable a => a -> SDoc
ppr PredType
cls_arg_kind, forall a. Outputable a => a -> SDoc
ppr Maybe TCvSubst
mb_match
, forall a. Outputable a => a -> SDoc
ppr [PredType]
final_tc_args, forall a. Outputable a => a -> SDoc
ppr [PredType]
final_cls_tys ]
; String -> SDoc -> TcRn ()
traceTc String
"deriveTyData 2" forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat
[ forall a. Outputable a => a -> SDoc
ppr [TyVar]
final_tkvs ]
; let final_tc_app :: PredType
final_tc_app = TyCon -> [PredType] -> PredType
mkTyConApp TyCon
tc [PredType]
final_tc_args
final_cls_args :: [PredType]
final_cls_args = [PredType]
final_cls_tys forall a. [a] -> [a] -> [a]
++ [PredType
final_tc_app]
; Bool -> SDoc -> TcRn ()
checkTc (VarSet -> [PredType] -> Bool
allDistinctTyVars ([TyVar] -> VarSet
mkVarSet [TyVar]
final_tkvs) [PredType]
args_to_drop)
(Class -> [PredType] -> PredType -> SDoc
derivingEtaErr Class
cls [PredType]
final_cls_tys PredType
final_tc_app)
; UserTypeCtxt -> Class -> [PredType] -> TcRn ()
checkValidInstHead UserTypeCtxt
DerivClauseCtxt Class
cls [PredType]
final_cls_args
; EarlyDerivSpec
spec <- Maybe OverlapMode
-> [TyVar]
-> Class
-> [PredType]
-> DerivContext
-> Maybe (DerivStrategy GhcTc)
-> TcM EarlyDerivSpec
mkEqnHelp forall a. Maybe a
Nothing [TyVar]
final_tkvs Class
cls [PredType]
final_cls_args
(Maybe SrcSpan -> DerivContext
InferContext forall a. Maybe a
Nothing) Maybe (DerivStrategy GhcTc)
final_mb_deriv_strat
; String -> SDoc -> TcRn ()
traceTc String
"deriveTyData 3" (forall a. Outputable a => a -> SDoc
ppr EarlyDerivSpec
spec)
; forall (m :: * -> *) a. Monad m => a -> m a
return EarlyDerivSpec
spec }
mkEqnHelp :: Maybe OverlapMode
-> [TyVar]
-> Class -> [Type]
-> DerivContext
-> Maybe (DerivStrategy GhcTc)
-> TcRn EarlyDerivSpec
mkEqnHelp :: Maybe OverlapMode
-> [TyVar]
-> Class
-> [PredType]
-> DerivContext
-> Maybe (DerivStrategy GhcTc)
-> TcM EarlyDerivSpec
mkEqnHelp Maybe OverlapMode
overlap_mode [TyVar]
tvs Class
cls [PredType]
cls_args DerivContext
deriv_ctxt Maybe (DerivStrategy GhcTc)
deriv_strat = do
Bool
is_boot <- TcRn Bool
tcIsHsBootOrSig
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
is_boot forall a b. (a -> b) -> a -> b
$
SDoc -> TcRn ()
bale_out (String -> SDoc
text String
"Cannot derive instances in hs-boot files"
SDoc -> SDoc -> SDoc
$+$ String -> SDoc
text String
"Write an instance declaration instead")
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT DerivM EarlyDerivSpec
mk_eqn DerivEnv
deriv_env
where
deriv_env :: DerivEnv
deriv_env = DerivEnv { denv_overlap_mode :: Maybe OverlapMode
denv_overlap_mode = Maybe OverlapMode
overlap_mode
, denv_tvs :: [TyVar]
denv_tvs = [TyVar]
tvs
, denv_cls :: Class
denv_cls = Class
cls
, denv_inst_tys :: [PredType]
denv_inst_tys = [PredType]
cls_args
, denv_ctxt :: DerivContext
denv_ctxt = DerivContext
deriv_ctxt
, denv_strat :: Maybe (DerivStrategy GhcTc)
denv_strat = Maybe (DerivStrategy GhcTc)
deriv_strat }
bale_out :: SDoc -> TcRn ()
bale_out SDoc
msg = forall a. SDoc -> TcM a
failWithTc forall a b. (a -> b) -> a -> b
$ Bool
-> Class
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> SDoc
-> SDoc
derivingThingErr Bool
False Class
cls [PredType]
cls_args Maybe (DerivStrategy GhcTc)
deriv_strat SDoc
msg
mk_eqn :: DerivM EarlyDerivSpec
mk_eqn :: DerivM EarlyDerivSpec
mk_eqn = do
DerivEnv { denv_inst_tys :: DerivEnv -> [PredType]
denv_inst_tys = [PredType]
cls_args
, denv_strat :: DerivEnv -> Maybe (DerivStrategy GhcTc)
denv_strat = Maybe (DerivStrategy GhcTc)
mb_strat } <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
case Maybe (DerivStrategy GhcTc)
mb_strat of
Just (StockStrategy XStockStrategy GhcTc
_) -> do
([PredType]
cls_tys, PredType
inst_ty) <- [PredType] -> DerivM ([PredType], PredType)
expectNonNullaryClsArgs [PredType]
cls_args
DerivInstTys
dit <- [PredType] -> PredType -> DerivM DerivInstTys
expectAlgTyConApp [PredType]
cls_tys PredType
inst_ty
DerivInstTys -> DerivM EarlyDerivSpec
mk_eqn_stock DerivInstTys
dit
Just (AnyclassStrategy XAnyClassStrategy GhcTc
_) -> DerivM EarlyDerivSpec
mk_eqn_anyclass
Just (ViaStrategy XViaStrategy GhcTc
via_ty) -> do
([PredType]
cls_tys, PredType
inst_ty) <- [PredType] -> DerivM ([PredType], PredType)
expectNonNullaryClsArgs [PredType]
cls_args
[PredType] -> PredType -> PredType -> DerivM EarlyDerivSpec
mk_eqn_via [PredType]
cls_tys PredType
inst_ty XViaStrategy GhcTc
via_ty
Just (NewtypeStrategy XNewtypeStrategy GhcTc
_) -> do
([PredType]
cls_tys, PredType
inst_ty) <- [PredType] -> DerivM ([PredType], PredType)
expectNonNullaryClsArgs [PredType]
cls_args
DerivInstTys
dit <- [PredType] -> PredType -> DerivM DerivInstTys
expectAlgTyConApp [PredType]
cls_tys PredType
inst_ty
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (TyCon -> Bool
isNewTyCon (DerivInstTys -> TyCon
dit_rep_tc DerivInstTys
dit)) forall a b. (a -> b) -> a -> b
$
forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
False SDoc
gndNonNewtypeErr
Bool -> DerivInstTys -> DerivM EarlyDerivSpec
mkNewTypeEqn Bool
True DerivInstTys
dit
Maybe (DerivStrategy GhcTc)
Nothing -> DerivM EarlyDerivSpec
mk_eqn_no_strategy
expectNonNullaryClsArgs :: [Type] -> DerivM ([Type], Type)
expectNonNullaryClsArgs :: [PredType] -> DerivM ([PredType], PredType)
expectNonNullaryClsArgs [PredType]
inst_tys =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
False SDoc
derivingNullaryErr) forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
forall a. [a] -> Maybe ([a], a)
snocView [PredType]
inst_tys
expectAlgTyConApp :: [Type]
-> Type
-> DerivM DerivInstTys
expectAlgTyConApp :: [PredType] -> PredType -> DerivM DerivInstTys
expectAlgTyConApp [PredType]
cls_tys PredType
inst_ty = do
FamInstEnvs
fam_envs <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift TcM FamInstEnvs
tcGetFamInstEnvs
case FamInstEnvs -> [PredType] -> PredType -> Maybe DerivInstTys
mk_deriv_inst_tys_maybe FamInstEnvs
fam_envs [PredType]
cls_tys PredType
inst_ty of
Maybe DerivInstTys
Nothing -> forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
False forall a b. (a -> b) -> a -> b
$
String -> SDoc
text String
"The last argument of the instance must be a"
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"data or newtype application"
Just DerivInstTys
dit -> do DerivInstTys -> ReaderT DerivEnv TcRn ()
expectNonDataFamTyCon DerivInstTys
dit
forall (f :: * -> *) a. Applicative f => a -> f a
pure DerivInstTys
dit
expectNonDataFamTyCon :: DerivInstTys -> DerivM ()
expectNonDataFamTyCon :: DerivInstTys -> ReaderT DerivEnv TcRn ()
expectNonDataFamTyCon (DerivInstTys { dit_tc :: DerivInstTys -> TyCon
dit_tc = TyCon
tc
, dit_tc_args :: DerivInstTys -> [PredType]
dit_tc_args = [PredType]
tc_args
, dit_rep_tc :: DerivInstTys -> TyCon
dit_rep_tc = TyCon
rep_tc }) =
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TyCon -> Bool
isDataFamilyTyCon TyCon
rep_tc) forall a b. (a -> b) -> a -> b
$
forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
False forall a b. (a -> b) -> a -> b
$
String -> SDoc
text String
"No family instance for" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (TyCon -> [PredType] -> SDoc
pprTypeApp TyCon
tc [PredType]
tc_args)
mk_deriv_inst_tys_maybe :: FamInstEnvs
-> [Type] -> Type -> Maybe DerivInstTys
mk_deriv_inst_tys_maybe :: FamInstEnvs -> [PredType] -> PredType -> Maybe DerivInstTys
mk_deriv_inst_tys_maybe FamInstEnvs
fam_envs [PredType]
cls_tys PredType
inst_ty =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (TyCon, [PredType]) -> DerivInstTys
lookup forall a b. (a -> b) -> a -> b
$ HasCallStack => PredType -> Maybe (TyCon, [PredType])
tcSplitTyConApp_maybe PredType
inst_ty
where
lookup :: (TyCon, [Type]) -> DerivInstTys
lookup :: (TyCon, [PredType]) -> DerivInstTys
lookup (TyCon
tc, [PredType]
tc_args) =
let (TyCon
rep_tc, [PredType]
rep_tc_args, Coercion
_co) = FamInstEnvs -> TyCon -> [PredType] -> (TyCon, [PredType], Coercion)
tcLookupDataFamInst FamInstEnvs
fam_envs TyCon
tc [PredType]
tc_args
in DerivInstTys { dit_cls_tys :: [PredType]
dit_cls_tys = [PredType]
cls_tys
, dit_tc :: TyCon
dit_tc = TyCon
tc
, dit_tc_args :: [PredType]
dit_tc_args = [PredType]
tc_args
, dit_rep_tc :: TyCon
dit_rep_tc = TyCon
rep_tc
, dit_rep_tc_args :: [PredType]
dit_rep_tc_args = [PredType]
rep_tc_args }
mk_eqn_from_mechanism :: DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism :: DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism DerivSpecMechanism
mechanism
= do DerivEnv { denv_overlap_mode :: DerivEnv -> Maybe OverlapMode
denv_overlap_mode = Maybe OverlapMode
overlap_mode
, denv_tvs :: DerivEnv -> [TyVar]
denv_tvs = [TyVar]
tvs
, denv_cls :: DerivEnv -> Class
denv_cls = Class
cls
, denv_inst_tys :: DerivEnv -> [PredType]
denv_inst_tys = [PredType]
inst_tys
, denv_ctxt :: DerivEnv -> DerivContext
denv_ctxt = DerivContext
deriv_ctxt } <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
DerivSpecMechanism -> ReaderT DerivEnv TcRn ()
doDerivInstErrorChecks1 DerivSpecMechanism
mechanism
SrcSpan
loc <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift TcRn SrcSpan
getSrcSpanM
Name
dfun_name <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ Class -> [PredType] -> SrcSpan -> TcM Name
newDFunName Class
cls [PredType]
inst_tys SrcSpan
loc
case DerivContext
deriv_ctxt of
InferContext Maybe SrcSpan
wildcard ->
do { ([ThetaOrigin]
inferred_constraints, [TyVar]
tvs', [PredType]
inst_tys')
<- DerivSpecMechanism -> DerivM ([ThetaOrigin], [TyVar], [PredType])
inferConstraints DerivSpecMechanism
mechanism
; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ DerivSpec [ThetaOrigin] -> EarlyDerivSpec
InferTheta forall a b. (a -> b) -> a -> b
$ DS
{ ds_loc :: SrcSpan
ds_loc = SrcSpan
loc
, ds_name :: Name
ds_name = Name
dfun_name, ds_tvs :: [TyVar]
ds_tvs = [TyVar]
tvs'
, ds_cls :: Class
ds_cls = Class
cls, ds_tys :: [PredType]
ds_tys = [PredType]
inst_tys'
, ds_theta :: [ThetaOrigin]
ds_theta = [ThetaOrigin]
inferred_constraints
, ds_overlap :: Maybe OverlapMode
ds_overlap = Maybe OverlapMode
overlap_mode
, ds_standalone_wildcard :: Maybe SrcSpan
ds_standalone_wildcard = Maybe SrcSpan
wildcard
, ds_mechanism :: DerivSpecMechanism
ds_mechanism = DerivSpecMechanism
mechanism } }
SupplyContext [PredType]
theta ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ DerivSpec [PredType] -> EarlyDerivSpec
GivenTheta forall a b. (a -> b) -> a -> b
$ DS
{ ds_loc :: SrcSpan
ds_loc = SrcSpan
loc
, ds_name :: Name
ds_name = Name
dfun_name, ds_tvs :: [TyVar]
ds_tvs = [TyVar]
tvs
, ds_cls :: Class
ds_cls = Class
cls, ds_tys :: [PredType]
ds_tys = [PredType]
inst_tys
, ds_theta :: [PredType]
ds_theta = [PredType]
theta
, ds_overlap :: Maybe OverlapMode
ds_overlap = Maybe OverlapMode
overlap_mode
, ds_standalone_wildcard :: Maybe SrcSpan
ds_standalone_wildcard = forall a. Maybe a
Nothing
, ds_mechanism :: DerivSpecMechanism
ds_mechanism = DerivSpecMechanism
mechanism }
mk_eqn_stock :: DerivInstTys
-> DerivM EarlyDerivSpec
mk_eqn_stock :: DerivInstTys -> DerivM EarlyDerivSpec
mk_eqn_stock dit :: DerivInstTys
dit@(DerivInstTys { dit_cls_tys :: DerivInstTys -> [PredType]
dit_cls_tys = [PredType]
cls_tys
, dit_tc :: DerivInstTys -> TyCon
dit_tc = TyCon
tc
, dit_rep_tc :: DerivInstTys -> TyCon
dit_rep_tc = TyCon
rep_tc })
= do DerivEnv { denv_cls :: DerivEnv -> Class
denv_cls = Class
cls
, denv_ctxt :: DerivEnv -> DerivContext
denv_ctxt = DerivContext
deriv_ctxt } <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
case DynFlags
-> DerivContext
-> Class
-> [PredType]
-> TyCon
-> TyCon
-> OriginativeDerivStatus
checkOriginativeSideConditions DynFlags
dflags DerivContext
deriv_ctxt Class
cls [PredType]
cls_tys
TyCon
tc TyCon
rep_tc of
CanDeriveStock SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
gen_fn -> DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism forall a b. (a -> b) -> a -> b
$
DerivSpecStock { dsm_stock_dit :: DerivInstTys
dsm_stock_dit = DerivInstTys
dit
, dsm_stock_gen_fn :: SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
dsm_stock_gen_fn = SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
gen_fn }
StockClassError SDoc
msg -> forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
False SDoc
msg
OriginativeDerivStatus
_ -> forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
False (Class -> SDoc
nonStdErr Class
cls)
mk_eqn_anyclass :: DerivM EarlyDerivSpec
mk_eqn_anyclass :: DerivM EarlyDerivSpec
mk_eqn_anyclass
= do DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
case DynFlags -> Validity
canDeriveAnyClass DynFlags
dflags of
Validity
IsValid -> DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism DerivSpecMechanism
DerivSpecAnyClass
NotValid SDoc
msg -> forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
False SDoc
msg
mk_eqn_newtype :: DerivInstTys
-> Type
-> DerivM EarlyDerivSpec
mk_eqn_newtype :: DerivInstTys -> PredType -> DerivM EarlyDerivSpec
mk_eqn_newtype DerivInstTys
dit PredType
rep_ty =
DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism forall a b. (a -> b) -> a -> b
$ DerivSpecNewtype { dsm_newtype_dit :: DerivInstTys
dsm_newtype_dit = DerivInstTys
dit
, dsm_newtype_rep_ty :: PredType
dsm_newtype_rep_ty = PredType
rep_ty }
mk_eqn_via :: [Type]
-> Type
-> Type
-> DerivM EarlyDerivSpec
mk_eqn_via :: [PredType] -> PredType -> PredType -> DerivM EarlyDerivSpec
mk_eqn_via [PredType]
cls_tys PredType
inst_ty PredType
via_ty =
DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism forall a b. (a -> b) -> a -> b
$ DerivSpecVia { dsm_via_cls_tys :: [PredType]
dsm_via_cls_tys = [PredType]
cls_tys
, dsm_via_inst_ty :: PredType
dsm_via_inst_ty = PredType
inst_ty
, dsm_via_ty :: PredType
dsm_via_ty = PredType
via_ty }
mk_eqn_no_strategy :: DerivM EarlyDerivSpec
mk_eqn_no_strategy :: DerivM EarlyDerivSpec
mk_eqn_no_strategy = do
DerivEnv { denv_cls :: DerivEnv -> Class
denv_cls = Class
cls
, denv_inst_tys :: DerivEnv -> [PredType]
denv_inst_tys = [PredType]
cls_args } <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
FamInstEnvs
fam_envs <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift TcM FamInstEnvs
tcGetFamInstEnvs
if | Just ([PredType]
cls_tys, PredType
inst_ty) <- forall a. [a] -> Maybe ([a], a)
snocView [PredType]
cls_args
, Just DerivInstTys
dit <- FamInstEnvs -> [PredType] -> PredType -> Maybe DerivInstTys
mk_deriv_inst_tys_maybe FamInstEnvs
fam_envs [PredType]
cls_tys PredType
inst_ty
-> if | TyCon -> Bool
isNewTyCon (DerivInstTys -> TyCon
dit_rep_tc DerivInstTys
dit)
-> Bool -> DerivInstTys -> DerivM EarlyDerivSpec
mkNewTypeEqn Bool
False DerivInstTys
dit
| Bool
otherwise
-> do
forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenIsJust (Class
-> Maybe
(SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name]))
hasStockDeriving Class
cls) forall a b. (a -> b) -> a -> b
$ \SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM
(Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs)),
[GenLocated SrcSpanAnnA (Sig GhcPs)], BagDerivStuff, [Name])
_ ->
DerivInstTys -> ReaderT DerivEnv TcRn ()
expectNonDataFamTyCon DerivInstTys
dit
DerivInstTys -> DerivM EarlyDerivSpec
mk_eqn_originative DerivInstTys
dit
| Bool
otherwise
-> DerivM EarlyDerivSpec
mk_eqn_anyclass
where
mk_eqn_originative :: DerivInstTys -> DerivM EarlyDerivSpec
mk_eqn_originative :: DerivInstTys -> DerivM EarlyDerivSpec
mk_eqn_originative dit :: DerivInstTys
dit@(DerivInstTys { dit_cls_tys :: DerivInstTys -> [PredType]
dit_cls_tys = [PredType]
cls_tys
, dit_tc :: DerivInstTys -> TyCon
dit_tc = TyCon
tc
, dit_rep_tc :: DerivInstTys -> TyCon
dit_rep_tc = TyCon
rep_tc }) = do
DerivEnv { denv_cls :: DerivEnv -> Class
denv_cls = Class
cls
, denv_ctxt :: DerivEnv -> DerivContext
denv_ctxt = DerivContext
deriv_ctxt } <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let dac_error :: SDoc -> SDoc
dac_error SDoc
msg
| TyCon -> Bool
isClassTyCon TyCon
rep_tc
= SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr TyCon
tc) SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"is a type class,"
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"and can only have a derived instance"
SDoc -> SDoc -> SDoc
$+$ String -> SDoc
text String
"if DeriveAnyClass is enabled"
| Bool
otherwise
= Class -> SDoc
nonStdErr Class
cls SDoc -> SDoc -> SDoc
$$ SDoc
msg
case DynFlags
-> DerivContext
-> Class
-> [PredType]
-> TyCon
-> TyCon
-> OriginativeDerivStatus
checkOriginativeSideConditions DynFlags
dflags DerivContext
deriv_ctxt Class
cls
[PredType]
cls_tys TyCon
tc TyCon
rep_tc of
NonDerivableClass SDoc
msg -> forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
False (SDoc -> SDoc
dac_error SDoc
msg)
StockClassError SDoc
msg -> forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
False SDoc
msg
CanDeriveStock SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
gen_fn -> DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism forall a b. (a -> b) -> a -> b
$
DerivSpecStock { dsm_stock_dit :: DerivInstTys
dsm_stock_dit = DerivInstTys
dit
, dsm_stock_gen_fn :: SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
dsm_stock_gen_fn = SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
gen_fn }
OriginativeDerivStatus
CanDeriveAnyClass -> DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism DerivSpecMechanism
DerivSpecAnyClass
mkNewTypeEqn :: Bool
-> DerivInstTys -> DerivM EarlyDerivSpec
mkNewTypeEqn :: Bool -> DerivInstTys -> DerivM EarlyDerivSpec
mkNewTypeEqn Bool
newtype_strat dit :: DerivInstTys
dit@(DerivInstTys { dit_cls_tys :: DerivInstTys -> [PredType]
dit_cls_tys = [PredType]
cls_tys
, dit_tc :: DerivInstTys -> TyCon
dit_tc = TyCon
tycon
, dit_rep_tc :: DerivInstTys -> TyCon
dit_rep_tc = TyCon
rep_tycon
, dit_rep_tc_args :: DerivInstTys -> [PredType]
dit_rep_tc_args = [PredType]
rep_tc_args })
= do DerivEnv { denv_cls :: DerivEnv -> Class
denv_cls = Class
cls
, denv_ctxt :: DerivEnv -> DerivContext
denv_ctxt = DerivContext
deriv_ctxt } <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let newtype_deriving :: Bool
newtype_deriving = Extension -> DynFlags -> Bool
xopt Extension
LangExt.GeneralizedNewtypeDeriving DynFlags
dflags
deriveAnyClass :: Bool
deriveAnyClass = Extension -> DynFlags -> Bool
xopt Extension
LangExt.DeriveAnyClass DynFlags
dflags
bale_out :: SDoc -> DerivM EarlyDerivSpec
bale_out = forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
newtype_deriving
non_std :: SDoc
non_std = Class -> SDoc
nonStdErr Class
cls
suggest_gnd :: SDoc
suggest_gnd = String -> SDoc
text String
"Try GeneralizedNewtypeDeriving for GHC's"
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"newtype-deriving extension"
nt_eta_arity :: Int
nt_eta_arity = TyCon -> Int
newTyConEtadArity TyCon
rep_tycon
rep_inst_ty :: PredType
rep_inst_ty = TyCon -> [PredType] -> PredType
newTyConInstRhs TyCon
rep_tycon [PredType]
rep_tc_args
might_be_newtype_derivable :: Bool
might_be_newtype_derivable
= Bool -> Bool
not (Class -> Bool
non_coercible_class Class
cls)
Bool -> Bool -> Bool
&& Bool
eta_ok
eta_ok :: Bool
eta_ok = [PredType]
rep_tc_args forall a. [a] -> Int -> Bool
`lengthAtLeast` Int
nt_eta_arity
cant_derive_err :: SDoc
cant_derive_err = Bool -> SDoc -> SDoc
ppUnless Bool
eta_ok SDoc
eta_msg
eta_msg :: SDoc
eta_msg = String -> SDoc
text String
"cannot eta-reduce the representation type enough"
MASSERT( cls_tys `lengthIs` (classArity cls - 1) )
if Bool
newtype_strat
then
if Bool
eta_ok Bool -> Bool -> Bool
&& Bool
newtype_deriving
then DerivInstTys -> PredType -> DerivM EarlyDerivSpec
mk_eqn_newtype DerivInstTys
dit PredType
rep_inst_ty
else SDoc -> DerivM EarlyDerivSpec
bale_out (SDoc
cant_derive_err SDoc -> SDoc -> SDoc
$$
if Bool
newtype_deriving then SDoc
empty else SDoc
suggest_gnd)
else
if Bool
might_be_newtype_derivable
Bool -> Bool -> Bool
&& ((Bool
newtype_deriving Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
deriveAnyClass)
Bool -> Bool -> Bool
|| Class -> Bool
std_class_via_coercible Class
cls)
then DerivInstTys -> PredType -> DerivM EarlyDerivSpec
mk_eqn_newtype DerivInstTys
dit PredType
rep_inst_ty
else case DynFlags
-> DerivContext
-> Class
-> [PredType]
-> TyCon
-> TyCon
-> OriginativeDerivStatus
checkOriginativeSideConditions DynFlags
dflags DerivContext
deriv_ctxt Class
cls [PredType]
cls_tys
TyCon
tycon TyCon
rep_tycon of
StockClassError SDoc
msg
| Bool
might_be_newtype_derivable Bool -> Bool -> Bool
&& Bool
newtype_deriving
-> DerivInstTys -> PredType -> DerivM EarlyDerivSpec
mk_eqn_newtype DerivInstTys
dit PredType
rep_inst_ty
| Bool
might_be_newtype_derivable Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
newtype_deriving
-> SDoc -> DerivM EarlyDerivSpec
bale_out (SDoc
msg SDoc -> SDoc -> SDoc
$$ SDoc
suggest_gnd)
| Bool
otherwise
-> SDoc -> DerivM EarlyDerivSpec
bale_out SDoc
msg
NonDerivableClass SDoc
_msg
| Bool
newtype_deriving -> SDoc -> DerivM EarlyDerivSpec
bale_out SDoc
cant_derive_err
| Bool
otherwise -> SDoc -> DerivM EarlyDerivSpec
bale_out (SDoc
non_std SDoc -> SDoc -> SDoc
$$ SDoc
suggest_gnd)
OriginativeDerivStatus
CanDeriveAnyClass -> do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
newtype_deriving Bool -> Bool -> Bool
&& Bool
deriveAnyClass) forall a b. (a -> b) -> a -> b
$
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall gbl lcl.
WarningFlag -> TcRnIf gbl lcl () -> TcRnIf gbl lcl ()
whenWOptM WarningFlag
Opt_WarnDerivingDefaults forall a b. (a -> b) -> a -> b
$
WarnReason -> SDoc -> TcRn ()
addWarnTc (WarningFlag -> WarnReason
Reason WarningFlag
Opt_WarnDerivingDefaults) forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
sep
[ String -> SDoc
text String
"Both DeriveAnyClass and"
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"GeneralizedNewtypeDeriving are enabled"
, String -> SDoc
text String
"Defaulting to the DeriveAnyClass strategy"
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"for instantiating" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr Class
cls
, String -> SDoc
text String
"Use DerivingStrategies to pick"
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"a different strategy"
]
DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism DerivSpecMechanism
DerivSpecAnyClass
CanDeriveStock SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
gen_fn -> DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism forall a b. (a -> b) -> a -> b
$
DerivSpecStock { dsm_stock_dit :: DerivInstTys
dsm_stock_dit = DerivInstTys
dit
, dsm_stock_gen_fn :: SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
dsm_stock_gen_fn = SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
gen_fn }
genInst :: DerivSpec theta
-> TcM (ThetaType -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])
genInst :: forall theta.
DerivSpec theta
-> TcM ([PredType] -> TcM (InstInfo GhcPs), BagDerivStuff, [Name])
genInst spec :: DerivSpec theta
spec@(DS { ds_tvs :: forall theta. DerivSpec theta -> [TyVar]
ds_tvs = [TyVar]
tvs, ds_mechanism :: forall theta. DerivSpec theta -> DerivSpecMechanism
ds_mechanism = DerivSpecMechanism
mechanism
, ds_tys :: forall theta. DerivSpec theta -> [PredType]
ds_tys = [PredType]
tys, ds_cls :: forall theta. DerivSpec theta -> Class
ds_cls = Class
clas, ds_loc :: forall theta. DerivSpec theta -> SrcSpan
ds_loc = SrcSpan
loc
, ds_standalone_wildcard :: forall theta. DerivSpec theta -> Maybe SrcSpan
ds_standalone_wildcard = Maybe SrcSpan
wildcard })
= do (Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
meth_binds, [GenLocated SrcSpanAnnA (Sig GhcPs)]
meth_sigs, BagDerivStuff
deriv_stuff, [Name]
unusedNames)
<- forall a. TcRn a -> TcRn a
set_span_and_ctxt forall a b. (a -> b) -> a -> b
$
DerivSpecMechanism
-> SrcSpan
-> Class
-> [PredType]
-> [TyVar]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
genDerivStuff DerivSpecMechanism
mechanism SrcSpan
loc Class
clas [PredType]
tys [TyVar]
tvs
let mk_inst_info :: [PredType] -> TcM (InstInfo GhcPs)
mk_inst_info [PredType]
theta = forall a. TcRn a -> TcRn a
set_span_and_ctxt forall a b. (a -> b) -> a -> b
$ do
ClsInst
inst_spec <- forall theta. [PredType] -> DerivSpec theta -> TcM ClsInst
newDerivClsInst [PredType]
theta DerivSpec theta
spec
Class
-> ClsInst
-> [PredType]
-> Maybe SrcSpan
-> DerivSpecMechanism
-> TcRn ()
doDerivInstErrorChecks2 Class
clas ClsInst
inst_spec [PredType]
theta Maybe SrcSpan
wildcard DerivSpecMechanism
mechanism
String -> SDoc -> TcRn ()
traceTc String
"newder" (forall a. Outputable a => a -> SDoc
ppr ClsInst
inst_spec)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ InstInfo
{ iSpec :: ClsInst
iSpec = ClsInst
inst_spec
, iBinds :: InstBindings GhcPs
iBinds = InstBindings
{ ib_binds :: LHsBinds GhcPs
ib_binds = Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
meth_binds
, ib_tyvars :: [Name]
ib_tyvars = forall a b. (a -> b) -> [a] -> [b]
map TyVar -> Name
Var.varName [TyVar]
tvs
, ib_pragmas :: [LSig GhcPs]
ib_pragmas = [GenLocated SrcSpanAnnA (Sig GhcPs)]
meth_sigs
, ib_extensions :: [Extension]
ib_extensions = [Extension]
extensions
, ib_derived :: Bool
ib_derived = Bool
True } }
forall (m :: * -> *) a. Monad m => a -> m a
return ([PredType] -> TcM (InstInfo GhcPs)
mk_inst_info, BagDerivStuff
deriv_stuff, [Name]
unusedNames)
where
extensions :: [LangExt.Extension]
extensions :: [Extension]
extensions
| DerivSpecMechanism -> Bool
isDerivSpecNewtype DerivSpecMechanism
mechanism Bool -> Bool -> Bool
|| DerivSpecMechanism -> Bool
isDerivSpecVia DerivSpecMechanism
mechanism
= [
Extension
LangExt.ImpredicativeTypes, Extension
LangExt.RankNTypes
, Extension
LangExt.InstanceSigs
]
| Bool
otherwise
= []
set_span_and_ctxt :: TcM a -> TcM a
set_span_and_ctxt :: forall a. TcRn a -> TcRn a
set_span_and_ctxt = forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SDoc -> TcM a -> TcM a
addErrCtxt (Class -> [PredType] -> SDoc
instDeclCtxt3 Class
clas [PredType]
tys)
doDerivInstErrorChecks1 :: DerivSpecMechanism -> DerivM ()
doDerivInstErrorChecks1 :: DerivSpecMechanism -> ReaderT DerivEnv TcRn ()
doDerivInstErrorChecks1 DerivSpecMechanism
mechanism =
case DerivSpecMechanism
mechanism of
DerivSpecStock{dsm_stock_dit :: DerivSpecMechanism -> DerivInstTys
dsm_stock_dit = DerivInstTys
dit}
-> DerivInstTys -> ReaderT DerivEnv TcRn ()
data_cons_in_scope_check DerivInstTys
dit
DerivSpecNewtype{dsm_newtype_dit :: DerivSpecMechanism -> DerivInstTys
dsm_newtype_dit = DerivInstTys
dit}
-> do ReaderT DerivEnv TcRn ()
atf_coerce_based_error_checks
DerivInstTys -> ReaderT DerivEnv TcRn ()
data_cons_in_scope_check DerivInstTys
dit
DerivSpecAnyClass{}
-> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
DerivSpecVia{}
-> ReaderT DerivEnv TcRn ()
atf_coerce_based_error_checks
where
data_cons_in_scope_check :: DerivInstTys -> DerivM ()
data_cons_in_scope_check :: DerivInstTys -> ReaderT DerivEnv TcRn ()
data_cons_in_scope_check (DerivInstTys { dit_tc :: DerivInstTys -> TyCon
dit_tc = TyCon
tc
, dit_rep_tc :: DerivInstTys -> TyCon
dit_rep_tc = TyCon
rep_tc }) = do
Bool
standalone <- DerivM Bool
isStandaloneDeriv
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
standalone forall a b. (a -> b) -> a -> b
$ do
let bale_out :: SDoc -> ReaderT DerivEnv TcRn ()
bale_out SDoc
msg = do SDoc
err <- DerivSpecMechanism -> SDoc -> DerivM SDoc
derivingThingErrMechanism DerivSpecMechanism
mechanism SDoc
msg
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a. SDoc -> TcM a
failWithTc SDoc
err
GlobalRdrEnv
rdr_env <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift TcRn GlobalRdrEnv
getGlobalRdrEnv
let data_con_names :: [Name]
data_con_names = forall a b. (a -> b) -> [a] -> [b]
map DataCon -> Name
dataConName (TyCon -> [DataCon]
tyConDataCons TyCon
rep_tc)
hidden_data_cons :: Bool
hidden_data_cons = Bool -> Bool
not (forall thing. NamedThing thing => thing -> Bool
isWiredIn TyCon
rep_tc) Bool -> Bool -> Bool
&&
(TyCon -> Bool
isAbstractTyCon TyCon
rep_tc Bool -> Bool -> Bool
||
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Name -> Bool
not_in_scope [Name]
data_con_names)
not_in_scope :: Name -> Bool
not_in_scope Name
dc = forall a. Maybe a -> Bool
isNothing (GlobalRdrEnv -> Name -> Maybe GlobalRdrElt
lookupGRE_Name GlobalRdrEnv
rdr_env Name
dc)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ GlobalRdrEnv -> TyCon -> TcRn ()
addUsedDataCons GlobalRdrEnv
rdr_env TyCon
rep_tc
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool -> Bool
not Bool
hidden_data_cons) forall a b. (a -> b) -> a -> b
$
SDoc -> ReaderT DerivEnv TcRn ()
bale_out forall a b. (a -> b) -> a -> b
$ TyCon -> SDoc
derivingHiddenErr TyCon
tc
atf_coerce_based_error_checks :: DerivM ()
atf_coerce_based_error_checks :: ReaderT DerivEnv TcRn ()
atf_coerce_based_error_checks = do
Class
cls <- forall (m :: * -> *) r a. Monad m => (r -> a) -> ReaderT r m a
asks DerivEnv -> Class
denv_cls
let bale_out :: SDoc -> ReaderT DerivEnv TcRn ()
bale_out SDoc
msg = do SDoc
err <- DerivSpecMechanism -> SDoc -> DerivM SDoc
derivingThingErrMechanism DerivSpecMechanism
mechanism SDoc
msg
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a. SDoc -> TcM a
failWithTc SDoc
err
cls_tyvars :: [TyVar]
cls_tyvars = Class -> [TyVar]
classTyVars Class
cls
ats_look_sensible :: Bool
ats_look_sensible
=
Bool
no_adfs
Bool -> Bool -> Bool
&& forall a. Maybe a -> Bool
isNothing Maybe TyCon
at_without_last_cls_tv
Bool -> Bool -> Bool
&& forall a. Maybe a -> Bool
isNothing Maybe TyCon
at_last_cls_tv_in_kinds
([TyCon]
adf_tcs, [TyCon]
atf_tcs) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition TyCon -> Bool
isDataFamilyTyCon [TyCon]
at_tcs
no_adfs :: Bool
no_adfs = forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TyCon]
adf_tcs
at_without_last_cls_tv :: Maybe TyCon
at_without_last_cls_tv
= forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\TyCon
tc -> TyVar
last_cls_tv forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` TyCon -> [TyVar]
tyConTyVars TyCon
tc) [TyCon]
atf_tcs
at_last_cls_tv_in_kinds :: Maybe TyCon
at_last_cls_tv_in_kinds
= forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\TyCon
tc -> forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (PredType -> Bool
at_last_cls_tv_in_kind forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyVar -> PredType
tyVarKind)
(TyCon -> [TyVar]
tyConTyVars TyCon
tc)
Bool -> Bool -> Bool
|| PredType -> Bool
at_last_cls_tv_in_kind (TyCon -> PredType
tyConResKind TyCon
tc)) [TyCon]
atf_tcs
at_last_cls_tv_in_kind :: PredType -> Bool
at_last_cls_tv_in_kind PredType
kind
= TyVar
last_cls_tv TyVar -> VarSet -> Bool
`elemVarSet` PredType -> VarSet
exactTyCoVarsOfType PredType
kind
at_tcs :: [TyCon]
at_tcs = Class -> [TyCon]
classATs Class
cls
last_cls_tv :: TyVar
last_cls_tv = ASSERT( notNull cls_tyvars )
forall a. [a] -> a
last [TyVar]
cls_tyvars
cant_derive_err :: SDoc
cant_derive_err
= [SDoc] -> SDoc
vcat [ Bool -> SDoc -> SDoc
ppUnless Bool
no_adfs SDoc
adfs_msg
, forall b a. b -> (a -> b) -> Maybe a -> b
maybe SDoc
empty TyCon -> SDoc
at_without_last_cls_tv_msg
Maybe TyCon
at_without_last_cls_tv
, forall b a. b -> (a -> b) -> Maybe a -> b
maybe SDoc
empty TyCon -> SDoc
at_last_cls_tv_in_kinds_msg
Maybe TyCon
at_last_cls_tv_in_kinds
]
adfs_msg :: SDoc
adfs_msg = String -> SDoc
text String
"the class has associated data types"
at_without_last_cls_tv_msg :: TyCon -> SDoc
at_without_last_cls_tv_msg TyCon
at_tc = SDoc -> Int -> SDoc -> SDoc
hang
(String -> SDoc
text String
"the associated type" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr TyCon
at_tc)
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"is not parameterized over the last type variable")
Int
2 (String -> SDoc
text String
"of the class" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr Class
cls))
at_last_cls_tv_in_kinds_msg :: TyCon -> SDoc
at_last_cls_tv_in_kinds_msg TyCon
at_tc = SDoc -> Int -> SDoc -> SDoc
hang
(String -> SDoc
text String
"the associated type" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr TyCon
at_tc)
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"contains the last type variable")
Int
2 (String -> SDoc
text String
"of the class" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr Class
cls)
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"in a kind, which is not (yet) allowed")
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
ats_look_sensible forall a b. (a -> b) -> a -> b
$ SDoc -> ReaderT DerivEnv TcRn ()
bale_out SDoc
cant_derive_err
doDerivInstErrorChecks2 :: Class -> ClsInst -> ThetaType -> Maybe SrcSpan
-> DerivSpecMechanism -> TcM ()
doDerivInstErrorChecks2 :: Class
-> ClsInst
-> [PredType]
-> Maybe SrcSpan
-> DerivSpecMechanism
-> TcRn ()
doDerivInstErrorChecks2 Class
clas ClsInst
clas_inst [PredType]
theta Maybe SrcSpan
wildcard DerivSpecMechanism
mechanism
= do { String -> SDoc -> TcRn ()
traceTc String
"doDerivInstErrorChecks2" (forall a. Outputable a => a -> SDoc
ppr ClsInst
clas_inst)
; DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
; Bool
xpartial_sigs <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.PartialTypeSignatures
; Bool
wpartial_sigs <- forall gbl lcl. WarningFlag -> TcRnIf gbl lcl Bool
woptM WarningFlag
Opt_WarnPartialTypeSignatures
; case Maybe SrcSpan
wildcard of
Maybe SrcSpan
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just SrcSpan
span -> forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
span forall a b. (a -> b) -> a -> b
$ do
Bool -> SDoc -> TcRn ()
checkTc Bool
xpartial_sigs (SDoc -> Int -> SDoc -> SDoc
hang SDoc
partial_sig_msg Int
2 SDoc
pts_suggestion)
WarnReason -> Bool -> SDoc -> TcRn ()
warnTc (WarningFlag -> WarnReason
Reason WarningFlag
Opt_WarnPartialTypeSignatures)
Bool
wpartial_sigs SDoc
partial_sig_msg
; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
exotic_mechanism Bool -> Bool -> Bool
&& Class -> Name
className Class
clas forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Name]
genericClassNames) forall a b. (a -> b) -> a -> b
$
do { Bool -> SDoc -> TcRn ()
failIfTc (DynFlags -> Bool
safeLanguageOn DynFlags
dflags) SDoc
gen_inst_err
; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (DynFlags -> Bool
safeInferOn DynFlags
dflags) (WarningMessages -> TcRn ()
recordUnsafeInfer forall a. Bag a
emptyBag) } }
where
exotic_mechanism :: Bool
exotic_mechanism = Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ DerivSpecMechanism -> Bool
isDerivSpecStock DerivSpecMechanism
mechanism
partial_sig_msg :: SDoc
partial_sig_msg = String -> SDoc
text String
"Found type wildcard" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (Char -> SDoc
char Char
'_')
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"standing for" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes ([PredType] -> SDoc
pprTheta [PredType]
theta)
pts_suggestion :: SDoc
pts_suggestion
= String -> SDoc
text String
"To use the inferred type, enable PartialTypeSignatures"
gen_inst_err :: SDoc
gen_inst_err = String -> SDoc
text String
"Generic instances can only be derived in"
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"Safe Haskell using the stock strategy."
derivingThingFailWith :: Bool
-> SDoc
-> DerivM a
derivingThingFailWith :: forall a. Bool -> SDoc -> DerivM a
derivingThingFailWith Bool
newtype_deriving SDoc
msg = do
SDoc
err <- Bool -> SDoc -> DerivM SDoc
derivingThingErrM Bool
newtype_deriving SDoc
msg
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a. SDoc -> TcM a
failWithTc SDoc
err
genDerivStuff :: DerivSpecMechanism -> SrcSpan -> Class
-> [Type] -> [TyVar]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
genDerivStuff :: DerivSpecMechanism
-> SrcSpan
-> Class
-> [PredType]
-> [TyVar]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
genDerivStuff DerivSpecMechanism
mechanism SrcSpan
loc Class
clas [PredType]
inst_tys [TyVar]
tyvars
= case DerivSpecMechanism
mechanism of
DerivSpecNewtype { dsm_newtype_rep_ty :: DerivSpecMechanism -> PredType
dsm_newtype_rep_ty = PredType
rhs_ty}
-> PredType
-> TcM
(Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs)),
[GenLocated SrcSpanAnnA (Sig GhcPs)], BagDerivStuff, [Name])
gen_newtype_or_via PredType
rhs_ty
DerivSpecStock { dsm_stock_dit :: DerivSpecMechanism -> DerivInstTys
dsm_stock_dit = DerivInstTys
{ dit_rep_tc :: DerivInstTys -> TyCon
dit_rep_tc = TyCon
rep_tc
, dit_rep_tc_args :: DerivInstTys -> [PredType]
dit_rep_tc_args = [PredType]
rep_tc_args
}
, dsm_stock_gen_fn :: DerivSpecMechanism
-> SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
dsm_stock_gen_fn = SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
gen_fn }
-> SrcSpan
-> TyCon
-> [PredType]
-> [PredType]
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff, [Name])
gen_fn SrcSpan
loc TyCon
rep_tc [PredType]
rep_tc_args [PredType]
inst_tys
DerivSpecMechanism
DerivSpecAnyClass -> do
let mini_env :: VarEnv PredType
mini_env = forall a. [(TyVar, a)] -> VarEnv a
mkVarEnv (Class -> [TyVar]
classTyVars Class
clas forall a b. [a] -> [b] -> [(a, b)]
`zip` [PredType]
inst_tys)
mini_subst :: TCvSubst
mini_subst = InScopeSet -> VarEnv PredType -> TCvSubst
mkTvSubst (VarSet -> InScopeSet
mkInScopeSet ([TyVar] -> VarSet
mkVarSet [TyVar]
tyvars)) VarEnv PredType
mini_env
DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
[[FamInst]]
tyfam_insts <-
ASSERT2( isValid (canDeriveAnyClass dflags)
, ppr "genDerivStuff: bad derived class" <+> ppr clas )
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (SrcSpan -> TCvSubst -> Uses -> ClassATItem -> TcM [FamInst]
tcATDefault SrcSpan
loc TCvSubst
mini_subst Uses
emptyNameSet)
(Class -> [ClassATItem]
classATItems Class
clas)
forall (m :: * -> *) a. Monad m => a -> m a
return ( forall a. Bag a
emptyBag, []
, forall a. [a] -> Bag a
listToBag (forall a b. (a -> b) -> [a] -> [b]
map FamInst -> DerivStuff
DerivFamInst (forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[FamInst]]
tyfam_insts))
, [] )
DerivSpecVia{dsm_via_ty :: DerivSpecMechanism -> PredType
dsm_via_ty = PredType
via_ty}
-> PredType
-> TcM
(Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs)),
[GenLocated SrcSpanAnnA (Sig GhcPs)], BagDerivStuff, [Name])
gen_newtype_or_via PredType
via_ty
where
gen_newtype_or_via :: PredType
-> TcM
(Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs)),
[GenLocated SrcSpanAnnA (Sig GhcPs)], BagDerivStuff, [Name])
gen_newtype_or_via PredType
ty = do
(Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
binds, [GenLocated SrcSpanAnnA (Sig GhcPs)]
sigs, BagDerivStuff
faminsts) <- SrcSpan
-> Class
-> [TyVar]
-> [PredType]
-> PredType
-> TcM (LHsBinds GhcPs, [LSig GhcPs], BagDerivStuff)
gen_Newtype_binds SrcSpan
loc Class
clas [TyVar]
tyvars [PredType]
inst_tys PredType
ty
forall (m :: * -> *) a. Monad m => a -> m a
return (Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
binds, [GenLocated SrcSpanAnnA (Sig GhcPs)]
sigs, BagDerivStuff
faminsts, [])
nonUnaryErr :: LHsSigType GhcRn -> SDoc
nonUnaryErr :: LHsSigType GhcRn -> SDoc
nonUnaryErr LHsSigType GhcRn
ct = SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr LHsSigType GhcRn
ct)
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"is not a unary constraint, as expected by a deriving clause"
nonStdErr :: Class -> SDoc
nonStdErr :: Class -> SDoc
nonStdErr Class
cls =
SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr Class
cls)
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"is not a stock derivable class (Eq, Show, etc.)"
gndNonNewtypeErr :: SDoc
gndNonNewtypeErr :: SDoc
gndNonNewtypeErr =
String -> SDoc
text String
"GeneralizedNewtypeDeriving cannot be used on non-newtypes"
derivingNullaryErr :: SDoc
derivingNullaryErr :: SDoc
derivingNullaryErr = String -> SDoc
text String
"Cannot derive instances for nullary classes"
derivingKindErr :: TyCon -> Class -> [Type] -> Kind -> Bool -> SDoc
derivingKindErr :: TyCon -> Class -> [PredType] -> PredType -> Bool -> SDoc
derivingKindErr TyCon
tc Class
cls [PredType]
cls_tys PredType
cls_kind Bool
enough_args
= [SDoc] -> SDoc
sep [ SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"Cannot derive well-kinded instance of form"
SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (Class -> [PredType] -> SDoc
pprClassPred Class
cls [PredType]
cls_tys
SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
parens (forall a. Outputable a => a -> SDoc
ppr TyCon
tc SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"...")))
Int
2 SDoc
gen1_suggestion
, Int -> SDoc -> SDoc
nest Int
2 (String -> SDoc
text String
"Class" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr Class
cls)
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"expects an argument of kind"
SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (PredType -> SDoc
pprKind PredType
cls_kind))
]
where
gen1_suggestion :: SDoc
gen1_suggestion | Class
cls forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
gen1ClassKey Bool -> Bool -> Bool
&& Bool
enough_args
= String -> SDoc
text String
"(Perhaps you intended to use PolyKinds)"
| Bool
otherwise = SDoc
Outputable.empty
derivingViaKindErr :: Class -> Kind -> Type -> Kind -> SDoc
derivingViaKindErr :: Class -> PredType -> PredType -> PredType -> SDoc
derivingViaKindErr Class
cls PredType
cls_kind PredType
via_ty PredType
via_kind
= SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"Cannot derive instance via" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (PredType -> SDoc
pprType PredType
via_ty))
Int
2 (String -> SDoc
text String
"Class" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr Class
cls)
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"expects an argument of kind"
SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (PredType -> SDoc
pprKind PredType
cls_kind) SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
','
SDoc -> SDoc -> SDoc
$+$ String -> SDoc
text String
"but" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (PredType -> SDoc
pprType PredType
via_ty)
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"has kind" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (PredType -> SDoc
pprKind PredType
via_kind))
derivingEtaErr :: Class -> [Type] -> Type -> SDoc
derivingEtaErr :: Class -> [PredType] -> PredType -> SDoc
derivingEtaErr Class
cls [PredType]
cls_tys PredType
inst_ty
= [SDoc] -> SDoc
sep [String -> SDoc
text String
"Cannot eta-reduce to an instance of form",
Int -> SDoc -> SDoc
nest Int
2 (String -> SDoc
text String
"instance (...) =>"
SDoc -> SDoc -> SDoc
<+> Class -> [PredType] -> SDoc
pprClassPred Class
cls ([PredType]
cls_tys forall a. [a] -> [a] -> [a]
++ [PredType
inst_ty]))]
derivingThingErr :: Bool -> Class -> [Type]
-> Maybe (DerivStrategy GhcTc) -> SDoc -> SDoc
derivingThingErr :: Bool
-> Class
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> SDoc
-> SDoc
derivingThingErr Bool
newtype_deriving Class
cls [PredType]
cls_args Maybe (DerivStrategy GhcTc)
mb_strat SDoc
why
= Bool
-> Class
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> SDoc
-> SDoc
-> SDoc
derivingThingErr' Bool
newtype_deriving Class
cls [PredType]
cls_args Maybe (DerivStrategy GhcTc)
mb_strat
(forall b a. b -> (a -> b) -> Maybe a -> b
maybe SDoc
empty forall a. DerivStrategy a -> SDoc
derivStrategyName Maybe (DerivStrategy GhcTc)
mb_strat) SDoc
why
derivingThingErrM :: Bool -> SDoc -> DerivM SDoc
derivingThingErrM :: Bool -> SDoc -> DerivM SDoc
derivingThingErrM Bool
newtype_deriving SDoc
why
= do DerivEnv { denv_cls :: DerivEnv -> Class
denv_cls = Class
cls
, denv_inst_tys :: DerivEnv -> [PredType]
denv_inst_tys = [PredType]
cls_args
, denv_strat :: DerivEnv -> Maybe (DerivStrategy GhcTc)
denv_strat = Maybe (DerivStrategy GhcTc)
mb_strat } <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Bool
-> Class
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> SDoc
-> SDoc
derivingThingErr Bool
newtype_deriving Class
cls [PredType]
cls_args Maybe (DerivStrategy GhcTc)
mb_strat SDoc
why
derivingThingErrMechanism :: DerivSpecMechanism -> SDoc -> DerivM SDoc
derivingThingErrMechanism :: DerivSpecMechanism -> SDoc -> DerivM SDoc
derivingThingErrMechanism DerivSpecMechanism
mechanism SDoc
why
= do DerivEnv { denv_cls :: DerivEnv -> Class
denv_cls = Class
cls
, denv_inst_tys :: DerivEnv -> [PredType]
denv_inst_tys = [PredType]
cls_args
, denv_strat :: DerivEnv -> Maybe (DerivStrategy GhcTc)
denv_strat = Maybe (DerivStrategy GhcTc)
mb_strat } <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Bool
-> Class
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> SDoc
-> SDoc
-> SDoc
derivingThingErr' (DerivSpecMechanism -> Bool
isDerivSpecNewtype DerivSpecMechanism
mechanism) Class
cls [PredType]
cls_args Maybe (DerivStrategy GhcTc)
mb_strat
(forall a. DerivStrategy a -> SDoc
derivStrategyName forall a b. (a -> b) -> a -> b
$ DerivSpecMechanism -> DerivStrategy GhcTc
derivSpecMechanismToStrategy DerivSpecMechanism
mechanism) SDoc
why
derivingThingErr' :: Bool -> Class -> [Type]
-> Maybe (DerivStrategy GhcTc) -> SDoc -> SDoc -> SDoc
derivingThingErr' :: Bool
-> Class
-> [PredType]
-> Maybe (DerivStrategy GhcTc)
-> SDoc
-> SDoc
-> SDoc
derivingThingErr' Bool
newtype_deriving Class
cls [PredType]
cls_args Maybe (DerivStrategy GhcTc)
mb_strat SDoc
strat_msg SDoc
why
= [SDoc] -> SDoc
sep [(SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"Can't make a derived instance of")
Int
2 (SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr PredType
pred) SDoc -> SDoc -> SDoc
<+> SDoc
via_mechanism)
SDoc -> SDoc -> SDoc
$$ Int -> SDoc -> SDoc
nest Int
2 SDoc
extra) SDoc -> SDoc -> SDoc
<> SDoc
colon,
Int -> SDoc -> SDoc
nest Int
2 SDoc
why]
where
strat_used :: Bool
strat_used = forall a. Maybe a -> Bool
isJust Maybe (DerivStrategy GhcTc)
mb_strat
extra :: SDoc
extra | Bool -> Bool
not Bool
strat_used, Bool
newtype_deriving
= String -> SDoc
text String
"(even with cunning GeneralizedNewtypeDeriving)"
| Bool
otherwise = SDoc
empty
pred :: PredType
pred = Class -> [PredType] -> PredType
mkClassPred Class
cls [PredType]
cls_args
via_mechanism :: SDoc
via_mechanism | Bool
strat_used
= String -> SDoc
text String
"with the" SDoc -> SDoc -> SDoc
<+> SDoc
strat_msg SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"strategy"
| Bool
otherwise
= SDoc
empty
derivingHiddenErr :: TyCon -> SDoc
derivingHiddenErr :: TyCon -> SDoc
derivingHiddenErr TyCon
tc
= SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"The data constructors of" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr TyCon
tc) SDoc -> SDoc -> SDoc
<+> PtrString -> SDoc
ptext (String -> PtrString
sLit String
"are not all in scope"))
Int
2 (String -> SDoc
text String
"so you cannot derive an instance for it")
standaloneCtxt :: LHsSigWcType GhcRn -> SDoc
standaloneCtxt :: LHsSigWcType GhcRn -> SDoc
standaloneCtxt LHsSigWcType GhcRn
ty = SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"In the stand-alone deriving instance for")
Int
2 (SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr LHsSigWcType GhcRn
ty))