To avoid having to manually plumb everything in DerivEnv throughout various functions in GHC.Tc.Deriv and GHC.Tc.Deriv.Infer, we use DerivM, which is a simple reader around TcRn.
Moduleghc-9.10.3GHC2021
GHC.Tc.Deriv.Utils
Error-checking and other utilities for deriving clauses or declarations.
- 9 types
- 22 values
- Packageghc-9.10.3
- Exports31
- LanguageGHC2021
- LicenceBSD-3-Clause
- SourceUtils.hs
Contains all of the information known about a derived instance when
determining what its EarlyDerivSpec should be.
See Note [DerivEnv and DerivSpecMechanism].
Constructors
DerivEnvdenv_overlap_mode :: Maybe OverlapModeIs this an overlapping instance?
denv_tvs :: [TyVar]Universally quantified type variables in the instance. If the
denv_ctxtis InferContext, these will be TcTyVar skolems. If thedenv_ctxtis SupplyContext, these will be ordinary TyVars. SeeNote [Overlap and deriving]in GHC.Tc.Deriv.Infer.All type variables that appear in the denv_inst_tys, denv_ctxt, denv_skol_info, and denv_strat should come from denv_tvs.
denv_cls :: ClassClass for which we need to derive an instance
denv_inst_tys :: [Type]All arguments to denv_cls in the derived instance.
denv_ctxt :: DerivContextSupplyContext thetafor standalone deriving (wherethetais the context of the instance). InferContext forderivingclauses, or for standalone deriving that uses a wildcard constraint. SeeNote [Inferring the instance context].denv_skol_info :: SkolemInfoThe SkolemInfo used to skolemise the
denv_tvsin the case where the denv_ctxt is InferContext.denv_strat :: Maybe (DerivStrategy GhcTc)denv_warn :: Maybe (WarningTxt GhcRn)A warning to emit whenever the derived instance is used
Instances1Outputable
Outputable DerivEnvDefined in ghc-9.10.3 · GHC.Tc.Deriv.Utils
Constructors
DSds_loc :: SrcSpands_name :: Nameds_tvs :: [TyVar]ds_theta :: thetads_cls :: Classds_tys :: [Type]ds_skol_info :: SkolemInfods_user_ctxt :: UserTypeCtxtds_overlap :: Maybe OverlapModeds_standalone_wildcard :: Maybe SrcSpands_mechanism :: DerivSpecMechanismds_warn :: Maybe (WarningTxt GhcRn)
Instances1Outputable
Outputable theta => Outputable (DerivSpec theta)Defined in ghc-9.10.3 · GHC.Tc.Deriv.Utils
Zonk the TcTyVars in a DerivSpec to TyVars.
See Note [What is zonking?] in GHC.Tc.Zonk.Type.
This is only used in the final zonking step when inferring
the context for a derived instance.
See Note [Overlap and deriving] in GHC.Tc.Deriv.Infer.
What action to take in order to derive a class instance.
See Note [DerivEnv and DerivSpecMechanism], as well as
Note [Deriving strategies] in GHC.Tc.Deriv.
Constructors
DerivSpecStock"Standard" classes
dsm_stock_dit :: DerivInstTysInformation about the arguments to the class in the derived instance, including what type constructor the last argument is headed by. See
Note [DerivEnv and DerivSpecMechanism].dsm_stock_gen_fns :: StockGenFnsHow to generate the instance bindings and associated type family instances.
DerivSpecNewtypeGeneralizedNewtypeDerivingdsm_newtype_dit :: DerivInstTysInformation about the arguments to the class in the derived instance, including what type constructor the last argument is headed by. See
Note [DerivEnv and DerivSpecMechanism].dsm_newtype_rep_ty :: TypeThe newtype rep type.
DerivSpecAnyClassDeriveAnyClassDerivSpecViaDerivingViadsm_via_cls_tys :: [Type]All arguments to the class besides the last one.
dsm_via_inst_ty :: TypeThe last argument to the class.
dsm_via_ty :: TypeThe
viatype
Instances1Outputable
Outputable DerivSpecMechanismDefined in ghc-9.10.3 · GHC.Tc.Deriv.Utils
Convert a DerivSpecMechanism to its corresponding DerivStrategy.
Zonk the TcTyVars in a DerivSpecMechanism to TyVars.
See Note [What is zonking?] in GHC.Tc.Zonk.Type.
This is only used in the final zonking step when inferring
the context for a derived instance.
See Note [Overlap and deriving] in GHC.Tc.Deriv.Infer.
Whether GHC is processing a deriving clause or a standalone deriving
declaration.
Constructors
InferContext (Maybe SrcSpan)SupplyContext ThetaTypeSupplyContext thetais a standalone deriving declaration, wherethetais the context supplied by the user.
Instances1Outputable
Outputable DerivContextDefined in ghc-9.10.3 · GHC.Tc.Deriv.Utils
Records whether a particular class can be derived by way of an
originative deriving strategy (i.e., stock or anyclass).
See Note [Deriving strategies] in GHC.Tc.Deriv.
Describes how to generate instance bindings (stock_gen_binds) and associated type family instances (stock_gen_fam_insts) for a particular stock-derived instance.
Constructors
StockGenFnsstock_gen_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, [LSig GhcPs], Bag AuxBindSpec, [Name])Describes how to generate instance bindings for a stock-derived instance.
This function takes two arguments:
SrcSpan: the source location where the instance is being derived. This will eventually be instantiated with the ds_loc field of a DerivSpec.
DerivInstTys: information about the argument types to which a class is applied in a derived instance. This will eventually be instantiated with the dsm_stock_dit field of a DerivSpecMechanism.
This function returns four things:
LHsBinds GhcPs: The derived instance's function bindings (e.g.,compare (T x) (T y) = compare x y)[LSig GhcPs]: A list of instance specific signatures/pragmas. Most likelyINLINEpragmas for class methods.Bag AuxBindSpec: Auxiliary bindings needed to support the derived instance. As examples, derived Eq and Ord instances sometimes require top-levelcon2tagfunctions. SeeNote [Auxiliary binders]in GHC.Tc.Deriv.Generate.[Name]: A list of Names for which-Wunused-bindsshould be suppressed. This is used to suppress unused warnings for record selectors when deriving Read, Show, orGeneric. SeeNote [Deriving and unused record selectors].
stock_gen_fam_insts :: SrcSpan -> DerivInstTys -> TcM [FamInst]Describes how to generate associated type family instances for a stock-derived instance. This function takes the same arguments as the stock_gen_binds function but returns a list of FamInsts instead. Generating type family instances is done separately from stock_gen_binds since the type family instances must be generated before the instance bindings can be typechecked. See
Note [Staging of tcDeriving]in GHC.Tc.Deriv.
Is GHC processing a standalone deriving declaration?
Is GHC processing a standalone deriving declaration with an
extra-constraints wildcard as the context?
(e.g., deriving instance _ => Eq (Foo a))
Return InstDeclCtxt if processing with a standalone deriving
declaration or DerivClauseCtxt if processing a deriving clause.
mkDerivOrigin wc returns StandAloneDerivOrigin if wc is True,
and DerivClauseOrigin if wc is False. Useful for error-reporting.
A PredSpec specifies a constraint to emitted when inferring the
instance context for a derived instance in GHC.Tc.Deriv.simplifyInfer.
Constructors
SimplePredSpecAn ordinary PredSpec that directly stores a PredType, which will be emitted as a wanted constraint in the constraint solving machinery. This is the simple case, as there are no skolems, metavariables, or given constraints involved.
sps_pred :: TcPredTypeThe constraint to emit as a wanted
sps_origin :: CtOriginThe origin of the constraint
sps_type_or_kind :: TypeOrKindWhether the constraint is a type or kind
SubTypePredSpecA special PredSpec that is only used by
DeriveAnyClass. This will check ifstps_ty_actualis a subtype of (i.e., more polymorphic than)stps_ty_expectedin the constraint solving machinery, emitting an implication constraint as a side effect. For more details on how this works, seeNote [Gathering and simplifying constraints for DeriveAnyClass]in GHC.Tc.Deriv.Infer.stps_ty_actual :: TcSigmaTypeThe actual type. In the context of
DeriveAnyClass, this is the default method type signature.stps_ty_expected :: TcSigmaTypeThe expected type. In the context of
DeriveAnyClass, this is the original method type signature.stps_origin :: CtOriginThe origin of the constraint
Instances1Outputable
Outputable PredSpecDefined in ghc-9.10.3 · GHC.Tc.Deriv.Utils
A list of PredSpec constraints to simplify when inferring a
derived instance's context. For the stock, newtype, and via deriving
strategies, these will consist of SimplePredSpecs, and for
DeriveAnyClass, these will consist of SubTypePredSpecs. Here is an
example to illustrate the latter:
class Foo a where
bar :: forall b. Ix b => a -> b -> String
default bar :: forall y. (Show a, Ix y) => a -> y -> String
bar x y = show x ++ show (range (y, y))
baz :: Eq a => a -> a -> Bool
default baz :: Ord a => a -> a -> Bool
baz x y = compare x y == EQ
data Quux q = Quux deriving anyclass Foo
Then it would generate two SubTypePredSpecs, one for each method:
[ SubTypePredSpec
{ stps_ty_actual = forall y. (Show (Quux q), Ix y) => Quux q -> y -> String
, stps_ty_expected = forall b. (Ix b) => Quux q -> b -> String
, stps_ty_origin = DerivClauseCtxt
}
, SubTypePredSpec
{ stps_ty_actual = Ord (Quux q) => Quux q -> Quux q -> Bool
, stps_ty_expected = Eq (Quux q) => Quux q -> Quux q -> Bool
, stps_ty_origin = DerivClauseCtxt
}
]
(Note that the type variable q is bound by the data type Quux, and thus
appears free in the stps_ty_actuals and stps_ty_expecteds.)
See Note [Gathering and simplifying constraints for DeriveAnyClass]
in GHC.Tc.Deriv.Infer for an explanation of how these SubTypePredSpecs
are used to compute implication constraints.
Build a list of SimplePredSpecs, using the supplied CtOrigin and TypeOrKind values for each PredType.
captureThetaSpecConstraints :: UserTypeCtxtUsed to inform error messages as to whether we are in a
derivingclause or a standalonederivingdeclaration-> ThetaSpecThe specs from which constraints will be created
-> TcM (TcLevel, WantedConstraints)
Capture wanted constraints from a ThetaSpec.