Skip to content

Commit c6f4513

Browse files
committed
extract out NLL-specific code from relate-tys into a delegate
No functional change.
1 parent a4955d1 commit c6f4513

File tree

1 file changed

+84
-53
lines changed

1 file changed

+84
-53
lines changed

src/librustc_mir/borrow_check/nll/type_check/relate_tys.rs

Lines changed: 84 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ pub(super) fn sub_types<'tcx>(
3232
debug!("sub_types(a={:?}, b={:?}, locations={:?})", a, b, locations);
3333
TypeRelating::new(
3434
infcx,
35+
NllTypeRelatingDelegate::new(borrowck_context, locations, category),
3536
ty::Variance::Covariant,
36-
locations,
37-
category,
38-
borrowck_context,
3937
ty::List::empty(),
4038
).relate(&a, &b)?;
4139
Ok(())
@@ -53,10 +51,8 @@ pub(super) fn eq_types<'tcx>(
5351
debug!("eq_types(a={:?}, b={:?}, locations={:?})", a, b, locations);
5452
TypeRelating::new(
5553
infcx,
54+
NllTypeRelatingDelegate::new(borrowck_context, locations, category),
5655
ty::Variance::Invariant,
57-
locations,
58-
category,
59-
borrowck_context,
6056
ty::List::empty(),
6157
).relate(&a, &b)?;
6258
Ok(())
@@ -90,17 +86,21 @@ pub(super) fn relate_type_and_user_type<'tcx>(
9086

9187
TypeRelating::new(
9288
infcx,
89+
NllTypeRelatingDelegate::new(borrowck_context, locations, category),
9390
v1,
94-
locations,
95-
category,
96-
borrowck_context,
9791
b_variables,
9892
).relate(&b_value, &a)?;
9993
Ok(())
10094
}
10195

102-
struct TypeRelating<'cx, 'bccx: 'cx, 'gcx: 'tcx, 'tcx: 'bccx> {
103-
infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
96+
struct TypeRelating<'me, 'gcx: 'tcx, 'tcx: 'me, D>
97+
where
98+
D: TypeRelatingDelegate<'tcx>,
99+
{
100+
infcx: &'me InferCtxt<'me, 'gcx, 'tcx>,
101+
102+
/// Callback to use when we deduce an outlives relationship
103+
delegate: D,
104104

105105
/// How are we relating `a` and `b`?
106106
///
@@ -125,15 +125,6 @@ struct TypeRelating<'cx, 'bccx: 'cx, 'gcx: 'tcx, 'tcx: 'bccx> {
125125
/// Same as `a_scopes`, but for the `b` type.
126126
b_scopes: Vec<BoundRegionScope<'tcx>>,
127127

128-
/// Where (and why) is this relation taking place?
129-
locations: Locations,
130-
131-
category: ConstraintCategory,
132-
133-
/// This will be `Some` when we are running the type check as part
134-
/// of NLL, and `None` if we are running a "sanity check".
135-
borrowck_context: Option<&'cx mut BorrowCheckContext<'bccx, 'tcx>>,
136-
137128
/// As we execute, the type on the LHS *may* come from a canonical
138129
/// source. In that case, we will sometimes find a constraint like
139130
/// `?0 = B`, where `B` is a type from the RHS. The first time we
@@ -148,6 +139,54 @@ struct TypeRelating<'cx, 'bccx: 'cx, 'gcx: 'tcx, 'tcx: 'bccx> {
148139
canonical_var_values: IndexVec<CanonicalVar, Option<Kind<'tcx>>>,
149140
}
150141

142+
trait TypeRelatingDelegate<'tcx> {
143+
fn push_outlives(&mut self, sup: ty::Region<'tcx>, sub: ty::Region<'tcx>);
144+
}
145+
146+
struct NllTypeRelatingDelegate<'me, 'bccx: 'me, 'tcx: 'bccx> {
147+
borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>,
148+
149+
/// Where (and why) is this relation taking place?
150+
locations: Locations,
151+
152+
/// What category do we assign the resulting `'a: 'b` relationships?
153+
category: ConstraintCategory,
154+
}
155+
156+
impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
157+
fn new(
158+
borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>,
159+
locations: Locations,
160+
category: ConstraintCategory,
161+
) -> Self {
162+
Self {
163+
borrowck_context,
164+
locations,
165+
category,
166+
}
167+
}
168+
}
169+
170+
impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
171+
fn push_outlives(&mut self, sup: ty::Region<'tcx>, sub: ty::Region<'tcx>) {
172+
if let Some(borrowck_context) = &mut self.borrowck_context {
173+
let sub = borrowck_context.universal_regions.to_region_vid(sub);
174+
let sup = borrowck_context.universal_regions.to_region_vid(sup);
175+
borrowck_context
176+
.constraints
177+
.outlives_constraints
178+
.push(OutlivesConstraint {
179+
sup,
180+
sub,
181+
locations: self.locations,
182+
category: self.category,
183+
});
184+
185+
// FIXME all facts!
186+
}
187+
}
188+
}
189+
151190
#[derive(Clone, Debug)]
152191
struct ScopesAndKind<'tcx> {
153192
scopes: Vec<BoundRegionScope<'tcx>>,
@@ -162,23 +201,22 @@ struct BoundRegionScope<'tcx> {
162201
#[derive(Copy, Clone)]
163202
struct UniversallyQuantified(bool);
164203

