Skip to content

Commit 597090e

Browse files
committed
Re-use TypeChecker instead of passing around some of its fields
1 parent e0aaffd commit 597090e

File tree

2 files changed

+31
-48
lines changed

2 files changed

+31
-48
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,16 +1163,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11631163
locations: Locations,
11641164
category: ConstraintCategory,
11651165
) -> Fallible<()> {
1166-
relate_tys::relate_types(
1167-
self.infcx,
1168-
self.param_env,
1169-
a,
1170-
v,
1171-
b,
1172-
locations,
1173-
category,
1174-
self.borrowck_context,
1175-
)
1166+
relate_tys::relate_types(self, a, v, b, locations, category)
11761167
}
11771168

11781169
/// Try to relate `sub <: sup`

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
2-
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
2+
use rustc_infer::infer::NllRegionVariableOrigin;
33
use rustc_middle::mir::ConstraintCategory;
44
use rustc_middle::ty::relate::TypeRelation;
55
use rustc_middle::ty::{self, Const, Ty};
66
use rustc_trait_selection::traits::query::Fallible;
77

88
use crate::constraints::OutlivesConstraint;
99
use crate::diagnostics::UniverseInfo;
10-
use crate::type_check::{BorrowCheckContext, Locations};
10+
use crate::type_check::{Locations, TypeChecker};
1111

1212
/// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
1313
///
@@ -17,38 +17,26 @@ use crate::type_check::{BorrowCheckContext, Locations};
1717
///
1818
/// N.B., the type `a` is permitted to have unresolved inference
1919
/// variables, but not the type `b`.
20-
#[instrument(skip(infcx, param_env, borrowck_context), level = "debug")]
20+
#[instrument(skip(type_checker), level = "debug")]
2121
pub(super) fn relate_types<'tcx>(
22-
infcx: &InferCtxt<'_, 'tcx>,
23-
param_env: ty::ParamEnv<'tcx>,
22+
type_checker: &mut TypeChecker<'_, 'tcx>,
2423
a: Ty<'tcx>,
2524
v: ty::Variance,
2625
b: Ty<'tcx>,
2726
locations: Locations,
2827
category: ConstraintCategory,
29-
borrowck_context: &mut BorrowCheckContext<'_, 'tcx>,
3028
) -> Fallible<()> {
3129
TypeRelating::new(
32-
infcx,
33-
NllTypeRelatingDelegate::new(
34-
infcx,
35-
borrowck_context,
36-
param_env,
37-
locations,
38-
category,
39-
UniverseInfo::relate(a, b),
40-
),
30+
type_checker.infcx,
31+
NllTypeRelatingDelegate::new(type_checker, locations, category, UniverseInfo::relate(a, b)),
4132
v,
4233
)
4334
.relate(a, b)?;
4435
Ok(())
4536
}
4637

4738
struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
48-
infcx: &'me InferCtxt<'me, 'tcx>,
49-
borrowck_context: &'me mut BorrowCheckContext<'bccx, 'tcx>,
50-
51-
param_env: ty::ParamEnv<'tcx>,
39+
type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
5240

5341
/// Where (and why) is this relation taking place?
5442
locations: Locations,
@@ -63,25 +51,24 @@ struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
6351

6452
impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
6553
fn new(
66-
infcx: &'me InferCtxt<'me, 'tcx>,
67-
borrowck_context: &'me mut BorrowCheckContext<'bccx, 'tcx>,
68-
param_env: ty::ParamEnv<'tcx>,
54+
type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
6955
locations: Locations,
7056
category: ConstraintCategory,
7157
universe_info: UniverseInfo<'tcx>,
7258
) -> Self {
73-
Self { infcx, borrowck_context, param_env, locations, category, universe_info }
59+
Self { type_checker, locations, category, universe_info }
7460
}
7561
}
7662

7763
impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
7864
fn param_env(&self) -> ty::ParamEnv<'tcx> {
79-
self.param_env
65+
self.type_checker.param_env
8066
}
8167

8268
fn create_next_universe(&mut self) -> ty::UniverseIndex {
83-
let universe = self.infcx.create_next_universe();
84-
self.borrowck_context
69+
let universe = self.type_checker.infcx.create_next_universe();
70+
self.type_checker
71+
.borrowck_context
8572
.constraints
8673
.universe_causes
8774
.insert(universe, self.universe_info.clone());
@@ -90,15 +77,18 @@ impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
9077

9178
fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
9279
let origin = NllRegionVariableOrigin::Existential { from_forall };
93-
self.infcx.next_nll_region_var(origin)
80+
self.type_checker.infcx.next_nll_region_var(origin)
9481
}
9582

9683
fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
97-
self.borrowck_context.constraints.placeholder_region(self.infcx, placeholder)
84+
self.type_checker
85+
.borrowck_context
86+
.constraints
87+
.placeholder_region(self.type_checker.infcx, placeholder)
9888
}
9989

10090
fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
101-
self.infcx.next_nll_region_var_in_universe(
91+
self.type_checker.infcx.next_nll_region_var_in_universe(
10292
NllRegionVariableOrigin::Existential { from_forall: false },
10393
universe,
10494
)
@@ -110,15 +100,17 @@ impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
110100
sub: ty::Region<'tcx>,
111101
info: ty::VarianceDiagInfo<'tcx>,
112102
) {
113-
let sub = self.borrowck_context.universal_regions.to_region_vid(sub);
114-
let sup = self.borrowck_context.universal_regions.to_region_vid(sup);
115-
self.borrowck_context.constraints.outlives_constraints.push(OutlivesConstraint {
116-
sup,
117-
sub,
118-
locations: self.locations,
119-
category: self.category,
120-
variance_info: info,
121-
});
103+
let sub = self.type_checker.borrowck_context.universal_regions.to_region_vid(sub);
104+
let sup = self.type_checker.borrowck_context.universal_regions.to_region_vid(sup);
105+
self.type_checker.borrowck_context.constraints.outlives_constraints.push(
106+
OutlivesConstraint {
107+
sup,
108+
sub,
109+
locations: self.locations,
110+
category: self.category,
111+
variance_info: info,
112+
},
113+
);
122114
}
123115

124116
// We don't have to worry about the equality of consts during borrow checking

0 commit comments

Comments
 (0)