1 use rustc_infer
::infer
::nll_relate
::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate}
;
2 use rustc_infer
::infer
::NllRegionVariableOrigin
;
3 use rustc_infer
::traits
::PredicateObligations
;
4 use rustc_middle
::mir
::ConstraintCategory
;
5 use rustc_middle
::ty
::relate
::TypeRelation
;
6 use rustc_middle
::ty
::{self, Ty}
;
8 use rustc_trait_selection
::traits
::query
::Fallible
;
10 use crate::constraints
::OutlivesConstraint
;
11 use crate::diagnostics
::UniverseInfo
;
12 use crate::type_check
::{InstantiateOpaqueType, Locations, TypeChecker}
;
14 impl<'a
, 'tcx
> TypeChecker
<'a
, 'tcx
> {
15 /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
17 /// - "Covariant" `a <: b`
18 /// - "Invariant" `a == b`
19 /// - "Contravariant" `a :> b`
21 /// N.B., the type `a` is permitted to have unresolved inference
22 /// variables, but not the type `b`.
23 #[instrument(skip(self), level = "debug")]
24 pub(super) fn relate_types(
30 category
: ConstraintCategory
<'tcx
>,
34 NllTypeRelatingDelegate
::new(self, locations
, category
, UniverseInfo
::relate(a
, b
)),
41 /// Add sufficient constraints to ensure `a == b`. See also [Self::relate_types].
42 pub(super) fn eq_substs(
44 a
: ty
::SubstsRef
<'tcx
>,
45 b
: ty
::SubstsRef
<'tcx
>,
47 category
: ConstraintCategory
<'tcx
>,
51 NllTypeRelatingDelegate
::new(self, locations
, category
, UniverseInfo
::other()),
52 ty
::Variance
::Invariant
,
59 struct NllTypeRelatingDelegate
<'me
, 'bccx
, 'tcx
> {
60 type_checker
: &'me
mut TypeChecker
<'bccx
, 'tcx
>,
62 /// Where (and why) is this relation taking place?
65 /// What category do we assign the resulting `'a: 'b` relationships?
66 category
: ConstraintCategory
<'tcx
>,
68 /// Information so that error reporting knows what types we are relating
69 /// when reporting a bound region error.
70 universe_info
: UniverseInfo
<'tcx
>,
73 impl<'me
, 'bccx
, 'tcx
> NllTypeRelatingDelegate
<'me
, 'bccx
, 'tcx
> {
75 type_checker
: &'me
mut TypeChecker
<'bccx
, 'tcx
>,
77 category
: ConstraintCategory
<'tcx
>,
78 universe_info
: UniverseInfo
<'tcx
>,
80 Self { type_checker, locations, category, universe_info }
84 impl<'tcx
> TypeRelatingDelegate
<'tcx
> for NllTypeRelatingDelegate
<'_
, '_
, 'tcx
> {
85 fn span(&self) -> Span
{
86 self.locations
.span(self.type_checker
.body
)
89 fn param_env(&self) -> ty
::ParamEnv
<'tcx
> {
90 self.type_checker
.param_env
93 fn create_next_universe(&mut self) -> ty
::UniverseIndex
{
94 let universe
= self.type_checker
.infcx
.create_next_universe();
99 .insert(universe
, self.universe_info
.clone());
103 fn next_existential_region_var(&mut self, from_forall
: bool
) -> ty
::Region
<'tcx
> {
104 let origin
= NllRegionVariableOrigin
::Existential { from_forall }
;
105 self.type_checker
.infcx
.next_nll_region_var(origin
)
108 fn next_placeholder_region(&mut self, placeholder
: ty
::PlaceholderRegion
) -> ty
::Region
<'tcx
> {
112 .placeholder_region(self.type_checker
.infcx
, placeholder
)
115 fn generalize_existential(&mut self, universe
: ty
::UniverseIndex
) -> ty
::Region
<'tcx
> {
116 self.type_checker
.infcx
.next_nll_region_var_in_universe(
117 NllRegionVariableOrigin
::Existential { from_forall: false }
,
124 sup
: ty
::Region
<'tcx
>,
125 sub
: ty
::Region
<'tcx
>,
126 info
: ty
::VarianceDiagInfo
<'tcx
>,
128 let sub
= self.type_checker
.borrowck_context
.universal_regions
.to_region_vid(sub
);
129 let sup
= self.type_checker
.borrowck_context
.universal_regions
.to_region_vid(sup
);
130 self.type_checker
.borrowck_context
.constraints
.outlives_constraints
.push(
134 locations
: self.locations
,
135 span
: self.locations
.span(self.type_checker
.body
),
136 category
: self.category
,
143 fn normalization() -> NormalizationStrategy
{
144 NormalizationStrategy
::Eager
147 fn forbid_inference_vars() -> bool
{
151 fn register_obligations(&mut self, obligations
: PredicateObligations
<'tcx
>) {
156 InstantiateOpaqueType
{
158 // These fields are filled in during execution of the operation
160 region_constraints
: None
,