165-
impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelating<'cx, 'bccx, 'gcx, 'tcx> {
204+
impl<'me, 'gcx, 'tcx, D> TypeRelating<'me, 'gcx, 'tcx, D>
205+
where
206+
D: TypeRelatingDelegate<'tcx>,
207+
{
166208
fn new(
167-
infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
209+
infcx: &'me InferCtxt<'me, 'gcx, 'tcx>,
210+
delegate: D,
168211
ambient_variance: ty::Variance,
169-
locations: Locations,
170-
category: ConstraintCategory,
171-
borrowck_context: Option<&'cx mut BorrowCheckContext<'bccx, 'tcx>>,
172212
canonical_var_infos: CanonicalVarInfos<'tcx>,
173213
) -> Self {
174214
let canonical_var_values = IndexVec::from_elem_n(None, canonical_var_infos.len());
175215
Self {
176216
infcx,
217+
delegate,
177218
ambient_variance,
178-
borrowck_context,
179-
locations,
180219
canonical_var_values,
181-
category,
182220
a_scopes: vec![],
183221
b_scopes: vec![],
184222
}
@@ -259,21 +297,7 @@ impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelating<'cx, 'bccx, 'gcx, 'tcx> {
259297
fn push_outlives(&mut self, sup: ty::Region<'tcx>, sub: ty::Region<'tcx>) {
260298
debug!("push_outlives({:?}: {:?})", sup, sub);
261299

262-
if let Some(borrowck_context) = &mut self.borrowck_context {
263-
let sub = borrowck_context.universal_regions.to_region_vid(sub);
264-
let sup = borrowck_context.universal_regions.to_region_vid(sup);
265-
borrowck_context
266-
.constraints
267-
.outlives_constraints
268-
.push(OutlivesConstraint {
269-
sup,
270-
sub,
271-
locations: self.locations,
272-
category: self.category,
273-
});
274-
275-
// FIXME all facts!
276-
}
300+
self.delegate.push_outlives(sup, sub);
277301
}
278302

279303
/// When we encounter a canonical variable `var` in the output,
@@ -325,10 +349,11 @@ impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelating<'cx, 'bccx, 'gcx, 'tcx> {
325349
}
326350
}
327351

328-
impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx>
329-
for TypeRelating<'cx, 'bccx, 'gcx, 'tcx>
352+
impl<D> TypeRelation<'me, 'gcx, 'tcx> for TypeRelating<'me, 'gcx, 'tcx, D>
353+
where
354+
D: TypeRelatingDelegate<'tcx>,
330355
{
331-
fn tcx(&self) -> TyCtxt<'cx, 'gcx, 'tcx> {
356+
fn tcx(&self) -> TyCtxt<'me, 'gcx, 'tcx> {
332357
self.infcx.tcx
333358
}
334359

@@ -534,15 +559,15 @@ impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx>
534559
/// binder depth, and finds late-bound regions targeting the
535560
/// `for<..`>. For each of those, it creates an entry in
536561
/// `bound_region_scope`.
537-
struct ScopeInstantiator<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
538-
infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
562+
struct ScopeInstantiator<'me, 'gcx: 'tcx, 'tcx: 'me> {
563+
infcx: &'me InferCtxt<'me, 'gcx, 'tcx>,
539564
// The debruijn index of the scope we are instantiating.
540565
target_index: ty::DebruijnIndex,
541566
universally_quantified: UniversallyQuantified,
542-
bound_region_scope: &'cx mut BoundRegionScope<'tcx>,
567+
bound_region_scope: &'me mut BoundRegionScope<'tcx>,
543568
}
544569

545-
impl<'cx, 'gcx, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'cx, 'gcx, 'tcx> {
570+
impl<'me, 'gcx, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'me, 'gcx, 'tcx> {
546571
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> bool {
547572
self.target_index.shift_in(1);
548573
t.super_visit_with(self);
@@ -596,8 +621,11 @@ impl<'cx, 'gcx, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'cx, 'gcx, 'tcx> {
596621
/// scopes.
597622
///
598623
/// [blog post]: https://is.gd/0hKvIr
599-
struct TypeGeneralizer<'me, 'bccx: 'me, 'gcx: 'tcx, 'tcx: 'bccx> {
600-
type_rel: &'me TypeRelating<'me, 'bccx, 'gcx, 'tcx>,
624+
struct TypeGeneralizer<'me, 'gcx: 'tcx, 'tcx: 'me, D>
625+
where
626+
D: TypeRelatingDelegate<'tcx> + 'me,
627+
{
628+
type_rel: &'me TypeRelating<'me, 'gcx, 'tcx, D>,
601629

602630
/// After we generalize this type, we are going to relative it to
603631
/// some other type. What will be the variance at this point?
@@ -608,7 +636,10 @@ struct TypeGeneralizer<'me, 'bccx: 'me, 'gcx: 'tcx, 'tcx: 'bccx> {
608636
universe: ty::UniverseIndex,
609637
}
610638

611-
impl TypeRelation<'me, 'gcx, 'tcx> for TypeGeneralizer<'me, 'bbcx, 'gcx, 'tcx> {
639+
impl<D> TypeRelation<'me, 'gcx, 'tcx> for TypeGeneralizer<'me, 'gcx, 'tcx, D>
640+
where
641+
D: TypeRelatingDelegate<'tcx>,
642+
{
612643
fn tcx(&self) -> TyCtxt<'me, 'gcx, 'tcx> {
613644
self.type_rel.infcx.tcx
614645
}

0 commit comments

Comments
 (0)