{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module GHC.Tc.TyCl.Utils(
RolesInfo,
inferRoles,
checkSynCycles,
checkClassCycles,
addTyConsToGblEnv, mkDefaultMethodType,
tcRecSelBinds, mkRecSelBinds, mkOneRecordSelector
) where
import GHC.Prelude
import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.Env
import GHC.Tc.Gen.Bind( tcValBinds )
import GHC.Tc.Utils.TcType
import GHC.Builtin.Types( unitTy )
import GHC.Builtin.Uniques ( mkBuiltinUnique )
import GHC.Hs
import GHC.Core.TyCo.Rep( Type(..), Coercion(..), MCoercion(..) )
import GHC.Core.Multiplicity
import GHC.Core.Predicate
import GHC.Core.Make( rEC_SEL_ERROR_ID )
import GHC.Core.Class
import GHC.Core.Type
import GHC.Core.TyCon
import GHC.Core.ConLike
import GHC.Core.DataCon
import GHC.Core.TyCon.Set
import GHC.Core.Coercion ( ltRole )
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Utils.Misc
import GHC.Utils.FV as FV
import GHC.Data.Maybe
import GHC.Data.FastString
import GHC.Unit.Module
import GHC.Rename.Utils (genHsVar, genLHsApp, genLHsLit, genWildPat, wrapGenSpan)
import GHC.Types.Basic
import GHC.Types.FieldLabel
import GHC.Types.SrcLoc
import GHC.Types.SourceFile
import GHC.Types.SourceText
import GHC.Types.Name
import GHC.Types.Name.Env
import GHC.Types.Name.Reader ( mkRdrUnqual )
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Types.Var.Env
import GHC.Types.Var.Set
import GHC.Types.Unique.Set
import GHC.Types.TyThing
import qualified GHC.LanguageExtensions as LangExt
import Language.Haskell.Syntax.Basic (FieldLabelString(..))
import Control.Monad
synonymTyConsOfType :: Type -> [TyCon]
synonymTyConsOfType :: Type -> [TyCon]
synonymTyConsOfType Type
ty
= NameEnv TyCon -> [TyCon]
forall a. NameEnv a -> [a]
nonDetNameEnvElts (Type -> NameEnv TyCon
go Type
ty)
where
go :: Type -> NameEnv TyCon
go :: Type -> NameEnv TyCon
go (TyConApp TyCon
tc [Type]
tys) = TyCon -> NameEnv TyCon
go_tc TyCon
tc NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` [Type] -> NameEnv TyCon
forall {t :: * -> *}. Foldable t => t Type -> NameEnv TyCon
go_s [Type]
tys
go (LitTy TyLit
_) = NameEnv TyCon
forall a. NameEnv a
emptyNameEnv
go (TyVarTy Id
_) = NameEnv TyCon
forall a. NameEnv a
emptyNameEnv
go (AppTy Type
a Type
b) = Type -> NameEnv TyCon
go Type
a NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` Type -> NameEnv TyCon
go Type
b
go (FunTy FunTyFlag
_ Type
w Type
a Type
b) = Type -> NameEnv TyCon
go Type
w NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` Type -> NameEnv TyCon
go Type
a NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` Type -> NameEnv TyCon
go Type
b
go (ForAllTy ForAllTyBinder
_ Type
ty) = Type -> NameEnv TyCon
go Type
ty
go (CastTy Type
ty KindCoercion
co) = Type -> NameEnv TyCon
go Type
ty NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` KindCoercion -> NameEnv TyCon
go_co KindCoercion
co
go (CoercionTy KindCoercion
co) = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co
go_mco :: MCoercionN -> NameEnv TyCon
go_mco MCoercionN
MRefl = NameEnv TyCon
forall a. NameEnv a
emptyNameEnv
go_mco (MCo KindCoercion
co) = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co
go_co :: KindCoercion -> NameEnv TyCon
go_co (Refl Type
ty) = Type -> NameEnv TyCon
go Type
ty
go_co (GRefl Role
_ Type
ty MCoercionN
mco) = Type -> NameEnv TyCon
go Type
ty NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` MCoercionN -> NameEnv TyCon
go_mco MCoercionN
mco
go_co (TyConAppCo Role
_ TyCon
tc [KindCoercion]
cs) = TyCon -> NameEnv TyCon
go_tc TyCon
tc NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` [KindCoercion] -> NameEnv TyCon
go_co_s [KindCoercion]
cs
go_co (AppCo KindCoercion
co KindCoercion
co') = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` KindCoercion -> NameEnv TyCon
go_co KindCoercion
co'
go_co (ForAllCo { fco_kind :: KindCoercion -> KindCoercion
fco_kind = KindCoercion
kind_co, fco_body :: KindCoercion -> KindCoercion
fco_body = KindCoercion
body_co })
= KindCoercion -> NameEnv TyCon
go_co KindCoercion
kind_co NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` KindCoercion -> NameEnv TyCon
go_co KindCoercion
body_co
go_co (FunCo { fco_mult :: KindCoercion -> KindCoercion
fco_mult = KindCoercion
m, fco_arg :: KindCoercion -> KindCoercion
fco_arg = KindCoercion
a, fco_res :: KindCoercion -> KindCoercion
fco_res = KindCoercion
r })
= KindCoercion -> NameEnv TyCon
go_co KindCoercion
m NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` KindCoercion -> NameEnv TyCon
go_co KindCoercion
a NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` KindCoercion -> NameEnv TyCon
go_co KindCoercion
r
go_co (CoVarCo Id
_) = NameEnv TyCon
forall a. NameEnv a
emptyNameEnv
go_co (HoleCo {}) = NameEnv TyCon
forall a. NameEnv a
emptyNameEnv
go_co (AxiomCo CoAxiomRule
_ [KindCoercion]
cs) = [KindCoercion] -> NameEnv TyCon
go_co_s [KindCoercion]
cs
go_co (UnivCo { uco_lty :: KindCoercion -> Type
uco_lty = Type
t1, uco_rty :: KindCoercion -> Type
uco_rty = Type
t2})
= Type -> NameEnv TyCon
go Type
t1 NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` Type -> NameEnv TyCon
go Type
t2
go_co (SymCo KindCoercion
co) = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co
go_co (TransCo KindCoercion
co KindCoercion
co') = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` KindCoercion -> NameEnv TyCon
go_co KindCoercion
co'
go_co (SelCo CoSel
_ KindCoercion
co) = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co
go_co (LRCo LeftOrRight
_ KindCoercion
co) = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co
go_co (InstCo KindCoercion
co KindCoercion
co') = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
`plusNameEnv` KindCoercion -> NameEnv TyCon
go_co KindCoercion
co'
go_co (KindCo KindCoercion
co) = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co
go_co (SubCo KindCoercion
co) = KindCoercion -> NameEnv TyCon
go_co KindCoercion
co
go_tc :: TyCon -> NameEnv TyCon
go_tc TyCon
tc | TyCon -> Bool
isTypeSynonymTyCon TyCon
tc = Name -> TyCon -> NameEnv TyCon
forall a. Name -> a -> NameEnv a
unitNameEnv (TyCon -> Name
tyConName TyCon
tc) TyCon
tc
| Bool
otherwise = NameEnv TyCon
forall a. NameEnv a
emptyNameEnv
go_s :: t Type -> NameEnv TyCon
go_s t Type
tys = (Type -> NameEnv TyCon -> NameEnv TyCon)
-> NameEnv TyCon -> t Type -> NameEnv TyCon
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
plusNameEnv (NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon)
-> (Type -> NameEnv TyCon)
-> Type
-> NameEnv TyCon
-> NameEnv TyCon
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> NameEnv TyCon
go) NameEnv TyCon
forall a. NameEnv a
emptyNameEnv t Type
tys
go_co_s :: [KindCoercion] -> NameEnv TyCon
go_co_s [KindCoercion]
cos = (KindCoercion -> NameEnv TyCon -> NameEnv TyCon)
-> NameEnv TyCon -> [KindCoercion] -> NameEnv TyCon
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon
forall a. NameEnv a -> NameEnv a -> NameEnv a
plusNameEnv (NameEnv TyCon -> NameEnv TyCon -> NameEnv TyCon)
-> (KindCoercion -> NameEnv TyCon)
-> KindCoercion
-> NameEnv TyCon
-> NameEnv TyCon
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindCoercion -> NameEnv TyCon
go_co) NameEnv TyCon
forall a. NameEnv a
emptyNameEnv [KindCoercion]
cos
newtype SynCycleM a = SynCycleM {
forall a.
SynCycleM a
-> SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState)
runSynCycleM :: SynCycleState -> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState) }
deriving ((forall a b. (a -> b) -> SynCycleM a -> SynCycleM b)
-> (forall a b. a -> SynCycleM b -> SynCycleM a)
-> Functor SynCycleM
forall a b. a -> SynCycleM b -> SynCycleM a
forall a b. (a -> b) -> SynCycleM a -> SynCycleM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> SynCycleM a -> SynCycleM b
fmap :: forall a b. (a -> b) -> SynCycleM a -> SynCycleM b
$c<$ :: forall a b. a -> SynCycleM b -> SynCycleM a
<$ :: forall a b. a -> SynCycleM b -> SynCycleM a
Functor)
type SynCycleState = TyConSet
instance Applicative SynCycleM where
pure :: forall a. a -> SynCycleM a
pure a
x = (SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState))
-> SynCycleM a
forall a.
(SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState))
-> SynCycleM a
SynCycleM ((SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState))
-> SynCycleM a)
-> (SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState))
-> SynCycleM a
forall a b. (a -> b) -> a -> b
$ \SynCycleState
state -> (a, SynCycleState)
-> Either
(SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
(a, SynCycleState)
forall a b. b -> Either a b
Right (a
x, SynCycleState
state)
<*> :: forall a b. SynCycleM (a -> b) -> SynCycleM a -> SynCycleM b
(<*>) = SynCycleM (a -> b) -> SynCycleM a -> SynCycleM b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
instance Monad SynCycleM where
SynCycleM a
m >>= :: forall a b. SynCycleM a -> (a -> SynCycleM b) -> SynCycleM b
>>= a -> SynCycleM b
f = (SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (b, SynCycleState))
-> SynCycleM b
forall a.
(SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState))
-> SynCycleM a
SynCycleM ((SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (b, SynCycleState))
-> SynCycleM b)
-> (SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (b, SynCycleState))
-> SynCycleM b
forall a b. (a -> b) -> a -> b
$ \SynCycleState
state ->
case SynCycleM a
-> SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState)
forall a.
SynCycleM a
-> SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState)
runSynCycleM SynCycleM a
m SynCycleState
state of
Right (a
x, SynCycleState
state') ->
SynCycleM b
-> SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (b, SynCycleState)
forall a.
SynCycleM a
-> SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState)
runSynCycleM (a -> SynCycleM b
f a
x) SynCycleState
state'
Left (SrcSpan, TySynCycleTyCons)
err -> (SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
-> Either
(SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
(b, SynCycleState)
forall a b. a -> Either a b
Left (SrcSpan, TySynCycleTyCons)
(SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
err
failSynCycleM :: SrcSpan -> TySynCycleTyCons -> SynCycleM ()
failSynCycleM :: SrcSpan -> TySynCycleTyCons -> SynCycleM ()
failSynCycleM SrcSpan
loc TySynCycleTyCons
seen_tcs = (SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) ((), SynCycleState))
-> SynCycleM ()
forall a.
(SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState))
-> SynCycleM a
SynCycleM ((SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) ((), SynCycleState))
-> SynCycleM ())
-> (SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) ((), SynCycleState))
-> SynCycleM ()
forall a b. (a -> b) -> a -> b
$ \SynCycleState
_ -> (SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
-> Either
(SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
((), SynCycleState)
forall a b. a -> Either a b
Left (SrcSpan
loc, TySynCycleTyCons
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))]
seen_tcs)
checkTyConIsAcyclic :: TyCon -> SynCycleM () -> SynCycleM ()
checkTyConIsAcyclic :: TyCon -> SynCycleM () -> SynCycleM ()
checkTyConIsAcyclic TyCon
tc SynCycleM ()
m = (SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) ((), SynCycleState))
-> SynCycleM ()
forall a.
(SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState))
-> SynCycleM a
SynCycleM ((SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) ((), SynCycleState))
-> SynCycleM ())
-> (SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) ((), SynCycleState))
-> SynCycleM ()
forall a b. (a -> b) -> a -> b
$ \SynCycleState
s ->
if TyCon
tc TyCon -> SynCycleState -> Bool
`elemTyConSet` SynCycleState
s
then ((), SynCycleState)
-> Either
(SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
((), SynCycleState)
forall a b. b -> Either a b
Right ((), SynCycleState
s)
else case SynCycleM ()
-> SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) ((), SynCycleState)
forall a.
SynCycleM a
-> SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState)
runSynCycleM SynCycleM ()
m SynCycleState
s of
Right ((), SynCycleState
s') -> ((), SynCycleState)
-> Either
(SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
((), SynCycleState)
forall a b. b -> Either a b
Right ((), SynCycleState -> TyCon -> SynCycleState
extendTyConSet SynCycleState
s' TyCon
tc)
Left (SrcSpan, TySynCycleTyCons)
err -> (SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
-> Either
(SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
((), SynCycleState)
forall a b. a -> Either a b
Left (SrcSpan, TySynCycleTyCons)
(SrcSpan,
[Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))])
err
checkSynCycles :: Unit -> [TyCon] -> [LTyClDecl GhcRn] -> TcM ()
checkSynCycles :: Unit -> [TyCon] -> [LTyClDecl (GhcPass 'Renamed)] -> TcM ()
checkSynCycles Unit
this_uid [TyCon]
tcs [LTyClDecl (GhcPass 'Renamed)]
tyclds =
case SynCycleM ()
-> SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) ((), SynCycleState)
forall a.
SynCycleM a
-> SynCycleState
-> Either (SrcSpan, TySynCycleTyCons) (a, SynCycleState)
runSynCycleM ((TyCon -> SynCycleM ()) -> [TyCon] -> SynCycleM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (SynCycleState -> [TyCon] -> TyCon -> SynCycleM ()
go SynCycleState
emptyTyConSet []) [TyCon]
tcs) SynCycleState
emptyTyConSet of
Left (SrcSpan
loc, TySynCycleTyCons
err) -> SrcSpan -> TcM () -> TcM ()
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc (TcM () -> TcM ()) -> TcM () -> TcM ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcM ()
forall a. TcRnMessage -> TcM a
failWithTc (TySynCycleTyCons -> TcRnMessage
TcRnTypeSynonymCycle TySynCycleTyCons
err)
Right ((), SynCycleState)
_ -> () -> TcM ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
lcl_decls :: NameEnv (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
lcl_decls = [(Name, GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))]
-> NameEnv (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
forall a. [(Name, a)] -> NameEnv a
mkNameEnv ([Name]
-> [GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed))]
-> [(Name, GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))]
forall a b. [a] -> [b] -> [(a, b)]
zip ((TyCon -> Name) -> [TyCon] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map TyCon -> Name
tyConName [TyCon]
tcs) [LTyClDecl (GhcPass 'Renamed)]
[GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed))]
tyclds)
go :: TyConSet -> [TyCon] -> TyCon -> SynCycleM ()
go :: SynCycleState -> [TyCon] -> TyCon -> SynCycleM ()
go SynCycleState
so_far [TyCon]
seen_tcs TyCon
tc =
TyCon -> SynCycleM () -> SynCycleM ()
checkTyConIsAcyclic TyCon
tc (SynCycleM () -> SynCycleM ()) -> SynCycleM () -> SynCycleM ()
forall a b. (a -> b) -> a -> b
$ SynCycleState -> [TyCon] -> TyCon -> SynCycleM ()
go' SynCycleState
so_far [TyCon]
seen_tcs TyCon
tc
go' :: TyConSet -> [TyCon] -> TyCon -> SynCycleM ()
go' :: SynCycleState -> [TyCon] -> TyCon -> SynCycleM ()
go' SynCycleState
so_far [TyCon]
seen_tcs TyCon
tc
| TyCon
tc TyCon -> SynCycleState -> Bool
`elemTyConSet` SynCycleState
so_far
= SrcSpan -> TySynCycleTyCons -> SynCycleM ()
failSynCycleM (TyCon -> SrcSpan
forall a. NamedThing a => a -> SrcSpan
getSrcSpan ([TyCon] -> TyCon
forall a. HasCallStack => [a] -> a
head [TyCon]
seen_tcs)) (TyCon
-> Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
lookup_decl (TyCon
-> Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed))))
-> [TyCon]
-> [Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [TyCon]
seen_tcs)
| Bool -> Bool
not (GenModule Unit -> Bool
forall u. GenModule (GenUnit u) -> Bool
isHoleModule GenModule Unit
mod Bool -> Bool -> Bool
||
GenModule Unit -> Unit
forall unit. GenModule unit -> unit
moduleUnit GenModule Unit
mod Unit -> Unit -> Bool
forall a. Eq a => a -> a -> Bool
== Unit
this_uid Bool -> Bool -> Bool
||
GenModule Unit -> Bool
isInteractiveModule GenModule Unit
mod)
= () -> SynCycleM ()
forall a. a -> SynCycleM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Just Type
ty <- TyCon -> Maybe Type
synTyConRhs_maybe TyCon
tc =
SynCycleState -> [TyCon] -> Type -> SynCycleM ()
go_ty (SynCycleState -> TyCon -> SynCycleState
extendTyConSet SynCycleState
so_far TyCon
tc) (TyCon
tcTyCon -> [TyCon] -> [TyCon]
forall a. a -> [a] -> [a]
:[TyCon]
seen_tcs) Type
ty
| Bool
otherwise = () -> SynCycleM ()
forall a. a -> SynCycleM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
n :: Name
n = TyCon -> Name
tyConName TyCon
tc
mod :: GenModule Unit
mod = HasDebugCallStack => Name -> GenModule Unit
Name -> GenModule Unit
nameModule Name
n
lookup_decl :: TyCon
-> Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
lookup_decl TyCon
tc =
case NameEnv (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
-> Name
-> Maybe (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv NameEnv (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
lcl_decls (TyCon -> Name
tyConName TyCon
tc) of
Just GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed))
decl -> GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed))
-> Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
forall a b. b -> Either a b
Right GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed))
decl
Maybe (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
Nothing -> TyCon
-> Either
TyCon (GenLocated SrcSpanAnnA (TyClDecl (GhcPass 'Renamed)))
forall a b. a -> Either a b
Left TyCon
tc
go_ty :: TyConSet -> [TyCon] -> Type -> SynCycleM ()
go_ty :: SynCycleState -> [TyCon] -> Type -> SynCycleM ()
go_ty SynCycleState
so_far [TyCon]
seen_tcs Type
ty =
(TyCon -> SynCycleM ()) -> [TyCon] -> SynCycleM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (SynCycleState -> [TyCon] -> TyCon -> SynCycleM ()
go SynCycleState
so_far [TyCon]
seen_tcs) (Type -> [TyCon]
synonymTyConsOfType Type
ty)
type ClassSet = UniqSet Class
checkClassCycles :: Class -> Maybe SuperclassCycle
checkClassCycles :: Class -> Maybe SuperclassCycle
checkClassCycles Class
cls
= do { (definite_cycle, details) <- ClassSet
-> Class -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go (Class -> ClassSet
forall a. Uniquable a => a -> UniqSet a
unitUniqSet Class
cls)
Class
cls ([Id] -> [Type]
mkTyVarTys (Class -> [Id]
classTyVars Class
cls))
; return (MkSuperclassCycle cls definite_cycle details) }
where
go :: ClassSet -> Class -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go :: ClassSet
-> Class -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go ClassSet
so_far Class
cls [Type]
tys = [Maybe (Bool, [SuperclassCycleDetail])]
-> Maybe (Bool, [SuperclassCycleDetail])
forall (f :: * -> *) a. Foldable f => f (Maybe a) -> Maybe a
firstJusts ([Maybe (Bool, [SuperclassCycleDetail])]
-> Maybe (Bool, [SuperclassCycleDetail]))
-> [Maybe (Bool, [SuperclassCycleDetail])]
-> Maybe (Bool, [SuperclassCycleDetail])
forall a b. (a -> b) -> a -> b
$
(Type -> Maybe (Bool, [SuperclassCycleDetail]))
-> [Type] -> [Maybe (Bool, [SuperclassCycleDetail])]
forall a b. (a -> b) -> [a] -> [b]
map (ClassSet -> Type -> Maybe (Bool, [SuperclassCycleDetail])
go_pred ClassSet
so_far) ([Type] -> [Maybe (Bool, [SuperclassCycleDetail])])
-> [Type] -> [Maybe (Bool, [SuperclassCycleDetail])]
forall a b. (a -> b) -> a -> b
$
Class -> [Type] -> [Type]
immSuperClasses Class
cls [Type]
tys
go_pred :: ClassSet -> PredType -> Maybe (Bool, [SuperclassCycleDetail])
go_pred :: ClassSet -> Type -> Maybe (Bool, [SuperclassCycleDetail])
go_pred ClassSet
so_far Type
pred
| Just (TyCon
tc, [Type]
tys) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe Type
pred
= ClassSet
-> Type -> TyCon -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go_tc ClassSet
so_far Type
pred TyCon
tc [Type]
tys
| Type -> Bool
hasTyVarHead Type
pred
= (Bool, [SuperclassCycleDetail])
-> Maybe (Bool, [SuperclassCycleDetail])
forall a. a -> Maybe a
Just (Bool
False, [Type -> SuperclassCycleDetail
SCD_HeadTyVar Type
pred])
| Bool
otherwise
= Maybe (Bool, [SuperclassCycleDetail])
forall a. Maybe a
Nothing
go_tc :: ClassSet -> PredType -> TyCon -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go_tc :: ClassSet
-> Type -> TyCon -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go_tc ClassSet
so_far Type
pred TyCon
tc [Type]
tys
| TyCon -> Bool
isFamilyTyCon TyCon
tc
= (Bool, [SuperclassCycleDetail])
-> Maybe (Bool, [SuperclassCycleDetail])
forall a. a -> Maybe a
Just (Bool
False, [Type -> SuperclassCycleDetail
SCD_HeadTyFam Type
pred])
| Just Class
cls <- TyCon -> Maybe Class
tyConClass_maybe TyCon
tc
= ClassSet
-> Class -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go_cls ClassSet
so_far Class
cls [Type]
tys
| Bool
otherwise
= Maybe (Bool, [SuperclassCycleDetail])
forall a. Maybe a
Nothing
go_cls :: ClassSet -> Class -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go_cls :: ClassSet
-> Class -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go_cls ClassSet
so_far Class
cls [Type]
tys
| Class
cls Class -> ClassSet -> Bool
forall a. Uniquable a => a -> UniqSet a -> Bool
`elementOfUniqSet` ClassSet
so_far
= (Bool, [SuperclassCycleDetail])
-> Maybe (Bool, [SuperclassCycleDetail])
forall a. a -> Maybe a
Just (Bool
True, [Class -> SuperclassCycleDetail
SCD_Superclass Class
cls])
| Class -> Bool
isCTupleClass Class
cls
= ClassSet
-> Class -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go ClassSet
so_far Class
cls [Type]
tys
| Bool
otherwise
= do { (b, details) <- ClassSet
-> Class -> [Type] -> Maybe (Bool, [SuperclassCycleDetail])
go (ClassSet
so_far ClassSet -> Class -> ClassSet
forall a. Uniquable a => UniqSet a -> a -> UniqSet a
`addOneToUniqSet` Class
cls) Class
cls [Type]
tys
; return (b, SCD_Superclass cls : details) }
type RolesInfo = Name -> [Role]
type RoleEnv = NameEnv [Role]
inferRoles :: HscSource -> RoleAnnotEnv -> [TyCon] -> Name -> [Role]
inferRoles :: HscSource -> RoleAnnotEnv -> [TyCon] -> Name -> [Role]
inferRoles HscSource
hsc_src RoleAnnotEnv
annots [TyCon]
tycons
= let role_env :: RoleEnv
role_env = HscSource -> RoleAnnotEnv -> [TyCon] -> RoleEnv
initialRoleEnv HscSource
hsc_src RoleAnnotEnv
annots [TyCon]
tycons
role_env' :: RoleEnv
role_env' = RoleEnv -> [TyCon] -> RoleEnv
irGroup RoleEnv
role_env [TyCon]
tycons in
\Name
name -> case RoleEnv -> Name -> Maybe [Role]
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv RoleEnv
role_env' Name
name of
Just [Role]
roles -> [Role]
roles
Maybe [Role]
Nothing -> String -> SDoc -> [Role]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"inferRoles" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
name)
initialRoleEnv :: HscSource -> RoleAnnotEnv -> [TyCon] -> RoleEnv
initialRoleEnv :: HscSource -> RoleAnnotEnv -> [TyCon] -> RoleEnv
initialRoleEnv HscSource
hsc_src RoleAnnotEnv
annots = RoleEnv -> [(Name, [Role])] -> RoleEnv
forall a. NameEnv a -> [(Name, a)] -> NameEnv a
extendNameEnvList RoleEnv
forall a. NameEnv a
emptyNameEnv ([(Name, [Role])] -> RoleEnv)
-> ([TyCon] -> [(Name, [Role])]) -> [TyCon] -> RoleEnv
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(TyCon -> (Name, [Role])) -> [TyCon] -> [(Name, [Role])]
forall a b. (a -> b) -> [a] -> [b]
map (HscSource -> RoleAnnotEnv -> TyCon -> (Name, [Role])
initialRoleEnv1 HscSource
hsc_src RoleAnnotEnv
annots)
initialRoleEnv1 :: HscSource -> RoleAnnotEnv -> TyCon -> (Name, [Role])
initialRoleEnv1 :: HscSource -> RoleAnnotEnv -> TyCon -> (Name, [Role])
initialRoleEnv1 HscSource
hsc_src RoleAnnotEnv
annots_env TyCon
tc
| TyCon -> Bool
isFamilyTyCon TyCon
tc = (Name
name, (TyConBinder -> Role) -> [TyConBinder] -> [Role]
forall a b. (a -> b) -> [a] -> [b]
map (Role -> TyConBinder -> Role
forall a b. a -> b -> a
const Role
Nominal) [TyConBinder]
bndrs)
| TyCon -> Bool
isAlgTyCon TyCon
tc = (Name
name, [Role]
default_roles)
| TyCon -> Bool
isTypeSynonymTyCon TyCon
tc = (Name
name, [Role]
default_roles)
| Bool
otherwise = String -> SDoc -> (Name, [Role])
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"initialRoleEnv1" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc)
where name :: Name
name = TyCon -> Name
tyConName TyCon
tc
bndrs :: [TyConBinder]
bndrs = TyCon -> [TyConBinder]
tyConBinders TyCon
tc
argflags :: [ForAllTyFlag]
argflags = (TyConBinder -> ForAllTyFlag) -> [TyConBinder] -> [ForAllTyFlag]
forall a b. (a -> b) -> [a] -> [b]
map TyConBinder -> ForAllTyFlag
forall a. VarBndr a TyConBndrVis -> ForAllTyFlag
tyConBinderForAllTyFlag [TyConBinder]
bndrs
num_exps :: Int
num_exps = (ForAllTyFlag -> Bool) -> [ForAllTyFlag] -> Int
forall a. (a -> Bool) -> [a] -> Int
count ForAllTyFlag -> Bool
isVisibleForAllTyFlag [ForAllTyFlag]
argflags
role_annots :: [Maybe Role]
role_annots
= case RoleAnnotEnv -> Name -> Maybe (LRoleAnnotDecl (GhcPass 'Renamed))
lookupRoleAnnot RoleAnnotEnv
annots_env Name
name of
Just (L SrcSpanAnnA
_ (RoleAnnotDecl XCRoleAnnotDecl (GhcPass 'Renamed)
_ LIdP (GhcPass 'Renamed)
_ [XRec (GhcPass 'Renamed) (Maybe Role)]
annots))
| [XRec (GhcPass 'Renamed) (Maybe Role)]
[GenLocated (EpAnn NoEpAnns) (Maybe Role)]
annots [GenLocated (EpAnn NoEpAnns) (Maybe Role)] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthIs` Int
num_exps -> (GenLocated (EpAnn NoEpAnns) (Maybe Role) -> Maybe Role)
-> [GenLocated (EpAnn NoEpAnns) (Maybe Role)] -> [Maybe Role]
forall a b. (a -> b) -> [a] -> [b]
map GenLocated (EpAnn NoEpAnns) (Maybe Role) -> Maybe Role
forall l e. GenLocated l e -> e
unLoc [XRec (GhcPass 'Renamed) (Maybe Role)]
[GenLocated (EpAnn NoEpAnns) (Maybe Role)]
annots
Maybe (LRoleAnnotDecl (GhcPass 'Renamed))
_ -> Int -> Maybe Role -> [Maybe Role]
forall a. Int -> a -> [a]
replicate Int
num_exps Maybe Role
forall a. Maybe a
Nothing
default_roles :: [Role]
default_roles = [ForAllTyFlag] -> [Maybe Role] -> [Role]
build_default_roles [ForAllTyFlag]
argflags [Maybe Role]
role_annots
build_default_roles :: [ForAllTyFlag] -> [Maybe Role] -> [Role]
build_default_roles (ForAllTyFlag
argf : [ForAllTyFlag]
argfs) (Maybe Role
m_annot : [Maybe Role]
ras)
| ForAllTyFlag -> Bool
isVisibleForAllTyFlag ForAllTyFlag
argf
= (Maybe Role
m_annot Maybe Role -> Role -> Role
forall a. Maybe a -> a -> a
`orElse` Role
default_role) Role -> [Role] -> [Role]
forall a. a -> [a] -> [a]
: [ForAllTyFlag] -> [Maybe Role] -> [Role]
build_default_roles [ForAllTyFlag]
argfs [Maybe Role]
ras
build_default_roles (ForAllTyFlag
_argf : [ForAllTyFlag]
argfs) [Maybe Role]
ras
= Role
Nominal Role -> [Role] -> [Role]
forall a. a -> [a] -> [a]
: [ForAllTyFlag] -> [Maybe Role] -> [Role]
build_default_roles [ForAllTyFlag]
argfs [Maybe Role]
ras
build_default_roles [] [] = []
build_default_roles [ForAllTyFlag]
_ [Maybe Role]
_ = String -> SDoc -> [Role]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"initialRoleEnv1 (2)"
([SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat [TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc, [Maybe Role] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Maybe Role]
role_annots])
default_role :: Role
default_role
| TyCon -> Bool
isClassTyCon TyCon
tc = Role
Nominal
| HscSource
HsBootFile <- HscSource
hsc_src
, TyCon -> Bool
isAbstractTyCon TyCon
tc = Role
Representational
| HscSource
HsigFile <- HscSource
hsc_src
, TyCon -> Bool
isAbstractTyCon TyCon
tc = Role
Nominal
| Bool
otherwise = Role
Phantom
irGroup :: RoleEnv -> [TyCon] -> RoleEnv
irGroup :: RoleEnv -> [TyCon] -> RoleEnv
irGroup RoleEnv
env [TyCon]
tcs
= let (RoleEnv
env', Bool
update) = RoleEnv -> RoleM () -> (RoleEnv, Bool)
runRoleM RoleEnv
env (RoleM () -> (RoleEnv, Bool)) -> RoleM () -> (RoleEnv, Bool)
forall a b. (a -> b) -> a -> b
$ (TyCon -> RoleM ()) -> [TyCon] -> RoleM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ TyCon -> RoleM ()
irTyCon [TyCon]
tcs in
if Bool
update
then RoleEnv -> [TyCon] -> RoleEnv
irGroup RoleEnv
env' [TyCon]
tcs
else RoleEnv
env'
irTyCon :: TyCon -> RoleM ()
irTyCon :: TyCon -> RoleM ()
irTyCon TyCon
tc
| TyCon -> Bool
isAlgTyCon TyCon
tc
= do { old_roles <- TyCon -> RoleM [Role]
lookupRoles TyCon
tc
; unless (all (== Nominal) old_roles) $
irTcTyVars tc $
do { mapM_ (irType emptyVarSet) (tyConStupidTheta tc)
; whenIsJust (tyConClass_maybe tc) irClass
; mapM_ irDataCon (visibleDataCons $ algTyConRhs tc) }}
| Just Type
ty <- TyCon -> Maybe Type
synTyConRhs_maybe TyCon
tc
= TyCon -> RoleM () -> RoleM ()
forall a. TyCon -> RoleM a -> RoleM a
irTcTyVars TyCon
tc (RoleM () -> RoleM ()) -> RoleM () -> RoleM ()
forall a b. (a -> b) -> a -> b
$
VarSet -> Type -> RoleM ()
irType VarSet
emptyVarSet Type
ty
| Bool
otherwise
= () -> RoleM ()
forall a. a -> RoleM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
irClass :: Class -> RoleM ()
irClass :: Class -> RoleM ()
irClass Class
cls
= (TyCon -> RoleM ()) -> [TyCon] -> RoleM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ TyCon -> RoleM ()
ir_at (Class -> [TyCon]
classATs Class
cls)
where
cls_tvs :: [Id]
cls_tvs = Class -> [Id]
classTyVars Class
cls
cls_tv_set :: VarSet
cls_tv_set = [Id] -> VarSet
mkVarSet [Id]
cls_tvs
ir_at :: TyCon -> RoleM ()
ir_at TyCon
at_tc
= (Id -> RoleM ()) -> [Id] -> RoleM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Role -> Id -> RoleM ()
updateRole Role
Nominal) [Id]
nvars
where nvars :: [Id]
nvars = (Id -> Bool) -> [Id] -> [Id]
forall a. (a -> Bool) -> [a] -> [a]
filter (Id -> VarSet -> Bool
`elemVarSet` VarSet
cls_tv_set) ([Id] -> [Id]) -> [Id] -> [Id]
forall a b. (a -> b) -> a -> b
$ TyCon -> [Id]
tyConTyVars TyCon
at_tc
irDataCon :: DataCon -> RoleM ()
irDataCon :: DataCon -> RoleM ()
irDataCon DataCon
datacon
= [Id] -> RoleM () -> RoleM ()
forall a. [Id] -> RoleM a -> RoleM a
setRoleInferenceVars [Id]
univ_tvs (RoleM () -> RoleM ()) -> RoleM () -> RoleM ()
forall a b. (a -> b) -> a -> b
$
[Id] -> (VarSet -> RoleM ()) -> RoleM ()
forall a. [Id] -> (VarSet -> RoleM a) -> RoleM a
irExTyVars [Id]
ex_tvs ((VarSet -> RoleM ()) -> RoleM ())
-> (VarSet -> RoleM ()) -> RoleM ()
forall a b. (a -> b) -> a -> b
$ \ VarSet
ex_var_set ->
do (Type -> RoleM ()) -> [Type] -> RoleM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (VarSet -> Type -> RoleM ()
irType VarSet
ex_var_set) ([EqSpec] -> [Type]
eqSpecPreds [EqSpec]
eq_spec [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
theta [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ (Scaled Type -> Type) -> [Scaled Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Scaled Type -> Type
forall a. Scaled a -> a
scaledThing [Scaled Type]
arg_tys)
(Type -> RoleM ()) -> [Type] -> RoleM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (VarSet -> Type -> RoleM ()
markNominal VarSet
ex_var_set) ((Id -> Type) -> [Id] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Id -> Type
tyVarKind [Id]
ex_tvs [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ (Scaled Type -> Type) -> [Scaled Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Scaled Type -> Type
forall a. Scaled a -> Type
scaledMult [Scaled Type]
arg_tys)
where
([Id]
univ_tvs, [Id]
ex_tvs, [EqSpec]
eq_spec, [Type]
theta, [Scaled Type]
arg_tys, Type
_res_ty)
= DataCon -> ([Id], [Id], [EqSpec], [Type], [Scaled Type], Type)
dataConFullSig DataCon
datacon
irType :: VarSet -> Type -> RoleM ()
irType :: VarSet -> Type -> RoleM ()
irType = VarSet -> Type -> RoleM ()
go
where
go :: VarSet -> Type -> RoleM ()
go VarSet
lcls Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty
= VarSet -> Type -> RoleM ()
go VarSet
lcls Type
ty'
go VarSet
lcls (TyVarTy Id
tv) = Bool -> RoleM () -> RoleM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Id
tv Id -> VarSet -> Bool
`elemVarSet` VarSet
lcls) (RoleM () -> RoleM ()) -> RoleM () -> RoleM ()
forall a b. (a -> b) -> a -> b
$
Role -> Id -> RoleM ()
updateRole Role
Representational Id
tv
go VarSet
lcls (AppTy Type
t1 Type
t2) = VarSet -> Type -> RoleM ()
go VarSet
lcls Type
t1 RoleM () -> RoleM () -> RoleM ()
forall a b. RoleM a -> RoleM b -> RoleM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> VarSet -> Type -> RoleM ()
markNominal VarSet
lcls Type
t2
go VarSet
lcls (TyConApp TyCon
tc [Type]
tys) = do { roles <- TyCon -> RoleM [Role]
lookupRolesX TyCon
tc
; zipWithM_ (go_app lcls) roles tys }
go VarSet
lcls (ForAllTy ForAllTyBinder
tvb Type
ty) = do { let tv :: Id
tv = ForAllTyBinder -> Id
forall tv argf. VarBndr tv argf -> tv
binderVar ForAllTyBinder
tvb
lcls' :: VarSet
lcls' = VarSet -> Id -> VarSet
extendVarSet VarSet
lcls Id
tv
; VarSet -> Type -> RoleM ()
markNominal VarSet
lcls (Id -> Type
tyVarKind Id
tv)
; VarSet -> Type -> RoleM ()
go VarSet
lcls' Type
ty }
go VarSet
lcls (FunTy FunTyFlag
_ Type
w Type
arg Type
res) = VarSet -> Type -> RoleM ()
markNominal VarSet
lcls Type
w RoleM () -> RoleM () -> RoleM ()
forall a b. RoleM a -> RoleM b -> RoleM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> VarSet -> Type -> RoleM ()
go VarSet
lcls Type
arg RoleM () -> RoleM () -> RoleM ()
forall a b. RoleM a -> RoleM b -> RoleM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> VarSet -> Type -> RoleM ()
go VarSet
lcls Type
res
go VarSet
_ (LitTy {}) = () -> RoleM ()
forall a. a -> RoleM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
go VarSet
lcls (CastTy Type
ty KindCoercion
_) = VarSet -> Type -> RoleM ()
go VarSet
lcls Type
ty
go VarSet
_ (CoercionTy KindCoercion
_) = () -> RoleM ()
forall a. a -> RoleM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
go_app :: VarSet -> Role -> Type -> RoleM ()
go_app VarSet
_ Role
Phantom Type
_ = () -> RoleM ()
forall a. a -> RoleM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
go_app VarSet
lcls Role
Nominal Type
ty = VarSet -> Type -> RoleM ()
markNominal VarSet
lcls Type
ty
go_app VarSet
lcls Role
Representational Type
ty = VarSet -> Type -> RoleM ()
go VarSet
lcls Type
ty
irTcTyVars :: TyCon -> RoleM a -> RoleM a
irTcTyVars :: forall a. TyCon -> RoleM a -> RoleM a
irTcTyVars TyCon
tc RoleM a
thing
= Name -> RoleM a -> RoleM a
forall a. Name -> RoleM a -> RoleM a
setRoleInferenceTc (TyCon -> Name
tyConName TyCon
tc) (RoleM a -> RoleM a) -> RoleM a -> RoleM a
forall a b. (a -> b) -> a -> b
$ [Id] -> RoleM a
go (TyCon -> [Id]
tyConTyVars TyCon
tc)
where
go :: [Id] -> RoleM a
go [] = RoleM a
thing
go (Id
tv:[Id]
tvs) = do { VarSet -> Type -> RoleM ()
markNominal VarSet
emptyVarSet (Id -> Type
tyVarKind Id
tv)
; Id -> RoleM a -> RoleM a
forall a. Id -> RoleM a -> RoleM a
addRoleInferenceVar Id
tv (RoleM a -> RoleM a) -> RoleM a -> RoleM a
forall a b. (a -> b) -> a -> b
$ [Id] -> RoleM a
go [Id]
tvs }
irExTyVars :: [TyVar] -> (TyVarSet -> RoleM a) -> RoleM a
irExTyVars :: forall a. [Id] -> (VarSet -> RoleM a) -> RoleM a
irExTyVars [Id]
orig_tvs VarSet -> RoleM a
thing = VarSet -> [Id] -> RoleM a
go VarSet
emptyVarSet [Id]
orig_tvs
where
go :: VarSet -> [Id] -> RoleM a
go VarSet
lcls [] = VarSet -> RoleM a
thing VarSet
lcls
go VarSet
lcls (Id
tv:[Id]
tvs) = do { VarSet -> Type -> RoleM ()
markNominal VarSet
lcls (Id -> Type
tyVarKind Id
tv)
; VarSet -> [Id] -> RoleM a
go (VarSet -> Id -> VarSet
extendVarSet VarSet
lcls Id
tv) [Id]
tvs }
markNominal :: TyVarSet
-> Type -> RoleM ()
markNominal :: VarSet -> Type -> RoleM ()
markNominal VarSet
lcls Type
ty = let nvars :: [Id]
nvars = FV -> [Id]
fvVarList (VarSet -> FV -> FV
FV.delFVs VarSet
lcls (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ Type -> FV
get_ty_vars Type
ty) in
(Id -> RoleM ()) -> [Id] -> RoleM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Role -> Id -> RoleM ()
updateRole Role
Nominal) [Id]
nvars
where
get_ty_vars :: Type -> FV
get_ty_vars :: Type -> FV
get_ty_vars Type
t | Just Type
t' <- Type -> Maybe Type
coreView Type
t
= Type -> FV
get_ty_vars Type
t'
get_ty_vars (TyVarTy Id
tv) = Id -> FV
unitFV Id
tv
get_ty_vars (AppTy Type
t1 Type
t2) = Type -> FV
get_ty_vars Type
t1 FV -> FV -> FV
`unionFV` Type -> FV
get_ty_vars Type
t2
get_ty_vars (FunTy FunTyFlag
_ Type
w Type
t1 Type
t2) = Type -> FV
get_ty_vars Type
w FV -> FV -> FV
`unionFV` Type -> FV
get_ty_vars Type
t1 FV -> FV -> FV
`unionFV` Type -> FV
get_ty_vars Type
t2
get_ty_vars (TyConApp TyCon
_ [Type]
tys) = (Type -> FV) -> [Type] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV Type -> FV
get_ty_vars [Type]
tys
get_ty_vars (ForAllTy ForAllTyBinder
tvb Type
ty) = ForAllTyBinder -> FV -> FV
tyCoFVsBndr ForAllTyBinder
tvb (Type -> FV
get_ty_vars Type
ty)
get_ty_vars (LitTy {}) = FV
emptyFV
get_ty_vars (CastTy Type
ty KindCoercion
_) = Type -> FV
get_ty_vars Type
ty
get_ty_vars (CoercionTy KindCoercion
_) = FV
emptyFV
lookupRolesX :: TyCon -> RoleM [Role]
lookupRolesX :: TyCon -> RoleM [Role]
lookupRolesX TyCon
tc
= do { roles <- TyCon -> RoleM [Role]
lookupRoles TyCon
tc
; return $ roles ++ repeat Nominal }
lookupRoles :: TyCon -> RoleM [Role]
lookupRoles :: TyCon -> RoleM [Role]
lookupRoles TyCon
tc
= do { env <- RoleM RoleEnv
getRoleEnv
; case lookupNameEnv env (tyConName tc) of
Just [Role]
roles -> [Role] -> RoleM [Role]
forall a. a -> RoleM a
forall (m :: * -> *) a. Monad m => a -> m a
return [Role]
roles
Maybe [Role]
Nothing -> [Role] -> RoleM [Role]
forall a. a -> RoleM a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Role] -> RoleM [Role]) -> [Role] -> RoleM [Role]
forall a b. (a -> b) -> a -> b
$ TyCon -> [Role]
tyConRoles TyCon
tc }
updateRole :: Role -> TyVar -> RoleM ()
updateRole :: Role -> Id -> RoleM ()
updateRole Role
role Id
tv
= do { var_ns <- RoleM VarPositions
getVarNs
; name <- getTyConName
; case lookupVarEnv var_ns tv of
Maybe Int
Nothing -> String -> SDoc -> RoleM ()
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"updateRole" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
name SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
tv SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ VarPositions -> SDoc
forall a. Outputable a => a -> SDoc
ppr VarPositions
var_ns)
Just Int
n -> Name -> Int -> Role -> RoleM ()
updateRoleEnv Name
name Int
n Role
role }
data RoleInferenceState = RIS { RoleInferenceState -> RoleEnv
role_env :: RoleEnv
, RoleInferenceState -> Bool
update :: Bool }
type VarPositions = VarEnv Int
newtype RoleM a = RM { forall a.
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
unRM :: Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState) }
deriving ((forall a b. (a -> b) -> RoleM a -> RoleM b)
-> (forall a b. a -> RoleM b -> RoleM a) -> Functor RoleM
forall a b. a -> RoleM b -> RoleM a
forall a b. (a -> b) -> RoleM a -> RoleM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> RoleM a -> RoleM b
fmap :: forall a b. (a -> b) -> RoleM a -> RoleM b
$c<$ :: forall a b. a -> RoleM b -> RoleM a
<$ :: forall a b. a -> RoleM b -> RoleM a
Functor)
instance Applicative RoleM where
pure :: forall a. a -> RoleM a
pure a
x = (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
forall a.
(Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
RM ((Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a)
-> (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
forall a b. (a -> b) -> a -> b
$ \Maybe Name
_ VarPositions
_ Int
_ RoleInferenceState
state -> (a
x, RoleInferenceState
state)
<*> :: forall a b. RoleM (a -> b) -> RoleM a -> RoleM b
(<*>) = RoleM (a -> b) -> RoleM a -> RoleM b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
instance Monad RoleM where
RoleM a
a >>= :: forall a b. RoleM a -> (a -> RoleM b) -> RoleM b
>>= a -> RoleM b
f = (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (b, RoleInferenceState))
-> RoleM b
forall a.
(Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
RM ((Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (b, RoleInferenceState))
-> RoleM b)
-> (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (b, RoleInferenceState))
-> RoleM b
forall a b. (a -> b) -> a -> b
$ \Maybe Name
m_info VarPositions
vps Int
nvps RoleInferenceState
state ->
let (a
a', RoleInferenceState
state') = RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
forall a.
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
unRM RoleM a
a Maybe Name
m_info VarPositions
vps Int
nvps RoleInferenceState
state in
RoleM b
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (b, RoleInferenceState)
forall a.
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
unRM (a -> RoleM b
f a
a') Maybe Name
m_info VarPositions
vps Int
nvps RoleInferenceState
state'
runRoleM :: RoleEnv -> RoleM () -> (RoleEnv, Bool)
runRoleM :: RoleEnv -> RoleM () -> (RoleEnv, Bool)
runRoleM RoleEnv
env RoleM ()
thing = (RoleEnv
env', Bool
update)
where RIS { role_env :: RoleInferenceState -> RoleEnv
role_env = RoleEnv
env', update :: RoleInferenceState -> Bool
update = Bool
update }
= ((), RoleInferenceState) -> RoleInferenceState
forall a b. (a, b) -> b
snd (((), RoleInferenceState) -> RoleInferenceState)
-> ((), RoleInferenceState) -> RoleInferenceState
forall a b. (a -> b) -> a -> b
$ RoleM ()
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> ((), RoleInferenceState)
forall a.
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
unRM RoleM ()
thing Maybe Name
forall a. Maybe a
Nothing VarPositions
forall a. VarEnv a
emptyVarEnv Int
0 RoleInferenceState
state
state :: RoleInferenceState
state = RIS { role_env :: RoleEnv
role_env = RoleEnv
env
, update :: Bool
update = Bool
False }
setRoleInferenceTc :: Name -> RoleM a -> RoleM a
setRoleInferenceTc :: forall a. Name -> RoleM a -> RoleM a
setRoleInferenceTc Name
name RoleM a
thing = (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
forall a.
(Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
RM ((Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a)
-> (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
forall a b. (a -> b) -> a -> b
$ \Maybe Name
m_name VarPositions
vps Int
nvps RoleInferenceState
state ->
Bool -> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a. HasCallStack => Bool -> a -> a
assert (Maybe Name -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Name
m_name) ((a, RoleInferenceState) -> (a, RoleInferenceState))
-> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a b. (a -> b) -> a -> b
$
Bool -> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a. HasCallStack => Bool -> a -> a
assert (VarPositions -> Bool
forall a. VarEnv a -> Bool
isEmptyVarEnv VarPositions
vps) ((a, RoleInferenceState) -> (a, RoleInferenceState))
-> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a b. (a -> b) -> a -> b
$
Bool -> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a. HasCallStack => Bool -> a -> a
assert (Int
nvps Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0) ((a, RoleInferenceState) -> (a, RoleInferenceState))
-> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a b. (a -> b) -> a -> b
$
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
forall a.
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
unRM RoleM a
thing (Name -> Maybe Name
forall a. a -> Maybe a
Just Name
name) VarPositions
vps Int
nvps RoleInferenceState
state
addRoleInferenceVar :: TyVar -> RoleM a -> RoleM a
addRoleInferenceVar :: forall a. Id -> RoleM a -> RoleM a
addRoleInferenceVar Id
tv RoleM a
thing
= (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
forall a.
(Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
RM ((Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a)
-> (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
forall a b. (a -> b) -> a -> b
$ \Maybe Name
m_name VarPositions
vps Int
nvps RoleInferenceState
state ->
Bool -> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a. HasCallStack => Bool -> a -> a
assert (Maybe Name -> Bool
forall a. Maybe a -> Bool
isJust Maybe Name
m_name) ((a, RoleInferenceState) -> (a, RoleInferenceState))
-> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a b. (a -> b) -> a -> b
$
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
forall a.
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
unRM RoleM a
thing Maybe Name
m_name (VarPositions -> Id -> Int -> VarPositions
forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv VarPositions
vps Id
tv Int
nvps) (Int
nvpsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) RoleInferenceState
state
setRoleInferenceVars :: [TyVar] -> RoleM a -> RoleM a
setRoleInferenceVars :: forall a. [Id] -> RoleM a -> RoleM a
setRoleInferenceVars [Id]
tvs RoleM a
thing
= (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
forall a.
(Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
RM ((Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a)
-> (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
forall a b. (a -> b) -> a -> b
$ \Maybe Name
m_name VarPositions
_vps Int
_nvps RoleInferenceState
state ->
Bool -> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a. HasCallStack => Bool -> a -> a
assert (Maybe Name -> Bool
forall a. Maybe a -> Bool
isJust Maybe Name
m_name) ((a, RoleInferenceState) -> (a, RoleInferenceState))
-> (a, RoleInferenceState) -> (a, RoleInferenceState)
forall a b. (a -> b) -> a -> b
$
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
forall a.
RoleM a
-> Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState)
unRM RoleM a
thing Maybe Name
m_name ([(Id, Int)] -> VarPositions
forall a. [(Id, a)] -> VarEnv a
mkVarEnv ([Id] -> [Int] -> [(Id, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Id]
tvs [Int
0..])) (String -> Int
forall a. HasCallStack => String -> a
panic String
"setRoleInferenceVars")
RoleInferenceState
state
getRoleEnv :: RoleM RoleEnv
getRoleEnv :: RoleM RoleEnv
getRoleEnv = (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (RoleEnv, RoleInferenceState))
-> RoleM RoleEnv
forall a.
(Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
RM ((Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (RoleEnv, RoleInferenceState))
-> RoleM RoleEnv)
-> (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (RoleEnv, RoleInferenceState))
-> RoleM RoleEnv
forall a b. (a -> b) -> a -> b
$ \Maybe Name
_ VarPositions
_ Int
_ state :: RoleInferenceState
state@(RIS { role_env :: RoleInferenceState -> RoleEnv
role_env = RoleEnv
env }) -> (RoleEnv
env, RoleInferenceState
state)
getVarNs :: RoleM VarPositions
getVarNs :: RoleM VarPositions
getVarNs = (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (VarPositions, RoleInferenceState))
-> RoleM VarPositions
forall a.
(Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
RM ((Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (VarPositions, RoleInferenceState))
-> RoleM VarPositions)
-> (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (VarPositions, RoleInferenceState))
-> RoleM VarPositions
forall a b. (a -> b) -> a -> b
$ \Maybe Name
_ VarPositions
vps Int
_ RoleInferenceState
state -> (VarPositions
vps, RoleInferenceState
state)
getTyConName :: RoleM Name
getTyConName :: RoleM Name
getTyConName = (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (Name, RoleInferenceState))
-> RoleM Name
forall a.
(Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
RM ((Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (Name, RoleInferenceState))
-> RoleM Name)
-> (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (Name, RoleInferenceState))
-> RoleM Name
forall a b. (a -> b) -> a -> b
$ \Maybe Name
m_name VarPositions
_ Int
_ RoleInferenceState
state ->
case Maybe Name
m_name of
Maybe Name
Nothing -> String -> (Name, RoleInferenceState)
forall a. HasCallStack => String -> a
panic String
"getTyConName"
Just Name
name -> (Name
name, RoleInferenceState
state)
updateRoleEnv :: Name -> Int -> Role -> RoleM ()
updateRoleEnv :: Name -> Int -> Role -> RoleM ()
updateRoleEnv Name
name Int
n Role
role
= (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> ((), RoleInferenceState))
-> RoleM ()
forall a.
(Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> (a, RoleInferenceState))
-> RoleM a
RM ((Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> ((), RoleInferenceState))
-> RoleM ())
-> (Maybe Name
-> VarPositions
-> Int
-> RoleInferenceState
-> ((), RoleInferenceState))
-> RoleM ()
forall a b. (a -> b) -> a -> b
$ \Maybe Name
_ VarPositions
_ Int
_ state :: RoleInferenceState
state@(RIS { role_env :: RoleInferenceState -> RoleEnv
role_env = RoleEnv
role_env }) -> ((),
case RoleEnv -> Name -> Maybe [Role]
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv RoleEnv
role_env Name
name of
Maybe [Role]
Nothing -> String -> SDoc -> RoleInferenceState
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"updateRoleEnv" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
name)
Just [Role]
roles -> let ([Role]
before, Role
old_role : [Role]
after) = Int -> [Role] -> ([Role], [Role])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [Role]
roles in
if Role
role Role -> Role -> Bool
`ltRole` Role
old_role
then let roles' :: [Role]
roles' = [Role]
before [Role] -> [Role] -> [Role]
forall a. [a] -> [a] -> [a]
++ Role
role Role -> [Role] -> [Role]
forall a. a -> [a] -> [a]
: [Role]
after
role_env' :: RoleEnv
role_env' = RoleEnv -> Name -> [Role] -> RoleEnv
forall a. NameEnv a -> Name -> a -> NameEnv a
extendNameEnv RoleEnv
role_env Name
name [Role]
roles' in
RIS { role_env :: RoleEnv
role_env = RoleEnv
role_env', update :: Bool
update = Bool
True }
else RoleInferenceState
state )
addTyConsToGblEnv :: [TyCon] -> TcM (TcGblEnv, ThBindEnv)
addTyConsToGblEnv :: [TyCon] -> TcM (TcGblEnv, ThBindEnv)
addTyConsToGblEnv [TyCon]
tyclss
= [TyCon] -> TcM (TcGblEnv, ThBindEnv) -> TcM (TcGblEnv, ThBindEnv)
forall r. [TyCon] -> TcM r -> TcM r
tcExtendTyConEnv [TyCon]
tyclss (TcM (TcGblEnv, ThBindEnv) -> TcM (TcGblEnv, ThBindEnv))
-> TcM (TcGblEnv, ThBindEnv) -> TcM (TcGblEnv, ThBindEnv)
forall a b. (a -> b) -> a -> b
$
[TyThing] -> TcM (TcGblEnv, ThBindEnv) -> TcM (TcGblEnv, ThBindEnv)
forall r. [TyThing] -> TcM r -> TcM r
tcExtendGlobalEnvImplicit [TyThing]
implicit_things (TcM (TcGblEnv, ThBindEnv) -> TcM (TcGblEnv, ThBindEnv))
-> TcM (TcGblEnv, ThBindEnv) -> TcM (TcGblEnv, ThBindEnv)
forall a b. (a -> b) -> a -> b
$
[Id] -> TcM (TcGblEnv, ThBindEnv) -> TcM (TcGblEnv, ThBindEnv)
forall a. [Id] -> TcM a -> TcM a
tcExtendGlobalValEnv [Id]
def_meth_ids (TcM (TcGblEnv, ThBindEnv) -> TcM (TcGblEnv, ThBindEnv))
-> TcM (TcGblEnv, ThBindEnv) -> TcM (TcGblEnv, ThBindEnv)
forall a b. (a -> b) -> a -> b
$
do { String -> SDoc -> TcM ()
traceTc String
"tcAddTyCons" (SDoc -> TcM ()) -> SDoc -> TcM ()
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat
[ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"tycons" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> [TyCon] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TyCon]
tyclss
, String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"implicits" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> [TyThing] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TyThing]
implicit_things ]
; gbl_env <- [(Id, LHsBind (GhcPass 'Renamed))] -> TcM TcGblEnv
tcRecSelBinds ([TyCon] -> [(Id, LHsBind (GhcPass 'Renamed))]
mkRecSelBinds [TyCon]
tyclss)
; th_bndrs <- tcTyThBinders implicit_things
; return (gbl_env, th_bndrs)
}
where
implicit_things :: [TyThing]
implicit_things = (TyCon -> [TyThing]) -> [TyCon] -> [TyThing]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap TyCon -> [TyThing]
implicitTyConThings [TyCon]
tyclss
def_meth_ids :: [Id]
def_meth_ids = [TyCon] -> [Id]
mkDefaultMethodIds [TyCon]
tyclss
mkDefaultMethodIds :: [TyCon] -> [Id]
mkDefaultMethodIds :: [TyCon] -> [Id]
mkDefaultMethodIds [TyCon]
tycons
= [ Name -> Type -> Id
mkExportedVanillaId Name
dm_name (Class -> Id -> DefMethSpec Type -> Type
mkDefaultMethodType Class
cls Id
sel_id DefMethSpec Type
dm_spec)
| TyCon
tc <- [TyCon]
tycons
, Just Class
cls <- [TyCon -> Maybe Class
tyConClass_maybe TyCon
tc]
, (Id
sel_id, Just (Name
dm_name, DefMethSpec Type
dm_spec)) <- Class -> [(Id, DefMethInfo)]
classOpItems Class
cls ]
mkDefaultMethodType :: Class -> Id -> DefMethSpec Type -> Type
mkDefaultMethodType :: Class -> Id -> DefMethSpec Type -> Type
mkDefaultMethodType Class
_ Id
sel_id DefMethSpec Type
VanillaDM = Id -> Type
idType Id
sel_id
mkDefaultMethodType Class
cls Id
_ (GenericDM Type
dm_ty) = [ForAllTyBinder] -> [Type] -> Type -> Type
HasDebugCallStack => [ForAllTyBinder] -> [Type] -> Type -> Type
mkSigmaTy [ForAllTyBinder]
tv_bndrs [Type
pred] Type
dm_ty
where
pred :: Type
pred = Class -> [Type] -> Type
mkClassPred Class
cls ([Id] -> [Type]
mkTyVarTys ([TyConBinder] -> [Id]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [TyConBinder]
cls_bndrs))
cls_bndrs :: [TyConBinder]
cls_bndrs = TyCon -> [TyConBinder]
tyConBinders (Class -> TyCon
classTyCon Class
cls)
tv_bndrs :: [ForAllTyBinder]
tv_bndrs = [VarBndr Id Specificity] -> [ForAllTyBinder]
forall a. [VarBndr a Specificity] -> [VarBndr a ForAllTyFlag]
tyVarSpecToBinders ([VarBndr Id Specificity] -> [ForAllTyBinder])
-> [VarBndr Id Specificity] -> [ForAllTyBinder]
forall a b. (a -> b) -> a -> b
$ [TyConBinder] -> [VarBndr Id Specificity]
tyConInvisTVBinders [TyConBinder]
cls_bndrs
tcRecSelBinds :: [(Id, LHsBind GhcRn)] -> TcM TcGblEnv
tcRecSelBinds :: [(Id, LHsBind (GhcPass 'Renamed))] -> TcM TcGblEnv
tcRecSelBinds [(Id, LHsBind (GhcPass 'Renamed))]
sel_bind_prs
= [Id] -> TcM TcGblEnv -> TcM TcGblEnv
forall a. [Id] -> TcM a -> TcM a
tcExtendGlobalValEnv [Id
sel_id | (L SrcSpanAnnA
_ (XSig (IdSig Id
sel_id))) <- [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs] (TcM TcGblEnv -> TcM TcGblEnv) -> TcM TcGblEnv -> TcM TcGblEnv
forall a b. (a -> b) -> a -> b
$
do { (rec_sel_binds, tcg_env) <- TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
-> TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
forall a. TcRn a -> TcRn a
discardWarnings (TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
-> TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv))
-> TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
-> TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
forall a b. (a -> b) -> a -> b
$
Extension
-> TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
-> TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
forall gbl lcl a. Extension -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
setXOptM Extension
LangExt.ImpredicativeTypes (TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
-> TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv))
-> TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
-> TcRn
([(RecFlag, [GenLocated SrcSpanAnnA (HsBindLR GhcTc GhcTc)])],
TcGblEnv)
forall a b. (a -> b) -> a -> b
$
TopLevelFlag
-> [(RecFlag, LHsBinds (GhcPass 'Renamed))]
-> [LSig (GhcPass 'Renamed)]
-> TcM TcGblEnv
-> TcM ([(RecFlag, LHsBinds GhcTc)], TcGblEnv)
forall thing.
TopLevelFlag
-> [(RecFlag, LHsBinds (GhcPass 'Renamed))]
-> [LSig (GhcPass 'Renamed)]
-> TcM thing
-> TcM ([(RecFlag, LHsBinds GhcTc)], thing)
tcValBinds TopLevelFlag
TopLevel [(RecFlag, LHsBinds (GhcPass 'Renamed))]
[(RecFlag,
[GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))])]
binds [LSig (GhcPass 'Renamed)]
[GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs TcM TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
; return (tcg_env `addTypecheckedBinds` map snd rec_sel_binds) }
where
sigs :: [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs = [ SrcSpanAnnA
-> Sig (GhcPass 'Renamed)
-> GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> SrcSpanAnnA
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc) (XXSig (GhcPass 'Renamed) -> Sig (GhcPass 'Renamed)
forall pass. XXSig pass -> Sig pass
XSig (XXSig (GhcPass 'Renamed) -> Sig (GhcPass 'Renamed))
-> XXSig (GhcPass 'Renamed) -> Sig (GhcPass 'Renamed)
forall a b. (a -> b) -> a -> b
$ Id -> IdSig
IdSig Id
sel_id)
| (Id
sel_id, GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))
_) <- [(Id, LHsBind (GhcPass 'Renamed))]
[(Id,
GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)))]
sel_bind_prs
, let loc :: SrcSpan
loc = Id -> SrcSpan
forall a. NamedThing a => a -> SrcSpan
getSrcSpan Id
sel_id ]
binds :: [(RecFlag,
[GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))])]
binds = [(RecFlag
NonRecursive, [GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))
bind]) | (Id
_, GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))
bind) <- [(Id, LHsBind (GhcPass 'Renamed))]
[(Id,
GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)))]
sel_bind_prs]
mkRecSelBinds :: [TyCon] -> [(Id, LHsBind GhcRn)]
mkRecSelBinds :: [TyCon] -> [(Id, LHsBind (GhcPass 'Renamed))]
mkRecSelBinds [TyCon]
tycons
= ((TyCon, FieldLabel)
-> (Id,
GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))))
-> [(TyCon, FieldLabel)]
-> [(Id,
GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)))]
forall a b. (a -> b) -> [a] -> [b]
map (TyCon, FieldLabel) -> (Id, LHsBind (GhcPass 'Renamed))
(TyCon, FieldLabel)
-> (Id,
GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)))
mkRecSelBind [ (TyCon
tc,FieldLabel
fld) | TyCon
tc <- [TyCon]
tycons
, FieldLabel
fld <- TyCon -> [FieldLabel]
tyConFieldLabels TyCon
tc ]
mkRecSelBind :: (TyCon, FieldLabel) -> (Id, LHsBind GhcRn)
mkRecSelBind :: (TyCon, FieldLabel) -> (Id, LHsBind (GhcPass 'Renamed))
mkRecSelBind (TyCon
tycon, FieldLabel
fl)
= [ConLike]
-> RecSelParent
-> FieldLabel
-> FieldSelectors
-> (Id, LHsBind (GhcPass 'Renamed))
mkOneRecordSelector [ConLike]
all_cons (TyCon -> RecSelParent
RecSelData TyCon
tycon) FieldLabel
fl
FieldSelectors
FieldSelectors
where
all_cons :: [ConLike]
all_cons = (DataCon -> ConLike) -> [DataCon] -> [ConLike]
forall a b. (a -> b) -> [a] -> [b]
map DataCon -> ConLike
RealDataCon (TyCon -> [DataCon]
tyConDataCons TyCon
tycon)
mkOneRecordSelector :: [ConLike] -> RecSelParent -> FieldLabel -> FieldSelectors
-> (Id, LHsBind GhcRn)
mkOneRecordSelector :: [ConLike]
-> RecSelParent
-> FieldLabel
-> FieldSelectors
-> (Id, LHsBind (GhcPass 'Renamed))
mkOneRecordSelector [ConLike]
all_cons RecSelParent
idDetails FieldLabel
fl FieldSelectors
has_sel
= (Id
sel_id, SrcSpanAnnA
-> HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)
-> GenLocated
SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> SrcSpanAnnA
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc) HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)
sel_bind)
where
loc :: SrcSpan
loc = Name -> SrcSpan
forall a. NamedThing a => a -> SrcSpan
getSrcSpan Name
sel_name
loc' :: SrcSpanAnnA
loc' = SrcSpan -> SrcSpanAnnA
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc
locn :: SrcSpanAnnN
locn = SrcSpan -> SrcSpanAnnN
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc
locc :: SrcSpanAnnA
locc = SrcSpan -> SrcSpanAnnA
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc
lbl :: FieldLabelString
lbl = FieldLabel -> FieldLabelString
flLabel FieldLabel
fl
sel_name :: Name
sel_name = FieldLabel -> Name
flSelector FieldLabel
fl
sel_lname :: GenLocated SrcSpanAnnN Name
sel_lname = SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
locn Name
sel_name
match_ctxt :: HsMatchContext (GenLocated SrcSpanAnnN Name)
match_ctxt = GenLocated SrcSpanAnnN Name
-> AnnFunRhs -> HsMatchContext (GenLocated SrcSpanAnnN Name)
forall fn. fn -> AnnFunRhs -> HsMatchContext fn
mkPrefixFunRhs GenLocated SrcSpanAnnN Name
sel_lname AnnFunRhs
forall a. NoAnn a => a
noAnn
sel_id :: Id
sel_id = IdDetails -> Name -> Type -> Id
mkExportedLocalId IdDetails
rec_details Name
sel_name Type
sel_ty
rec_sel_info :: RecSelInfo
rec_sel_info@(RSI { rsi_def :: RecSelInfo -> [ConLike]
rsi_def = [ConLike]
cons_w_field })
= [ConLike] -> [FieldLabelString] -> RecSelInfo
conLikesRecSelInfo [ConLike]
all_cons [FieldLabelString
lbl]
con1 :: ConLike
con1 = Bool -> ConLike -> ConLike
forall a. HasCallStack => Bool -> a -> a
assert (Bool -> Bool
not ([ConLike] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ConLike]
cons_w_field)) (ConLike -> ConLike) -> ConLike -> ConLike
forall a b. (a -> b) -> a -> b
$ [ConLike] -> ConLike
forall a. HasCallStack => [a] -> a
head [ConLike]
cons_w_field
rec_details :: IdDetails
rec_details = RecSelId { sel_tycon :: RecSelParent
sel_tycon = RecSelParent
idDetails
, sel_naughty :: Bool
sel_naughty = Bool
is_naughty
, sel_fieldLabel :: FieldLabel
sel_fieldLabel = FieldLabel
fl
, sel_cons :: RecSelInfo
sel_cons = RecSelInfo
rec_sel_info }
([Id]
univ_tvs, [Id]
_, [EqSpec]
_, [Type]
_, [Type]
req_theta, [Scaled Type]
_, Type
data_ty) = ConLike
-> ([Id], [Id], [EqSpec], [Type], [Type], [Scaled Type], Type)
conLikeFullSig ConLike
con1
field_ty :: Type
field_ty = ConLike -> FieldLabelString -> Type
conLikeFieldType ConLike
con1 FieldLabelString
lbl
field_ty_tvs :: VarSet
field_ty_tvs = Type -> VarSet
tyCoVarsOfType Type
field_ty
data_ty_tvs :: VarSet
data_ty_tvs = Type -> VarSet
tyCoVarsOfType Type
data_ty
sel_tvs :: VarSet
sel_tvs = VarSet
field_ty_tvs VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
data_ty_tvs
sel_tvbs :: [VarBndr Id Specificity]
sel_tvbs = (VarBndr Id Specificity -> Bool)
-> [VarBndr Id Specificity] -> [VarBndr Id Specificity]
forall a. (a -> Bool) -> [a] -> [a]
filter (\VarBndr Id Specificity
tvb -> VarBndr Id Specificity -> Id
forall tv argf. VarBndr tv argf -> tv
binderVar VarBndr Id Specificity
tvb Id -> VarSet -> Bool
`elemVarSet` VarSet
sel_tvs) ([VarBndr Id Specificity] -> [VarBndr Id Specificity])
-> [VarBndr Id Specificity] -> [VarBndr Id Specificity]
forall a b. (a -> b) -> a -> b
$
ConLike -> [VarBndr Id Specificity]
conLikeUserTyVarBinders ConLike
con1
is_naughty :: Bool
is_naughty = Bool -> Bool
not Bool
ok_scoping Bool -> Bool -> Bool
|| Bool
no_selectors
ok_scoping :: Bool
ok_scoping = case ConLike
con1 of
RealDataCon {} -> VarSet
field_ty_tvs VarSet -> VarSet -> Bool
`subVarSet` VarSet
data_ty_tvs
PatSynCon {} -> VarSet
field_ty_tvs VarSet -> VarSet -> Bool
`subVarSet` [Id] -> VarSet
mkVarSet [Id]
univ_tvs
no_selectors :: Bool
no_selectors = FieldSelectors
has_sel FieldSelectors -> FieldSelectors -> Bool
forall a. Eq a => a -> a -> Bool
== FieldSelectors
NoFieldSelectors
sel_ty :: Type
sel_ty | Bool
is_naughty = Type
unitTy
| Bool
otherwise = [ForAllTyBinder] -> Type -> Type
mkForAllTys ([VarBndr Id Specificity] -> [ForAllTyBinder]
forall a. [VarBndr a Specificity] -> [VarBndr a ForAllTyFlag]
tyVarSpecToBinders [VarBndr Id Specificity]
sel_tvbs) (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$
[Type] -> Type -> Type
HasDebugCallStack => [Type] -> Type -> Type
mkPhiTy (ConLike -> [Type]
conLikeStupidTheta ConLike
con1) (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$
[Type] -> Type -> Type
HasDebugCallStack => [Type] -> Type -> Type
mkPhiTy [Type]
req_theta (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => Type -> Type -> Type
Type -> Type -> Type
mkVisFunTyMany Type
data_ty (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$
Type
field_ty
sel_bind :: HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)
sel_bind = Origin
-> GenLocated SrcSpanAnnN Name
-> [LMatch (GhcPass 'Renamed) (LHsExpr (GhcPass 'Renamed))]
-> HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)
mkTopFunBind (GenReason -> DoPmc -> Origin
Generated GenReason
OtherExpansion DoPmc
SkipPmc) GenLocated SrcSpanAnnN Name
sel_lname [LMatch (GhcPass 'Renamed) (LHsExpr (GhcPass 'Renamed))]
[GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed))))]
alts
where
alts :: [GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed))))]
alts | Bool
is_naughty = [HsMatchContext (LIdP (NoGhcTc (GhcPass 'Renamed)))
-> LocatedE [LPat (GhcPass 'Renamed)]
-> LocatedA (HsExpr (GhcPass 'Renamed))
-> LMatch (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed)))
forall (p :: Pass) (body :: * -> *).
(Anno (Match (GhcPass p) (LocatedA (body (GhcPass p))))
~ SrcSpanAnnA,
Anno (GRHS (GhcPass p) (LocatedA (body (GhcPass p))))
~ EpAnn NoEpAnns) =>
HsMatchContext (LIdP (NoGhcTc (GhcPass p)))
-> LocatedE [LPat (GhcPass p)]
-> LocatedA (body (GhcPass p))
-> LMatch (GhcPass p) (LocatedA (body (GhcPass p)))
mkSimpleMatch HsMatchContext (LIdP (NoGhcTc (GhcPass 'Renamed)))
HsMatchContext (GenLocated SrcSpanAnnN Name)
match_ctxt ([GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
-> GenLocated
EpaLocation [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA []) LocatedA (HsExpr (GhcPass 'Renamed))
unit_rhs]
| Bool
otherwise = (ConLike
-> GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed)))))
-> [ConLike]
-> [GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed))))]
forall a b. (a -> b) -> [a] -> [b]
map ConLike
-> LMatch (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed)))
ConLike
-> GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed))))
mk_match [ConLike]
cons_w_field [GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed))))]
-> [GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed))))]
-> [GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed))))]
forall a. [a] -> [a] -> [a]
++ [GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed))))]
deflt
mk_match :: ConLike
-> LMatch (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed)))
mk_match ConLike
con = HsMatchContext (LIdP (NoGhcTc (GhcPass 'Renamed)))
-> LocatedE [LPat (GhcPass 'Renamed)]
-> LocatedA (HsExpr (GhcPass 'Renamed))
-> LMatch (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed)))
forall (p :: Pass) (body :: * -> *).
(Anno (Match (GhcPass p) (LocatedA (body (GhcPass p))))
~ SrcSpanAnnA,
Anno (GRHS (GhcPass p) (LocatedA (body (GhcPass p))))
~ EpAnn NoEpAnns) =>
HsMatchContext (LIdP (NoGhcTc (GhcPass p)))
-> LocatedE [LPat (GhcPass p)]
-> LocatedA (body (GhcPass p))
-> LMatch (GhcPass p) (LocatedA (body (GhcPass p)))
mkSimpleMatch HsMatchContext (LIdP (NoGhcTc (GhcPass 'Renamed)))
HsMatchContext (GenLocated SrcSpanAnnN Name)
match_ctxt
(EpaLocation
-> [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
-> GenLocated
EpaLocation [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
forall l e. l -> e -> GenLocated l e
L (SrcSpanAnnA -> EpaLocation
forall a b. (HasLoc a, HasAnnotation b) => a -> b
l2l SrcSpanAnnA
loc') [SrcSpanAnnA
-> Pat (GhcPass 'Renamed)
-> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc' (ConLike -> Pat (GhcPass 'Renamed)
mk_sel_pat ConLike
con)])
(SrcSpanAnnA
-> HsExpr (GhcPass 'Renamed)
-> LocatedA (HsExpr (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc' (XVar (GhcPass 'Renamed)
-> LIdP (GhcPass 'Renamed) -> HsExpr (GhcPass 'Renamed)
forall p. XVar p -> LIdP p -> HsExpr p
HsVar XVar (GhcPass 'Renamed)
NoExtField
noExtField (SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
locn Name
field_var)))
mk_sel_pat :: ConLike -> Pat (GhcPass 'Renamed)
mk_sel_pat ConLike
con = XConPat (GhcPass 'Renamed)
-> XRec (GhcPass 'Renamed) (ConLikeP (GhcPass 'Renamed))
-> HsConPatDetails (GhcPass 'Renamed)
-> Pat (GhcPass 'Renamed)
forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat (GhcPass 'Renamed)
NoExtField
NoExtField (SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
locn (ConLike -> Name
forall a. NamedThing a => a -> Name
getName ConLike
con)) (HsRecFields
(GhcPass 'Renamed)
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)))
-> HsConDetails
(HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
(LPat (GhcPass 'Renamed))
(HsRecFields
(GhcPass 'Renamed)
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))))
forall tyarg arg rec. rec -> HsConDetails tyarg arg rec
RecCon HsRecFields
(GhcPass 'Renamed)
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)))
rec_fields)
rec_fields :: HsRecFields
(GhcPass 'Renamed)
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)))
rec_fields = HsRecFields { rec_ext :: XHsRecFields (GhcPass 'Renamed)
rec_ext = NoExtField
XHsRecFields (GhcPass 'Renamed)
noExtField, rec_flds :: [LHsRecField
(GhcPass 'Renamed)
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)))]
rec_flds = [LHsRecField
(GhcPass 'Renamed)
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)))
GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))))
rec_field], rec_dotdot :: Maybe (XRec (GhcPass 'Renamed) RecFieldsDotDot)
rec_dotdot = Maybe (XRec (GhcPass 'Renamed) RecFieldsDotDot)
forall a. Maybe a
Nothing }
rec_field :: GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))))
rec_field = HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)))
-> GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))))
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (HsFieldBind
{ hfbAnn :: XHsFieldBind (GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
hfbAnn = Maybe (EpToken "=")
XHsFieldBind (GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
forall a. NoAnn a => a
noAnn
, hfbLHS :: GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed))
hfbLHS
= SrcSpanAnnA
-> FieldOcc (GhcPass 'Renamed)
-> GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
locc (XCFieldOcc (GhcPass 'Renamed)
-> LIdP (GhcPass 'Renamed) -> FieldOcc (GhcPass 'Renamed)
forall pass. XCFieldOcc pass -> LIdP pass -> FieldOcc pass
FieldOcc (OccName -> RdrName
mkRdrUnqual (OccName -> RdrName) -> OccName -> RdrName
forall a b. (a -> b) -> a -> b
$ Name -> OccName
nameOccName Name
sel_name) (SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
locn Name
sel_name))
, hfbRHS :: GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
hfbRHS
= SrcSpanAnnA
-> Pat (GhcPass 'Renamed)
-> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc' (XVarPat (GhcPass 'Renamed)
-> LIdP (GhcPass 'Renamed) -> Pat (GhcPass 'Renamed)
forall p. XVarPat p -> LIdP p -> Pat p
VarPat XVarPat (GhcPass 'Renamed)
NoExtField
noExtField (SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
locn Name
field_var))
, hfbPun :: Bool
hfbPun = Bool
False })
field_var :: Name
field_var = Unique -> OccName -> SrcSpan -> Name
mkInternalName (Int -> Unique
mkBuiltinUnique Int
1) (Name -> OccName
forall a. NamedThing a => a -> OccName
getOccName Name
sel_name) SrcSpan
loc
deflt :: [GenLocated
SrcSpanAnnA
(Match (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed))))]
deflt | (ConLike -> Bool) -> [ConLike] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ConLike -> Bool
dealt_with [ConLike]
all_cons = []
| Bool
otherwise = [HsMatchContext (LIdP (NoGhcTc (GhcPass 'Renamed)))
-> LocatedE [LPat (GhcPass 'Renamed)]
-> LocatedA (HsExpr (GhcPass 'Renamed))
-> LMatch (GhcPass 'Renamed) (LocatedA (HsExpr (GhcPass 'Renamed)))
forall (p :: Pass) (body :: * -> *).
(Anno (Match (GhcPass p) (LocatedA (body (GhcPass p))))
~ SrcSpanAnnA,
Anno (GRHS (GhcPass p) (LocatedA (body (GhcPass p))))
~ EpAnn NoEpAnns) =>
HsMatchContext (LIdP (NoGhcTc (GhcPass p)))
-> LocatedE [LPat (GhcPass p)]
-> LocatedA (body (GhcPass p))
-> LMatch (GhcPass p) (LocatedA (body (GhcPass p)))
mkSimpleMatch HsMatchContext (LIdP (NoGhcTc (GhcPass 'Renamed)))
HsMatchContext (GenLocated SrcSpanAnnN Name)
match_ctxt ([GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
-> GenLocated
EpaLocation [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
forall e a. HasAnnotation e => a -> GenLocated e a
wrapGenSpan [LPat (GhcPass 'Renamed)
GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
genWildPat])
(HsExpr (GhcPass 'Renamed)
-> LHsExpr (GhcPass 'Renamed) -> LHsExpr (GhcPass 'Renamed)
genLHsApp
(Name -> HsExpr (GhcPass 'Renamed)
genHsVar (Id -> Name
forall a. NamedThing a => a -> Name
getName Id
rEC_SEL_ERROR_ID))
(HsLit (GhcPass 'Renamed) -> LocatedA (HsExpr (GhcPass 'Renamed))
forall an.
NoAnn an =>
HsLit (GhcPass 'Renamed)
-> LocatedAn an (HsExpr (GhcPass 'Renamed))
genLHsLit HsLit (GhcPass 'Renamed)
msg_lit))]
dealt_with :: ConLike -> Bool
dealt_with :: ConLike -> Bool
dealt_with (PatSynCon PatSyn
_) = Bool
False
dealt_with con :: ConLike
con@(RealDataCon DataCon
dc)
= ConLike
con ConLike -> [ConLike] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ConLike]
cons_w_field Bool -> Bool -> Bool
|| [Type] -> DataCon -> Bool
dataConCannotMatch [Type]
inst_tys DataCon
dc
where
inst_tys :: [Type]
inst_tys = DataCon -> [Type]
dataConResRepTyArgs DataCon
dc
unit_rhs :: LHsExpr (GhcPass 'Renamed)
unit_rhs = [LHsExpr (GhcPass 'Renamed)]
-> XExplicitTuple (GhcPass 'Renamed) -> LHsExpr (GhcPass 'Renamed)
forall (p :: Pass).
[LHsExpr (GhcPass p)]
-> XExplicitTuple (GhcPass p) -> LHsExpr (GhcPass p)
mkLHsTupleExpr [] XExplicitTuple (GhcPass 'Renamed)
NoExtField
noExtField
msg_lit :: HsLit (GhcPass 'Renamed)
msg_lit = XHsStringPrim (GhcPass 'Renamed)
-> ByteString -> HsLit (GhcPass 'Renamed)
forall x. XHsStringPrim x -> ByteString -> HsLit x
HsStringPrim XHsStringPrim (GhcPass 'Renamed)
SourceText
NoSourceText (FastString -> ByteString
bytesFS (FieldLabelString -> FastString
field_label FieldLabelString
lbl))