{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module GHC.Tc.Deriv ( tcDeriving, DerivInfo(..) ) where
import GHC.Prelude
import GHC.Hs
import GHC.Driver.Session
import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Monad
import GHC.Tc.Instance.Family
import GHC.Tc.Types.Origin
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( 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.Panic.Plain
import GHC.Utils.Logger
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 ThetaSpec)
| GivenTheta (DerivSpec ThetaType)
splitEarlyDerivSpec :: [EarlyDerivSpec]
-> ([DerivSpec ThetaSpec], [DerivSpec ThetaType])
splitEarlyDerivSpec :: [EarlyDerivSpec] -> ([DerivSpec ThetaSpec], [DerivSpec [Type]])
splitEarlyDerivSpec [] = ([],[])
splitEarlyDerivSpec (InferTheta DerivSpec ThetaSpec
spec : [EarlyDerivSpec]
specs) =
case [EarlyDerivSpec] -> ([DerivSpec ThetaSpec], [DerivSpec [Type]])
splitEarlyDerivSpec [EarlyDerivSpec]
specs of ([DerivSpec ThetaSpec]
is, [DerivSpec [Type]]
gs) -> (DerivSpec ThetaSpec
spec forall a. a -> [a] -> [a]
: [DerivSpec ThetaSpec]
is, [DerivSpec [Type]]
gs)
splitEarlyDerivSpec (GivenTheta DerivSpec [Type]
spec : [EarlyDerivSpec]
specs) =
case [EarlyDerivSpec] -> ([DerivSpec ThetaSpec], [DerivSpec [Type]])
splitEarlyDerivSpec [EarlyDerivSpec]
specs of ([DerivSpec ThetaSpec]
is, [DerivSpec [Type]]
gs) -> ([DerivSpec ThetaSpec]
is, DerivSpec [Type]
spec forall a. a -> [a] -> [a]
: [DerivSpec [Type]]
gs)
instance Outputable EarlyDerivSpec where
ppr :: EarlyDerivSpec -> SDoc
ppr (InferTheta DerivSpec ThetaSpec
spec) = forall a. Outputable a => a -> SDoc
ppr DerivSpec ThetaSpec
spec forall doc. IsLine doc => doc -> doc -> doc
<+> forall doc. IsLine doc => String -> doc
text String
"(Infer)"
ppr (GivenTheta DerivSpec [Type]
spec) = forall a. Outputable a => a -> SDoc
ppr DerivSpec [Type]
spec forall doc. IsLine doc => doc -> doc -> doc
<+> forall doc. IsLine doc => String -> doc
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 ThetaSpec]
infer_specs, [DerivSpec [Type]]
given_specs) = [EarlyDerivSpec] -> ([DerivSpec ThetaSpec], [DerivSpec [Type]])
splitEarlyDerivSpec [EarlyDerivSpec]
early_specs
; [FamInst]
famInsts1 <- forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable f) =>
(a -> m [b]) -> f a -> m [b]
concatMapM forall theta. DerivSpec theta -> TcM [FamInst]
genFamInsts [DerivSpec [Type]]
given_specs
; [FamInst]
famInsts2 <- forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable f) =>
(a -> m [b]) -> f a -> m [b]
concatMapM forall theta. DerivSpec theta -> TcM [FamInst]
genFamInsts [DerivSpec ThetaSpec]
infer_specs
; let famInsts :: [FamInst]
famInsts = [FamInst]
famInsts1 forall a. [a] -> [a] -> [a]
++ [FamInst]
famInsts2
; Logger
logger <- forall (m :: * -> *). HasLogger m => m Logger
getLogger
; forall a. [FamInst] -> TcM a -> TcM a
tcExtendLocalFamInstEnv [FamInst]
famInsts forall a b. (a -> b) -> a -> b
$
do { [(InstInfo GhcPs, Bag AuxBindSpec, [Name])]
given_inst_binds <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM DerivSpec [Type] -> TcM (InstInfo GhcPs, Bag AuxBindSpec, [Name])
genInstBinds [DerivSpec [Type]]
given_specs
; let given_inst_infos :: [InstInfo GhcPs]
given_inst_infos = forall a b. (a -> b) -> [a] -> [b]
map forall a b c. (a, b, c) -> a
fstOf3 [(InstInfo GhcPs, Bag AuxBindSpec, [Name])]
given_inst_binds
; [DerivSpec [Type]]
final_infer_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]
given_inst_infos) forall a b. (a -> b) -> a -> b
$
[DerivSpec ThetaSpec] -> TcM [DerivSpec [Type]]
simplifyInstanceContexts [DerivSpec ThetaSpec]
infer_specs
; [(InstInfo GhcPs, Bag AuxBindSpec, [Name])]
infer_inst_binds <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM DerivSpec [Type] -> TcM (InstInfo GhcPs, Bag AuxBindSpec, [Name])
genInstBinds [DerivSpec [Type]]
final_infer_specs
; let ([InstInfo GhcPs]
_, [Bag AuxBindSpec]
aux_specs, [[Name]]
fvs) = forall a b c. [(a, b, c)] -> ([a], [b], [c])
unzip3 ([(InstInfo GhcPs, Bag AuxBindSpec, [Name])]
given_inst_binds forall a. [a] -> [a] -> [a]
++ [(InstInfo GhcPs, Bag AuxBindSpec, [Name])]
infer_inst_binds)
; SrcSpan
loc <- TcRn SrcSpan
getSrcSpanM
; let aux_binds :: Bag (LHsBind GhcPs, LSig GhcPs)
aux_binds = SrcSpan -> Bag AuxBindSpec -> Bag (LHsBind GhcPs, LSig GhcPs)
genAuxBinds SrcSpan
loc (forall a. [Bag a] -> Bag a
unionManyBags [Bag AuxBindSpec]
aux_specs)
; let infer_inst_infos :: [InstInfo GhcPs]
infer_inst_infos = forall a b. (a -> b) -> [a] -> [b]
map forall a b c. (a, b, c) -> a
fstOf3 [(InstInfo GhcPs, Bag AuxBindSpec, [Name])]
infer_inst_binds
; let inst_infos :: [InstInfo GhcPs]
inst_infos = [InstInfo GhcPs]
given_inst_infos forall a. [a] -> [a] -> [a]
++ [InstInfo GhcPs]
infer_inst_infos
; (Bag (InstInfo GhcRn)
inst_info, HsValBinds GhcRn
rn_aux_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)
aux_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 -> DumpFlag -> String -> DumpFormat -> SDoc -> IO ()
putDumpFileMaybe Logger
logger DumpFlag
Opt_D_dump_deriv String
"Derived instances"
DumpFormat
FormatHaskell
(Bag (InstInfo GhcRn) -> HsValBinds GhcRn -> [FamInst] -> SDoc
ddump_deriving Bag (InstInfo GhcRn)
inst_info HsValBinds GhcRn
rn_aux_binds [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_aux_binds) } }
where
ddump_deriving :: Bag (InstInfo GhcRn) -> HsValBinds GhcRn
-> [FamInst]
-> SDoc
ddump_deriving :: Bag (InstInfo GhcRn) -> HsValBinds GhcRn -> [FamInst] -> SDoc
ddump_deriving Bag (InstInfo GhcRn)
inst_infos HsValBinds GhcRn
extra_binds [FamInst]
famInsts
= SDoc -> Int -> SDoc -> SDoc
hang (forall doc. IsLine doc => String -> doc
text String
"Derived class instances:")
Int
2 (forall doc. IsDoc doc => [doc] -> doc
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 forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsLine doc => String -> doc
text String
"") (forall a. Bag a -> [a]
bagToList Bag (InstInfo GhcRn)
inst_infos))
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall a. Outputable a => a -> SDoc
ppr HsValBinds GhcRn
extra_binds)
forall doc. IsDoc doc => doc -> doc -> doc
$$ SDoc -> SDoc -> SDoc
hangP (forall doc. IsLine doc => String -> doc
text String
"Derived type family instances:")
(forall doc. IsDoc doc => [doc] -> doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map FamInst -> SDoc
pprRepTy [FamInst]
famInsts))
hangP :: SDoc -> SDoc -> SDoc
hangP SDoc
s SDoc
x = forall doc. IsLine doc => String -> doc
text String
"" forall doc. IsDoc doc => doc -> doc -> doc
$$ SDoc -> Int -> SDoc -> SDoc
hang SDoc
s Int
2 SDoc
x
pprRepTy :: FamInst -> SDoc
pprRepTy :: FamInst -> SDoc
pprRepTy fi :: FamInst
fi@(FamInst { fi_tys :: FamInst -> [Type]
fi_tys = [Type]
lhs })
= forall doc. IsLine doc => String -> doc
text String
"type" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr (TyCon -> [Type] -> Type
mkTyConApp (FamInst -> TyCon
famInstTyCon FamInst
fi) [Type]
lhs) forall doc. IsLine doc => doc -> doc -> doc
<+>
forall doc. IsLine doc => doc
equals forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr Type
rhs
where rhs :: Type
rhs = FamInst -> Type
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" (forall doc. IsDoc doc => [doc] -> doc
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 forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsLine doc => String -> doc
text String
"") [InstInfo GhcPs]
inst_infos))
; let (Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
aux_binds, Bag (GenLocated SrcSpanAnnA (Sig GhcPs))
aux_sigs) = forall a b. Bag (a, b) -> (Bag a, Bag b)
unzipBag Bag (LHsBind GhcPs, LSig GhcPs)
bagBinds
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 SrcAnn NoEpAnns
_ (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
$ forall doc. IsDoc doc => [doc] -> doc
vcat
[ forall doc. IsLine doc => String -> doc
text String
"tvs" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [TyVar]
tvs
, forall doc. IsLine doc => String -> doc
text String
"scoped_tvs" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [(Name, TyVar)]
scoped_tvs
, forall doc. IsLine doc => String -> doc
text String
"tc" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr TyCon
tc
, forall doc. IsLine doc => String -> doc
text String
"tys" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [Type]
tys
, forall doc. IsLine doc => String -> doc
text String
"mb_lderiv_strat" forall doc. IsLine doc => doc -> doc -> doc
<+> 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 (SrcAnn NoEpAnns) (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
-> [Type]
-> Maybe (LDerivStrategy GhcTc)
-> [TyVar]
-> LHsSigType GhcRn
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe EarlyDerivSpec)
derivePred TyCon
tc [Type]
tys Maybe (GenLocated (SrcAnn NoEpAnns) (DerivStrategy GhcTc))
mb_lderiv_strat' [TyVar]
via_tvs) [LHsSigType GhcRn]
deriv_preds
where
tvs :: [TyVar]
tvs = TyCon -> [TyVar]
tyConTyVars TyCon
rep_tc
(TyCon
tc, [Type]
tys) = case TyCon -> Maybe (TyCon, [Type], CoAxiom Unbranched)
tyConFamInstSig_maybe TyCon
rep_tc of
Just (TyCon
fam_tc, [Type]
pats, CoAxiom Unbranched
_) -> (TyCon
fam_tc, [Type]
pats)
Maybe (TyCon, [Type], CoAxiom Unbranched)
_ -> (TyCon
rep_tc, [TyVar] -> [Type]
mkTyVarTys [TyVar]
tvs)
derivePred :: TyCon -> [Type] -> Maybe (LDerivStrategy GhcTc) -> [TyVar]
-> LHsSigType GhcRn -> TcM (Maybe EarlyDerivSpec)
derivePred :: TyCon
-> [Type]
-> Maybe (LDerivStrategy GhcTc)
-> [TyVar]
-> LHsSigType GhcRn
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe EarlyDerivSpec)
derivePred TyCon
tc [Type]
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
$ forall doc. IsDoc doc => [doc] -> doc
vcat
[ forall doc. IsLine doc => String -> doc
text String
"tc" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr TyCon
tc
, forall doc. IsLine doc => String -> doc
text String
"tys" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [Type]
tys
, forall doc. IsLine doc => String -> doc
text String
"deriv_pred" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr LHsSigType GhcRn
deriv_pred
, forall doc. IsLine doc => String -> doc
text String
"mb_lderiv_strat" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr Maybe (LDerivStrategy GhcTc)
mb_lderiv_strat
, forall doc. IsLine doc => String -> doc
text String
"via_tvs" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [TyVar]
via_tvs ]
([TyVar]
cls_tvs, Class
cls, [Type]
cls_tys, [Type]
cls_arg_kinds) <- LHsSigType GhcRn -> TcM ([TyVar], Class, [Type], [Type])
tcHsDeriv LHsSigType GhcRn
deriv_pred
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Type]
cls_arg_kinds forall a. [a] -> Int -> Bool
`lengthIsNot` Int
1) forall a b. (a -> b) -> a -> b
$
forall a. TcRnMessage -> TcM a
failWithTc (LHsSigType GhcRn -> TcRnMessage
TcRnNonUnaryTypeclassConstraint LHsSigType GhcRn
deriv_pred)
let [Type
cls_arg_kind] = [Type]
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
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> [TyVar]
-> Class
-> [Type]
-> Type
-> TcM EarlyDerivSpec
deriveTyData TyCon
tc [Type]
tys Maybe (DerivStrategy GhcTc)
mb_deriv_strat
[TyVar]
deriv_tvs Class
cls [Type]
cls_tys Type
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
$
forall doc. IsDoc doc => [doc] -> doc
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 (SrcAnn NoEpAnns) (DerivStrategy GhcTc))
mb_lderiv_strat, [TyVar]
via_tvs) <- Maybe (LDerivStrategy GhcRn)
-> TcM (Maybe (LDerivStrategy GhcTc), [TyVar])
tcDerivStrategy Maybe (LDerivStrategy GhcRn)
mb_lderiv_strat
; String -> SDoc -> TcRn ()
traceTc String
"Deriving strategy (standalone deriving) 2" forall a b. (a -> b) -> a -> b
$
forall doc. IsDoc doc => [doc] -> doc
vcat [forall a. Outputable a => a -> SDoc
ppr Maybe (GenLocated (SrcAnn NoEpAnns) (DerivStrategy GhcTc))
mb_lderiv_strat, forall a. Outputable a => a -> SDoc
ppr [TyVar]
via_tvs]
; ([TyVar]
cls_tvs, DerivContext
deriv_ctxt, Class
cls, [Type]
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, [Type])
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 (SrcAnn NoEpAnns) (DerivStrategy GhcTc))
mb_lderiv_strat
tvs :: [TyVar]
tvs = [TyVar]
via_tvs forall a. [a] -> [a] -> [a]
++ [TyVar]
cls_tvs
; ([TyVar]
tvs', DerivContext
deriv_ctxt', [Type]
inst_tys', Maybe (DerivStrategy GhcTc)
mb_deriv_strat') <-
case Maybe (DerivStrategy GhcTc)
mb_deriv_strat of
Just (ViaStrategy XViaStrategy GhcTc
via_ty)
| Just Type
inst_ty <- forall a. [a] -> Maybe a
lastMaybe [Type]
inst_tys
-> do
let via_kind :: Type
via_kind = HasDebugCallStack => Type -> Type
typeKind XViaStrategy GhcTc
via_ty
inst_ty_kind :: Type
inst_ty_kind = HasDebugCallStack => Type -> Type
typeKind Type
inst_ty
mb_match :: Maybe Subst
mb_match = Type -> Type -> Maybe Subst
tcUnifyTy Type
inst_ty_kind Type
via_kind
Bool -> TcRnMessage -> TcRn ()
checkTc (forall a. Maybe a -> Bool
isJust Maybe Subst
mb_match)
(Class
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> TcRnMessage
TcRnCannotDeriveInstance Class
cls forall a. Monoid a => a
mempty forall a. Maybe a
Nothing UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving forall a b. (a -> b) -> a -> b
$
Type -> Type -> Type -> DeriveInstanceErrReason
DerivErrDerivingViaWrongKind Type
inst_ty_kind XViaStrategy GhcTc
via_ty Type
via_kind)
let Just Subst
kind_subst = Maybe Subst
mb_match
ki_subst_range :: VarSet
ki_subst_range = Subst -> VarSet
getSubstRangeTyCoFVs Subst
kind_subst
unmapped_tkvs :: [TyVar]
unmapped_tkvs = forall a. (a -> Bool) -> [a] -> [a]
filter (\TyVar
v -> TyVar
v TyVar -> Subst -> Bool
`notElemSubst` Subst
kind_subst
Bool -> Bool -> Bool
&& Bool -> Bool
not (TyVar
v TyVar -> VarSet -> Bool
`elemVarSet` VarSet
ki_subst_range))
[TyVar]
tvs
(Subst
subst, [TyVar]
_) = HasDebugCallStack => Subst -> [TyVar] -> (Subst, [TyVar])
substTyVarBndrs Subst
kind_subst [TyVar]
unmapped_tkvs
(DerivContext
final_deriv_ctxt, [Type]
final_deriv_ctxt_tys)
= case DerivContext
deriv_ctxt of
InferContext Maybe SrcSpan
wc -> (Maybe SrcSpan -> DerivContext
InferContext Maybe SrcSpan
wc, [])
SupplyContext [Type]
theta ->
let final_theta :: [Type]
final_theta = HasDebugCallStack => Subst -> [Type] -> [Type]
substTheta Subst
subst [Type]
theta
in ([Type] -> DerivContext
SupplyContext [Type]
final_theta, [Type]
final_theta)
final_inst_tys :: [Type]
final_inst_tys = HasDebugCallStack => Subst -> [Type] -> [Type]
substTys Subst
subst [Type]
inst_tys
final_via_ty :: Type
final_via_ty = HasDebugCallStack => Subst -> Type -> Type
substTy Subst
subst XViaStrategy GhcTc
via_ty
final_tvs :: [TyVar]
final_tvs = [Type] -> [TyVar]
tyCoVarsOfTypesWellScoped forall a b. (a -> b) -> a -> b
$
[Type]
final_deriv_ctxt_tys forall a. [a] -> [a] -> [a]
++ [Type]
final_inst_tys
forall a. [a] -> [a] -> [a]
++ [Type
final_via_ty]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ( [TyVar]
final_tvs, DerivContext
final_deriv_ctxt, [Type]
final_inst_tys
, forall a. a -> Maybe a
Just (forall pass. XViaStrategy pass -> DerivStrategy pass
ViaStrategy Type
final_via_ty) )
Maybe (DerivStrategy GhcTc)
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TyVar]
tvs, DerivContext
deriv_ctxt, [Type]
inst_tys, Maybe (DerivStrategy GhcTc)
mb_deriv_strat)
; String -> SDoc -> TcRn ()
traceTc String
"Standalone deriving;" forall a b. (a -> b) -> a -> b
$ forall doc. IsDoc doc => [doc] -> doc
vcat
[ forall doc. IsLine doc => String -> doc
text String
"tvs':" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [TyVar]
tvs'
, forall doc. IsLine doc => String -> doc
text String
"mb_deriv_strat':" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr Maybe (DerivStrategy GhcTc)
mb_deriv_strat'
, forall doc. IsLine doc => String -> doc
text String
"deriv_ctxt':" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr DerivContext
deriv_ctxt'
, forall doc. IsLine doc => String -> doc
text String
"cls:" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr Class
cls
, forall doc. IsLine doc => String -> doc
text String
"inst_tys':" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [Type]
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
-> [Type]
-> 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 [Type]
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, [Type])
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 Type
dfun_ty <- UserTypeCtxt -> LHsSigType GhcRn -> TcM Type
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, [Type]
_theta, Class
cls, [Type]
inst_tys) = Type -> ([TyVar], [Type], Class, [Type])
tcSplitDFunTy Type
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, [Type]
inst_tys)
| Bool
otherwise
= do Type
dfun_ty <- UserTypeCtxt -> LHsSigType GhcRn -> TcM Type
tcHsClsInstType UserTypeCtxt
ctxt LHsSigType GhcRn
deriv_ty
let ([TyVar]
tvs, [Type]
theta, Class
cls, [Type]
inst_tys) = Type -> ([TyVar], [Type], Class, [Type])
tcSplitDFunTy Type
dfun_ty
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TyVar]
tvs, [Type] -> DerivContext
SupplyContext [Type]
theta, Class
cls, [Type]
inst_tys)
warnUselessTypeable :: TcM ()
warnUselessTypeable :: TcRn ()
warnUselessTypeable = TcRnMessage -> TcRn ()
addDiagnosticTc TcRnMessage
TcRnUselessTypeable
deriveTyData :: TyCon -> [Type]
-> Maybe (DerivStrategy GhcTc)
-> [TyVar]
-> Class
-> [Type]
-> Kind
-> TcM EarlyDerivSpec
deriveTyData :: TyCon
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> [TyVar]
-> Class
-> [Type]
-> Type
-> TcM EarlyDerivSpec
deriveTyData TyCon
tc [Type]
tc_args Maybe (DerivStrategy GhcTc)
mb_deriv_strat [TyVar]
deriv_tvs Class
cls [Type]
cls_tys Type
cls_arg_kind
= do {
let ([Scaled Type]
arg_kinds, Type
_) = Type -> ([Scaled Type], Type)
splitFunTys Type
cls_arg_kind
n_args_to_drop :: Int
n_args_to_drop = forall (t :: * -> *) a. Foldable t => t a -> Int
length [Scaled Type]
arg_kinds
n_args_to_keep :: Int
n_args_to_keep = forall (t :: * -> *) a. Foldable t => t a -> Int
length [Type]
tc_args forall a. Num a => a -> a -> a
- Int
n_args_to_drop
([Type]
tc_args_to_keep, [Type]
args_to_drop)
= forall a. Int -> [a] -> ([a], [a])
splitAt Int
n_args_to_keep [Type]
tc_args
inst_ty_kind :: Type
inst_ty_kind = HasDebugCallStack => Type -> Type
typeKind (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tc_args_to_keep)
mb_match :: Maybe Subst
mb_match = Type -> Type -> Maybe Subst
tcUnifyTy Type
inst_ty_kind Type
cls_arg_kind
enough_args :: Bool
enough_args = Int
n_args_to_keep forall a. Ord a => a -> a -> Bool
>= Int
0
; String -> SDoc -> TcRn ()
traceTc String
"deriveTyData" forall a b. (a -> b) -> a -> b
$
forall doc. IsDoc doc => [doc] -> doc
vcat [ forall doc. IsLine doc => String -> doc
text String
"class:" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr Class
cls forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc
dcolon forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr (TyCon -> Type
tyConKind (Class -> TyCon
classTyCon Class
cls))
, forall doc. IsLine doc => String -> doc
text String
"cls_tys:" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [Type]
cls_tys
, forall doc. IsLine doc => String -> doc
text String
"tycon:" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr TyCon
tc forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc
dcolon forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr (TyCon -> Type
tyConKind TyCon
tc)
, forall doc. IsLine doc => String -> doc
text String
"cls_arg:" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tc_args_to_keep) forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc
dcolon forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr Type
inst_ty_kind
, forall doc. IsLine doc => String -> doc
text String
"cls_arg_kind:" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr Type
cls_arg_kind ]
; Bool -> TcRnMessage -> TcRn ()
checkTc (Bool
enough_args Bool -> Bool -> Bool
&& forall a. Maybe a -> Bool
isJust Maybe Subst
mb_match)
(Class
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> TcRnMessage
TcRnCannotDeriveInstance Class
cls [Type]
cls_tys forall a. Maybe a
Nothing UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving forall a b. (a -> b) -> a -> b
$
TyCon -> Type -> Int -> DeriveInstanceErrReason
DerivErrNotWellKinded TyCon
tc Type
cls_arg_kind Int
n_args_to_keep)
; let
deriv_strat_tys :: Maybe (DerivStrategy GhcTc) -> [Type]
deriv_strat_tys :: Maybe (DerivStrategy GhcTc) -> [Type]
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 :: Subst
-> [TyVar]
-> [Type]
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> ([TyVar], [Type], [Type], Maybe (DerivStrategy GhcTc))
propagate_subst Subst
kind_subst [TyVar]
tkvs' [Type]
cls_tys' [Type]
tc_args' Maybe (DerivStrategy GhcTc)
mb_deriv_strat'
= ([TyVar]
final_tkvs, [Type]
final_cls_tys, [Type]
final_tc_args, Maybe (DerivStrategy GhcTc)
final_mb_deriv_strat)
where
ki_subst_range :: VarSet
ki_subst_range = Subst -> VarSet
getSubstRangeTyCoFVs Subst
kind_subst
unmapped_tkvs :: [TyVar]
unmapped_tkvs = forall a. (a -> Bool) -> [a] -> [a]
filter (\TyVar
v -> TyVar
v TyVar -> Subst -> Bool
`notElemSubst` Subst
kind_subst
Bool -> Bool -> Bool
&& Bool -> Bool
not (TyVar
v TyVar -> VarSet -> Bool
`elemVarSet` VarSet
ki_subst_range))
[TyVar]
tkvs'
(Subst
subst, [TyVar]
_) = HasDebugCallStack => Subst -> [TyVar] -> (Subst, [TyVar])
substTyVarBndrs Subst
kind_subst [TyVar]
unmapped_tkvs
final_tc_args :: [Type]
final_tc_args = HasDebugCallStack => Subst -> [Type] -> [Type]
substTys Subst
subst [Type]
tc_args'
final_cls_tys :: [Type]
final_cls_tys = HasDebugCallStack => Subst -> [Type] -> [Type]
substTys Subst
subst [Type]
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 (HasDebugCallStack => Subst -> Type -> Type
substTy Subst
subst))
Maybe (DerivStrategy GhcTc)
mb_deriv_strat'
final_tkvs :: [TyVar]
final_tkvs = [Type] -> [TyVar]
tyCoVarsOfTypesWellScoped forall a b. (a -> b) -> a -> b
$
[Type]
final_cls_tys forall a. [a] -> [a] -> [a]
++ [Type]
final_tc_args
forall a. [a] -> [a] -> [a]
++ Maybe (DerivStrategy GhcTc) -> [Type]
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 ([Type] -> FV
tyCoFVsOfTypes [Type]
tc_args_to_keep)
([TyVar] -> FV
FV.mkFVs [TyVar]
deriv_tvs)
Just Subst
kind_subst = Maybe Subst
mb_match
([TyVar]
tkvs', [Type]
cls_tys', [Type]
tc_args', Maybe (DerivStrategy GhcTc)
mb_deriv_strat')
= Subst
-> [TyVar]
-> [Type]
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> ([TyVar], [Type], [Type], Maybe (DerivStrategy GhcTc))
propagate_subst Subst
kind_subst [TyVar]
tkvs [Type]
cls_tys
[Type]
tc_args_to_keep Maybe (DerivStrategy GhcTc)
mb_deriv_strat
; ([TyVar]
final_tkvs, [Type]
final_cls_tys, [Type]
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 :: Type
via_kind = HasDebugCallStack => Type -> Type
typeKind XViaStrategy GhcTc
via_ty
inst_ty_kind :: Type
inst_ty_kind
= HasDebugCallStack => Type -> Type
typeKind (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tc_args')
via_match :: Maybe Subst
via_match = Type -> Type -> Maybe Subst
tcUnifyTy Type
inst_ty_kind Type
via_kind
Bool -> TcRnMessage -> TcRn ()
checkTc (forall a. Maybe a -> Bool
isJust Maybe Subst
via_match)
(Class
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> TcRnMessage
TcRnCannotDeriveInstance Class
cls forall a. Monoid a => a
mempty forall a. Maybe a
Nothing UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving forall a b. (a -> b) -> a -> b
$
Type -> Type -> Type -> DeriveInstanceErrReason
DerivErrDerivingViaWrongKind Type
inst_ty_kind XViaStrategy GhcTc
via_ty Type
via_kind)
let Just Subst
via_subst = Maybe Subst
via_match
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Subst
-> [TyVar]
-> [Type]
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> ([TyVar], [Type], [Type], Maybe (DerivStrategy GhcTc))
propagate_subst Subst
via_subst [TyVar]
tkvs' [Type]
cls_tys'
[Type]
tc_args' Maybe (DerivStrategy GhcTc)
mb_deriv_strat'
Maybe (DerivStrategy GhcTc)
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TyVar]
tkvs', [Type]
cls_tys', [Type]
tc_args', Maybe (DerivStrategy GhcTc)
mb_deriv_strat')
; String -> SDoc -> TcRn ()
traceTc String
"deriveTyData 1" forall a b. (a -> b) -> a -> b
$ forall doc. IsDoc doc => [doc] -> doc
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 [Type]
tc_args
, [TyVar] -> SDoc
pprTyVars ([Type] -> [TyVar]
tyCoVarsOfTypesList [Type]
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 Type
inst_ty_kind, forall a. Outputable a => a -> SDoc
ppr Type
cls_arg_kind, forall a. Outputable a => a -> SDoc
ppr Maybe Subst
mb_match
, forall a. Outputable a => a -> SDoc
ppr [Type]
final_tc_args, forall a. Outputable a => a -> SDoc
ppr [Type]
final_cls_tys ]
; String -> SDoc -> TcRn ()
traceTc String
"deriveTyData 2" forall a b. (a -> b) -> a -> b
$ forall doc. IsDoc doc => [doc] -> doc
vcat
[ forall a. Outputable a => a -> SDoc
ppr [TyVar]
final_tkvs ]
; let final_tc_app :: Type
final_tc_app = TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
final_tc_args
final_cls_args :: [Type]
final_cls_args = [Type]
final_cls_tys forall a. [a] -> [a] -> [a]
++ [Type
final_tc_app]
; Bool -> TcRnMessage -> TcRn ()
checkTc (VarSet -> [Type] -> Bool
allDistinctTyVars ([TyVar] -> VarSet
mkVarSet [TyVar]
final_tkvs) [Type]
args_to_drop)
(Class
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> TcRnMessage
TcRnCannotDeriveInstance Class
cls [Type]
final_cls_tys forall a. Maybe a
Nothing UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving forall a b. (a -> b) -> a -> b
$
Type -> DeriveInstanceErrReason
DerivErrNoEtaReduce Type
final_tc_app)
; UserTypeCtxt -> Class -> [Type] -> TcRn ()
checkValidInstHead UserTypeCtxt
DerivClauseCtxt Class
cls [Type]
final_cls_args
; EarlyDerivSpec
spec <- Maybe OverlapMode
-> [TyVar]
-> Class
-> [Type]
-> DerivContext
-> Maybe (DerivStrategy GhcTc)
-> TcM EarlyDerivSpec
mkEqnHelp forall a. Maybe a
Nothing [TyVar]
final_tkvs Class
cls [Type]
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
-> [Type]
-> DerivContext
-> Maybe (DerivStrategy GhcTc)
-> TcM EarlyDerivSpec
mkEqnHelp Maybe OverlapMode
overlap_mode [TyVar]
tvs Class
cls [Type]
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
$ DeriveInstanceErrReason -> TcRn ()
bale_out DeriveInstanceErrReason
DerivErrBootFileFound
let pred :: Type
pred = Class -> [Type] -> Type
mkClassPred Class
cls [Type]
cls_args
SkolemInfo
skol_info <- forall (m :: * -> *). MonadIO m => SkolemInfoAnon -> m SkolemInfo
mkSkolemInfo (Type -> SkolemInfoAnon
DerivSkol Type
pred)
([TyVar]
tvs', [Type]
cls_args', Maybe (DerivStrategy GhcTc)
deriv_strat') <-
SkolemInfo
-> DerivContext
-> TcM ([TyVar], [Type], Maybe (DerivStrategy GhcTc))
skolemise_when_inferring_context SkolemInfo
skol_info DerivContext
deriv_ctxt
let 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 :: [Type]
denv_inst_tys = [Type]
cls_args'
, denv_ctxt :: DerivContext
denv_ctxt = DerivContext
deriv_ctxt
, denv_skol_info :: SkolemInfo
denv_skol_info = SkolemInfo
skol_info
, denv_strat :: Maybe (DerivStrategy GhcTc)
denv_strat = Maybe (DerivStrategy GhcTc)
deriv_strat' }
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT DerivM EarlyDerivSpec
mk_eqn DerivEnv
deriv_env
where
skolemise_when_inferring_context ::
SkolemInfo -> DerivContext
-> TcM ([TcTyVar], [TcType], Maybe (DerivStrategy GhcTc))
skolemise_when_inferring_context :: SkolemInfo
-> DerivContext
-> TcM ([TyVar], [Type], Maybe (DerivStrategy GhcTc))
skolemise_when_inferring_context SkolemInfo
skol_info DerivContext
deriv_ctxt =
case DerivContext
deriv_ctxt of
InferContext{} -> do
(Subst
skol_subst, [TyVar]
tvs') <- SkolemInfo -> [TyVar] -> TcM (Subst, [TyVar])
tcInstSkolTyVars SkolemInfo
skol_info [TyVar]
tvs
let cls_args' :: [Type]
cls_args' = HasDebugCallStack => Subst -> [Type] -> [Type]
substTys Subst
skol_subst [Type]
cls_args
deriv_strat' :: Maybe (DerivStrategy GhcTc)
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 (HasDebugCallStack => Subst -> Type -> Type
substTy Subst
skol_subst))
Maybe (DerivStrategy GhcTc)
deriv_strat
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TyVar]
tvs', [Type]
cls_args', Maybe (DerivStrategy GhcTc)
deriv_strat')
SupplyContext{} -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TyVar]
tvs, [Type]
cls_args, Maybe (DerivStrategy GhcTc)
deriv_strat)
bale_out :: DeriveInstanceErrReason -> TcRn ()
bale_out =
forall a. TcRnMessage -> TcM a
failWithTc forall b c a. (b -> c) -> (a -> b) -> a -> c
. Class
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> TcRnMessage
TcRnCannotDeriveInstance Class
cls [Type]
cls_args Maybe (DerivStrategy GhcTc)
deriv_strat UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving
mk_eqn :: DerivM EarlyDerivSpec
mk_eqn :: DerivM EarlyDerivSpec
mk_eqn = do
DerivEnv { denv_inst_tys :: DerivEnv -> [Type]
denv_inst_tys = [Type]
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
([Type]
cls_tys, Type
inst_ty) <- [Type] -> DerivM ([Type], Type)
expectNonNullaryClsArgs [Type]
cls_args
DerivInstTys
dit <- [Type] -> Type -> DerivM DerivInstTys
expectAlgTyConApp [Type]
cls_tys Type
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
([Type]
cls_tys, Type
inst_ty) <- [Type] -> DerivM ([Type], Type)
expectNonNullaryClsArgs [Type]
cls_args
[Type] -> Type -> Type -> DerivM EarlyDerivSpec
mk_eqn_via [Type]
cls_tys Type
inst_ty XViaStrategy GhcTc
via_ty
Just (NewtypeStrategy XNewtypeStrategy GhcTc
_) -> do
([Type]
cls_tys, Type
inst_ty) <- [Type] -> DerivM ([Type], Type)
expectNonNullaryClsArgs [Type]
cls_args
DerivInstTys
dit <- [Type] -> Type -> DerivM DerivInstTys
expectAlgTyConApp [Type]
cls_tys Type
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.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving DeriveInstanceErrReason
DerivErrGNDUsedOnData
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 :: [Type] -> DerivM ([Type], Type)
expectNonNullaryClsArgs [Type]
inst_tys =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving DeriveInstanceErrReason
DerivErrNullaryClasses) forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
forall a. [a] -> Maybe ([a], a)
snocView [Type]
inst_tys
expectAlgTyConApp :: [Type]
-> Type
-> DerivM DerivInstTys
expectAlgTyConApp :: [Type] -> Type -> DerivM DerivInstTys
expectAlgTyConApp [Type]
cls_tys Type
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 -> [Type] -> Type -> Maybe DerivInstTys
mk_deriv_inst_tys_maybe FamInstEnvs
fam_envs [Type]
cls_tys Type
inst_ty of
Maybe DerivInstTys
Nothing -> forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving DeriveInstanceErrReason
DerivErrLastArgMustBeApp
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 -> [Type]
dit_tc_args = [Type]
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.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving forall a b. (a -> b) -> a -> b
$
TyCon -> [Type] -> DeriveInstanceErrReason
DerivErrNoFamilyInstance TyCon
tc [Type]
tc_args
mk_deriv_inst_tys_maybe :: FamInstEnvs
-> [Type] -> Type -> Maybe DerivInstTys
mk_deriv_inst_tys_maybe :: FamInstEnvs -> [Type] -> Type -> Maybe DerivInstTys
mk_deriv_inst_tys_maybe FamInstEnvs
fam_envs [Type]
cls_tys Type
inst_ty =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (TyCon, [Type]) -> DerivInstTys
lookup forall a b. (a -> b) -> a -> b
$ HasCallStack => Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe Type
inst_ty
where
lookup :: (TyCon, [Type]) -> DerivInstTys
lookup :: (TyCon, [Type]) -> DerivInstTys
lookup (TyCon
tc, [Type]
tc_args) =
let (TyCon
rep_tc, [Type]
rep_tc_args, Coercion
_co) = FamInstEnvs -> TyCon -> [Type] -> (TyCon, [Type], Coercion)
tcLookupDataFamInst FamInstEnvs
fam_envs TyCon
tc [Type]
tc_args
dc_inst_arg_env :: DataConEnv [Type]
dc_inst_arg_env = TyCon -> [Type] -> DataConEnv [Type]
buildDataConInstArgEnv TyCon
rep_tc [Type]
rep_tc_args
in DerivInstTys { dit_cls_tys :: [Type]
dit_cls_tys = [Type]
cls_tys
, dit_tc :: TyCon
dit_tc = TyCon
tc
, dit_tc_args :: [Type]
dit_tc_args = [Type]
tc_args
, dit_rep_tc :: TyCon
dit_rep_tc = TyCon
rep_tc
, dit_rep_tc_args :: [Type]
dit_rep_tc_args = [Type]
rep_tc_args
, dit_dc_inst_arg_env :: DataConEnv [Type]
dit_dc_inst_arg_env = DataConEnv [Type]
dc_inst_arg_env }
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 -> [Type]
denv_inst_tys = [Type]
inst_tys
, denv_ctxt :: DerivEnv -> DerivContext
denv_ctxt = DerivContext
deriv_ctxt
, denv_skol_info :: DerivEnv -> SkolemInfo
denv_skol_info = SkolemInfo
skol_info } <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
UserTypeCtxt
user_ctxt <- DerivM UserTypeCtxt
askDerivUserTypeCtxt
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 -> [Type] -> SrcSpan -> TcM Name
newDFunName Class
cls [Type]
inst_tys SrcSpan
loc
case DerivContext
deriv_ctxt of
InferContext Maybe SrcSpan
wildcard ->
do { (ThetaSpec
inferred_constraints, [TyVar]
tvs', [Type]
inst_tys', DerivSpecMechanism
mechanism')
<- DerivSpecMechanism
-> DerivM (ThetaSpec, [TyVar], [Type], DerivSpecMechanism)
inferConstraints DerivSpecMechanism
mechanism
; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ DerivSpec ThetaSpec -> 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 :: [Type]
ds_tys = [Type]
inst_tys'
, ds_theta :: ThetaSpec
ds_theta = ThetaSpec
inferred_constraints
, ds_skol_info :: SkolemInfo
ds_skol_info = SkolemInfo
skol_info
, ds_user_ctxt :: UserTypeCtxt
ds_user_ctxt = UserTypeCtxt
user_ctxt
, 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 [Type]
theta ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ DerivSpec [Type] -> 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 :: [Type]
ds_tys = [Type]
inst_tys
, ds_theta :: [Type]
ds_theta = [Type]
theta
, ds_skol_info :: SkolemInfo
ds_skol_info = SkolemInfo
skol_info
, ds_user_ctxt :: UserTypeCtxt
ds_user_ctxt = UserTypeCtxt
user_ctxt
, 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 DerivInstTys
dit
= do DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let isDeriveAnyClassEnabled :: DeriveAnyClassEnabled
isDeriveAnyClassEnabled =
Bool -> DeriveAnyClassEnabled
deriveAnyClassEnabled (Extension -> DynFlags -> Bool
xopt Extension
LangExt.DeriveAnyClass DynFlags
dflags)
DerivInstTys -> DerivM OriginativeDerivStatus
checkOriginativeSideConditions DerivInstTys
dit forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
CanDeriveStock StockGenFns
gen_fns -> 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_fns :: StockGenFns
dsm_stock_gen_fns = StockGenFns
gen_fns }
StockClassError DeriveInstanceErrReason
why -> forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving DeriveInstanceErrReason
why
OriginativeDerivStatus
CanDeriveAnyClass -> forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving
(DeriveAnyClassEnabled -> DeriveInstanceErrReason
DerivErrNotStockDeriveable DeriveAnyClassEnabled
isDeriveAnyClassEnabled)
OriginativeDerivStatus
NonDerivableClass -> forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving
(DeriveAnyClassEnabled -> DeriveInstanceErrReason
DerivErrNotStockDeriveable DeriveAnyClassEnabled
YesDeriveAnyClassEnabled)
mk_eqn_anyclass :: DerivM EarlyDerivSpec
mk_eqn_anyclass :: DerivM EarlyDerivSpec
mk_eqn_anyclass
= do DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let isDeriveAnyClassEnabled :: DeriveAnyClassEnabled
isDeriveAnyClassEnabled =
Bool -> DeriveAnyClassEnabled
deriveAnyClassEnabled (Extension -> DynFlags -> Bool
xopt Extension
LangExt.DeriveAnyClass DynFlags
dflags)
case Extension -> DynFlags -> Bool
xopt Extension
LangExt.DeriveAnyClass DynFlags
dflags of
Bool
True -> DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism DerivSpecMechanism
DerivSpecAnyClass
Bool
False -> forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving
(DeriveAnyClassEnabled -> DeriveInstanceErrReason
DerivErrNotDeriveable DeriveAnyClassEnabled
isDeriveAnyClassEnabled)
mk_eqn_newtype :: DerivInstTys
-> Type
-> DerivM EarlyDerivSpec
mk_eqn_newtype :: DerivInstTys -> Type -> DerivM EarlyDerivSpec
mk_eqn_newtype DerivInstTys
dit Type
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 :: Type
dsm_newtype_rep_ty = Type
rep_ty }
mk_eqn_via :: [Type]
-> Type
-> Type
-> DerivM EarlyDerivSpec
mk_eqn_via :: [Type] -> Type -> Type -> DerivM EarlyDerivSpec
mk_eqn_via [Type]
cls_tys Type
inst_ty Type
via_ty =
DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism forall a b. (a -> b) -> a -> b
$ DerivSpecVia { dsm_via_cls_tys :: [Type]
dsm_via_cls_tys = [Type]
cls_tys
, dsm_via_inst_ty :: Type
dsm_via_inst_ty = Type
inst_ty
, dsm_via_ty :: Type
dsm_via_ty = Type
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 -> [Type]
denv_inst_tys = [Type]
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 ([Type]
cls_tys, Type
inst_ty) <- forall a. [a] -> Maybe ([a], a)
snocView [Type]
cls_args
, Just DerivInstTys
dit <- FamInstEnvs -> [Type] -> Type -> Maybe DerivInstTys
mk_deriv_inst_tys_maybe FamInstEnvs
fam_envs [Type]
cls_tys Type
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 StockGenFns
hasStockDeriving Class
cls) forall a b. (a -> b) -> a -> b
$ \StockGenFns
_ ->
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_tc :: DerivInstTys -> TyCon
dit_tc = TyCon
tc
, dit_rep_tc :: DerivInstTys -> TyCon
dit_rep_tc = TyCon
rep_tc }) = do
DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let isDeriveAnyClassEnabled :: DeriveAnyClassEnabled
isDeriveAnyClassEnabled =
Bool -> DeriveAnyClassEnabled
deriveAnyClassEnabled (Extension -> DynFlags -> Bool
xopt Extension
LangExt.DeriveAnyClass DynFlags
dflags)
let dac_error :: DeriveInstanceErrReason
dac_error
| TyCon -> Bool
isClassTyCon TyCon
rep_tc
= TyCon -> DeriveAnyClassEnabled -> DeriveInstanceErrReason
DerivErrOnlyAnyClassDeriveable TyCon
tc DeriveAnyClassEnabled
isDeriveAnyClassEnabled
| Bool
otherwise
= DeriveAnyClassEnabled -> DeriveInstanceErrReason
DerivErrNotStockDeriveable DeriveAnyClassEnabled
isDeriveAnyClassEnabled
DerivInstTys -> DerivM OriginativeDerivStatus
checkOriginativeSideConditions DerivInstTys
dit forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
OriginativeDerivStatus
NonDerivableClass -> forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving DeriveInstanceErrReason
dac_error
StockClassError DeriveInstanceErrReason
why -> forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving DeriveInstanceErrReason
why
CanDeriveStock StockGenFns
gen_fns -> 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_fns :: StockGenFns
dsm_stock_gen_fns = StockGenFns
gen_fns }
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 -> [Type]
dit_cls_tys = [Type]
cls_tys
, dit_rep_tc :: DerivInstTys -> TyCon
dit_rep_tc = TyCon
rep_tycon
, dit_rep_tc_args :: DerivInstTys -> [Type]
dit_rep_tc_args = [Type]
rep_tc_args })
= do DerivEnv{denv_cls :: DerivEnv -> Class
denv_cls = Class
cls} <- 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 :: DeriveInstanceErrReason -> DerivM EarlyDerivSpec
bale_out = forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith (Bool -> UsingGeneralizedNewtypeDeriving
usingGeneralizedNewtypeDeriving Bool
newtype_deriving)
nt_eta_arity :: Int
nt_eta_arity = TyCon -> Int
newTyConEtadArity TyCon
rep_tycon
rep_inst_ty :: Type
rep_inst_ty = TyCon -> [Type] -> Type
newTyConInstRhs TyCon
rep_tycon [Type]
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 = [Type]
rep_tc_args forall a. [a] -> Int -> Bool
`lengthAtLeast` Int
nt_eta_arity
forall (m :: * -> *). (HasCallStack, Applicative m) => Bool -> m ()
massert ([Type]
cls_tys forall a. [a] -> Int -> Bool
`lengthIs` (Class -> Int
classArity Class
cls forall a. Num a => a -> a -> a
- Int
1))
if Bool
newtype_strat
then
if Bool
eta_ok Bool -> Bool -> Bool
&& Bool
newtype_deriving
then DerivInstTys -> Type -> DerivM EarlyDerivSpec
mk_eqn_newtype DerivInstTys
dit Type
rep_inst_ty
else DeriveInstanceErrReason -> DerivM EarlyDerivSpec
bale_out (Bool -> DeriveInstanceErrReason
DerivErrCannotEtaReduceEnough Bool
eta_ok)
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 -> Type -> DerivM EarlyDerivSpec
mk_eqn_newtype DerivInstTys
dit Type
rep_inst_ty
else DerivInstTys -> DerivM OriginativeDerivStatus
checkOriginativeSideConditions DerivInstTys
dit forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
StockClassError DeriveInstanceErrReason
why
| Bool
might_be_newtype_derivable Bool -> Bool -> Bool
&& Bool
newtype_deriving
-> DerivInstTys -> Type -> DerivM EarlyDerivSpec
mk_eqn_newtype DerivInstTys
dit Type
rep_inst_ty
| Bool
might_be_newtype_derivable Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
newtype_deriving
-> DeriveInstanceErrReason -> DerivM EarlyDerivSpec
bale_out DeriveInstanceErrReason
why
| Bool
otherwise
-> DeriveInstanceErrReason -> DerivM EarlyDerivSpec
bale_out DeriveInstanceErrReason
why
OriginativeDerivStatus
NonDerivableClass
| Bool
newtype_deriving -> DeriveInstanceErrReason -> DerivM EarlyDerivSpec
bale_out (Bool -> DeriveInstanceErrReason
DerivErrCannotEtaReduceEnough Bool
eta_ok)
| Bool
otherwise -> DeriveInstanceErrReason -> DerivM EarlyDerivSpec
bale_out DeriveInstanceErrReason
DerivErrNewtypeNonDeriveableClass
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
$ TcRnMessage -> TcRn ()
addDiagnosticTc
forall a b. (a -> b) -> a -> b
$ Class -> TcRnMessage
TcRnDerivingDefaults Class
cls
DerivSpecMechanism -> DerivM EarlyDerivSpec
mk_eqn_from_mechanism DerivSpecMechanism
DerivSpecAnyClass
CanDeriveStock StockGenFns
gen_fns -> 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_fns :: StockGenFns
dsm_stock_gen_fns = StockGenFns
gen_fns }
genInstBinds :: DerivSpec ThetaType
-> TcM (InstInfo GhcPs, Bag AuxBindSpec, [Name])
genInstBinds :: DerivSpec [Type] -> TcM (InstInfo GhcPs, Bag AuxBindSpec, [Name])
genInstBinds spec :: DerivSpec [Type]
spec@(DS { ds_tvs :: forall theta. DerivSpec theta -> [TyVar]
ds_tvs = [TyVar]
tyvars, ds_mechanism :: forall theta. DerivSpec theta -> DerivSpecMechanism
ds_mechanism = DerivSpecMechanism
mechanism
, ds_tys :: forall theta. DerivSpec theta -> [Type]
ds_tys = [Type]
inst_tys, ds_theta :: forall theta. DerivSpec theta -> theta
ds_theta = [Type]
theta, 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 })
= forall theta a. DerivSpec theta -> TcM a -> TcM a
set_spec_span_and_ctxt DerivSpec [Type]
spec forall a b. (a -> b) -> a -> b
$
do (Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
meth_binds, [GenLocated SrcSpanAnnA (Sig GhcPs)]
meth_sigs, Bag AuxBindSpec
aux_specs, [Name]
unusedNames) <- TcM (LHsBinds GhcPs, [LSig GhcPs], Bag AuxBindSpec, [Name])
gen_inst_binds
ClsInst
inst_spec <- DerivSpec [Type] -> TcM ClsInst
newDerivClsInst DerivSpec [Type]
spec
Class
-> ClsInst
-> [Type]
-> Maybe SrcSpan
-> DerivSpecMechanism
-> TcRn ()
doDerivInstErrorChecks2 Class
clas ClsInst
inst_spec [Type]
theta Maybe SrcSpan
wildcard DerivSpecMechanism
mechanism
String -> SDoc -> TcRn ()
traceTc String
"newder" (forall a. Outputable a => a -> SDoc
ppr ClsInst
inst_spec)
let inst_info :: InstInfo GhcPs
inst_info =
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]
tyvars
, 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 (InstInfo GhcPs
inst_info, Bag AuxBindSpec
aux_specs, [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
, Extension
LangExt.UnboxedTuples
]
| Bool
otherwise
= []
gen_inst_binds :: TcM (LHsBinds GhcPs, [LSig GhcPs], Bag AuxBindSpec, [Name])
gen_inst_binds :: TcM (LHsBinds GhcPs, [LSig GhcPs], Bag AuxBindSpec, [Name])
gen_inst_binds
= case DerivSpecMechanism
mechanism of
DerivSpecNewtype { dsm_newtype_rep_ty :: DerivSpecMechanism -> Type
dsm_newtype_rep_ty = Type
rhs_ty}
-> Type
-> IOEnv
(Env TcGblEnv TcLclEnv)
(Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs)),
[GenLocated SrcSpanAnnA (Sig GhcPs)], Bag AuxBindSpec, [Name])
gen_newtype_or_via Type
rhs_ty
DerivSpecStock { dsm_stock_dit :: DerivSpecMechanism -> DerivInstTys
dsm_stock_dit = DerivInstTys
dit
, dsm_stock_gen_fns :: DerivSpecMechanism -> StockGenFns
dsm_stock_gen_fns =
StockGenFns { stock_gen_binds :: StockGenFns
-> SrcSpan
-> DerivInstTys
-> TcM (LHsBinds GhcPs, [LSig GhcPs], Bag AuxBindSpec, [Name])
stock_gen_binds = SrcSpan
-> DerivInstTys
-> TcM (LHsBinds GhcPs, [LSig GhcPs], Bag AuxBindSpec, [Name])
gen_fn } }
-> SrcSpan
-> DerivInstTys
-> TcM (LHsBinds GhcPs, [LSig GhcPs], Bag AuxBindSpec, [Name])
gen_fn SrcSpan
loc DerivInstTys
dit
DerivSpecMechanism
DerivSpecAnyClass
-> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Bag a
emptyBag, [], forall a. Bag a
emptyBag, [])
DerivSpecVia{dsm_via_ty :: DerivSpecMechanism -> Type
dsm_via_ty = Type
via_ty}
-> Type
-> IOEnv
(Env TcGblEnv TcLclEnv)
(Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs)),
[GenLocated SrcSpanAnnA (Sig GhcPs)], Bag AuxBindSpec, [Name])
gen_newtype_or_via Type
via_ty
gen_newtype_or_via :: Type
-> IOEnv
(Env TcGblEnv TcLclEnv)
(Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs)),
[GenLocated SrcSpanAnnA (Sig GhcPs)], Bag AuxBindSpec, [Name])
gen_newtype_or_via Type
ty = do
let (LHsBinds GhcPs
binds, [LSig GhcPs]
sigs) = SrcSpan
-> Class
-> [TyVar]
-> [Type]
-> Type
-> (LHsBinds GhcPs, [LSig GhcPs])
gen_Newtype_binds SrcSpan
loc Class
clas [TyVar]
tyvars [Type]
inst_tys Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsBinds GhcPs
binds, [LSig GhcPs]
sigs, forall a. Bag a
emptyBag, [])
genFamInsts :: DerivSpec theta -> TcM [FamInst]
genFamInsts :: forall theta. DerivSpec theta -> TcM [FamInst]
genFamInsts spec :: DerivSpec theta
spec@(DS { ds_tvs :: forall theta. DerivSpec theta -> [TyVar]
ds_tvs = [TyVar]
tyvars, ds_mechanism :: forall theta. DerivSpec theta -> DerivSpecMechanism
ds_mechanism = DerivSpecMechanism
mechanism
, ds_tys :: forall theta. DerivSpec theta -> [Type]
ds_tys = [Type]
inst_tys, ds_cls :: forall theta. DerivSpec theta -> Class
ds_cls = Class
clas, ds_loc :: forall theta. DerivSpec theta -> SrcSpan
ds_loc = SrcSpan
loc })
= forall theta a. DerivSpec theta -> TcM a -> TcM a
set_spec_span_and_ctxt DerivSpec theta
spec forall a b. (a -> b) -> a -> b
$
case DerivSpecMechanism
mechanism of
DerivSpecNewtype { dsm_newtype_rep_ty :: DerivSpecMechanism -> Type
dsm_newtype_rep_ty = Type
rhs_ty}
-> Type -> TcM [FamInst]
gen_newtype_or_via Type
rhs_ty
DerivSpecStock { dsm_stock_dit :: DerivSpecMechanism -> DerivInstTys
dsm_stock_dit = DerivInstTys
dit
, dsm_stock_gen_fns :: DerivSpecMechanism -> StockGenFns
dsm_stock_gen_fns =
StockGenFns { stock_gen_fam_insts :: StockGenFns -> SrcSpan -> DerivInstTys -> TcM [FamInst]
stock_gen_fam_insts = SrcSpan -> DerivInstTys -> TcM [FamInst]
gen_fn } }
-> SrcSpan -> DerivInstTys -> TcM [FamInst]
gen_fn SrcSpan
loc DerivInstTys
dit
DerivSpecMechanism
DerivSpecAnyClass -> do
let mini_env :: VarEnv Type
mini_env = forall a. [(TyVar, a)] -> VarEnv a
mkVarEnv (Class -> [TyVar]
classTyVars Class
clas forall a b. [a] -> [b] -> [(a, b)]
`zip` [Type]
inst_tys)
mini_subst :: Subst
mini_subst = InScopeSet -> VarEnv Type -> Subst
mkTvSubst ([TyVar] -> InScopeSet
mkInScopeSetList [TyVar]
tyvars) VarEnv Type
mini_env
DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
[[FamInst]]
tyfam_insts <-
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (Extension -> DynFlags -> Bool
xopt Extension
LangExt.DeriveAnyClass DynFlags
dflags)
(forall doc. IsLine doc => String -> doc
text String
"genFamInsts: bad derived class" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr Class
clas) forall a b. (a -> b) -> a -> b
$
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (SrcSpan -> Subst -> Uses -> ClassATItem -> TcM [FamInst]
tcATDefault SrcSpan
loc Subst
mini_subst Uses
emptyNameSet)
(Class -> [ClassATItem]
classATItems Class
clas)
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[FamInst]]
tyfam_insts
DerivSpecVia{dsm_via_ty :: DerivSpecMechanism -> Type
dsm_via_ty = Type
via_ty}
-> Type -> TcM [FamInst]
gen_newtype_or_via Type
via_ty
where
gen_newtype_or_via :: Type -> TcM [FamInst]
gen_newtype_or_via Type
ty = SrcSpan -> Class -> [TyVar] -> [Type] -> Type -> TcM [FamInst]
gen_Newtype_fam_insts SrcSpan
loc Class
clas [TyVar]
tyvars [Type]
inst_tys Type
ty
set_spec_span_and_ctxt :: DerivSpec theta -> TcM a -> TcM a
set_spec_span_and_ctxt :: forall theta a. DerivSpec theta -> TcM a -> TcM a
set_spec_span_and_ctxt (DS{ ds_loc :: forall theta. DerivSpec theta -> SrcSpan
ds_loc = SrcSpan
loc, ds_cls :: forall theta. DerivSpec theta -> Class
ds_cls = Class
clas, ds_tys :: forall theta. DerivSpec theta -> [Type]
ds_tys = [Type]
tys }) =
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 -> [Type] -> SDoc
instDeclCtxt3 Class
clas [Type]
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 :: DeriveInstanceErrReason -> ReaderT DerivEnv TcRn ()
bale_out DeriveInstanceErrReason
msg = do TcRnMessage
err <- DerivSpecMechanism -> DeriveInstanceErrReason -> DerivM TcRnMessage
derivingThingErrMechanism DerivSpecMechanism
mechanism DeriveInstanceErrReason
msg
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a. TcRnMessage -> TcM a
failWithTc TcRnMessage
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
$
DeriveInstanceErrReason -> ReaderT DerivEnv TcRn ()
bale_out forall a b. (a -> b) -> a -> b
$ TyCon -> DeriveInstanceErrReason
DerivErrDataConsNotAllInScope 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 :: DeriveInstanceErrReason -> ReaderT DerivEnv TcRn ()
bale_out DeriveInstanceErrReason
msg = do TcRnMessage
err <- DerivSpecMechanism -> DeriveInstanceErrReason -> DerivM TcRnMessage
derivingThingErrMechanism DerivSpecMechanism
mechanism DeriveInstanceErrReason
msg
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a. TcRnMessage -> TcM a
failWithTc TcRnMessage
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 (Type -> Bool
at_last_cls_tv_in_kind forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyVar -> Type
tyVarKind)
(TyCon -> [TyVar]
tyConTyVars TyCon
tc)
Bool -> Bool -> Bool
|| Type -> Bool
at_last_cls_tv_in_kind (TyCon -> Type
tyConResKind TyCon
tc)) [TyCon]
atf_tcs
at_last_cls_tv_in_kind :: Type -> Bool
at_last_cls_tv_in_kind Type
kind
= TyVar
last_cls_tv TyVar -> VarSet -> Bool
`elemVarSet` Type -> VarSet
exactTyCoVarsOfType Type
kind
at_tcs :: [TyCon]
at_tcs = Class -> [TyCon]
classATs Class
cls
last_cls_tv :: TyVar
last_cls_tv = forall a. HasCallStack => Bool -> a -> a
assert (forall (f :: * -> *) a. Foldable f => f a -> Bool
notNull [TyVar]
cls_tyvars )
forall a. [a] -> a
last [TyVar]
cls_tyvars
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
ats_look_sensible forall a b. (a -> b) -> a -> b
$
DeriveInstanceErrReason -> ReaderT DerivEnv TcRn ()
bale_out (HasAssociatedDataFamInsts
-> AssociatedTyLastVarInKind
-> AssociatedTyNotParamOverLastTyVar
-> DeriveInstanceErrReason
DerivErrHasAssociatedDatatypes
(Bool -> HasAssociatedDataFamInsts
hasAssociatedDataFamInsts (Bool -> Bool
not Bool
no_adfs))
(Maybe TyCon -> AssociatedTyLastVarInKind
associatedTyLastVarInKind Maybe TyCon
at_last_cls_tv_in_kinds)
(Maybe TyCon -> AssociatedTyNotParamOverLastTyVar
associatedTyNotParamOverLastTyVar Maybe TyCon
at_without_last_cls_tv)
)
doDerivInstErrorChecks2 :: Class -> ClsInst -> ThetaType -> Maybe SrcSpan
-> DerivSpecMechanism -> TcM ()
doDerivInstErrorChecks2 :: Class
-> ClsInst
-> [Type]
-> Maybe SrcSpan
-> DerivSpecMechanism
-> TcRn ()
doDerivInstErrorChecks2 Class
clas ClsInst
clas_inst [Type]
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
let suggParSigs :: SuggestPartialTypeSignatures
suggParSigs = Bool -> SuggestPartialTypeSignatures
suggestPartialTypeSignatures Bool
xpartial_sigs
let dia :: TcRnMessage
dia = SuggestPartialTypeSignatures -> [Type] -> TcRnMessage
TcRnPartialTypeSignatures SuggestPartialTypeSignatures
suggParSigs [Type]
theta
Bool -> TcRnMessage -> TcRn ()
checkTc Bool
xpartial_sigs TcRnMessage
dia
Bool -> TcRnMessage -> TcRn ()
diagnosticTc Bool
wpartial_sigs TcRnMessage
dia
; 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 -> TcRnMessage -> TcRn ()
failIfTc (DynFlags -> Bool
safeLanguageOn DynFlags
dflags)
(Class
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> TcRnMessage
TcRnCannotDeriveInstance Class
clas forall a. Monoid a => a
mempty forall a. Maybe a
Nothing UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving forall a b. (a -> b) -> a -> b
$
DeriveInstanceErrReason
DerivErrSafeHaskellGenericInst)
; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (DynFlags -> Bool
safeInferOn DynFlags
dflags) (Messages TcRnMessage -> TcRn ()
recordUnsafeInfer forall e. Messages e
emptyMessages) } }
where
exotic_mechanism :: Bool
exotic_mechanism = Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ DerivSpecMechanism -> Bool
isDerivSpecStock DerivSpecMechanism
mechanism
derivingThingFailWith :: UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> DerivM a
derivingThingFailWith :: forall a.
UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM a
derivingThingFailWith UsingGeneralizedNewtypeDeriving
newtype_deriving DeriveInstanceErrReason
msg = do
TcRnMessage
err <- UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM TcRnMessage
derivingThingErrM UsingGeneralizedNewtypeDeriving
newtype_deriving DeriveInstanceErrReason
msg
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a. TcRnMessage -> TcM a
failWithTc TcRnMessage
err
derivingThingErrM :: UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> DerivM TcRnMessage
derivingThingErrM :: UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason -> DerivM TcRnMessage
derivingThingErrM UsingGeneralizedNewtypeDeriving
newtype_deriving DeriveInstanceErrReason
why
= do DerivEnv { denv_cls :: DerivEnv -> Class
denv_cls = Class
cls
, denv_inst_tys :: DerivEnv -> [Type]
denv_inst_tys = [Type]
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
$ Class
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> TcRnMessage
TcRnCannotDeriveInstance Class
cls [Type]
cls_args Maybe (DerivStrategy GhcTc)
mb_strat UsingGeneralizedNewtypeDeriving
newtype_deriving DeriveInstanceErrReason
why
derivingThingErrMechanism :: DerivSpecMechanism -> DeriveInstanceErrReason -> DerivM TcRnMessage
derivingThingErrMechanism :: DerivSpecMechanism -> DeriveInstanceErrReason -> DerivM TcRnMessage
derivingThingErrMechanism DerivSpecMechanism
mechanism DeriveInstanceErrReason
why
= do DerivEnv { denv_cls :: DerivEnv -> Class
denv_cls = Class
cls
, denv_inst_tys :: DerivEnv -> [Type]
denv_inst_tys = [Type]
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
$ Class
-> [Type]
-> Maybe (DerivStrategy GhcTc)
-> UsingGeneralizedNewtypeDeriving
-> DeriveInstanceErrReason
-> TcRnMessage
TcRnCannotDeriveInstance Class
cls [Type]
cls_args Maybe (DerivStrategy GhcTc)
mb_strat UsingGeneralizedNewtypeDeriving
newtype_deriving DeriveInstanceErrReason
why
where
newtype_deriving :: UsingGeneralizedNewtypeDeriving
newtype_deriving :: UsingGeneralizedNewtypeDeriving
newtype_deriving
= if DerivSpecMechanism -> Bool
isDerivSpecNewtype DerivSpecMechanism
mechanism then UsingGeneralizedNewtypeDeriving
YesGeneralizedNewtypeDeriving
else UsingGeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving
standaloneCtxt :: LHsSigWcType GhcRn -> SDoc
standaloneCtxt :: LHsSigWcType GhcRn -> SDoc
standaloneCtxt LHsSigWcType GhcRn
ty = SDoc -> Int -> SDoc -> SDoc
hang (forall doc. IsLine doc => String -> doc
text String
"In the stand-alone deriving instance for")
Int
2 (SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr LHsSigWcType GhcRn
ty))