Skip to content

Commit fde2412

Browse files
authored
Rollup merge of #89675 - oli-obk:type_checker, r=davidtwco
Re-use TypeChecker instead of passing around some of its fields In the future (for lazy TAIT) we will need more of its fields, but even ignoring that, this change seems reasonable on its own to me.
2 parents d3984e1 + 49b06a2 commit fde2412

File tree

2 files changed

+53
-81
lines changed

2 files changed

+53
-81
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,28 +1153,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11531153
.convert_all(data);
11541154
}
11551155

1156-
/// Convenient wrapper around `relate_tys::relate_types` -- see
1157-
/// that fn for docs.
1158-
fn relate_types(
1159-
&mut self,
1160-
a: Ty<'tcx>,
1161-
v: ty::Variance,
1162-
b: Ty<'tcx>,
1163-
locations: Locations,
1164-
category: ConstraintCategory,
1165-
) -> 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-
)
1176-
}
1177-
11781156
/// Try to relate `sub <: sup`
11791157
fn sub_types(
11801158
&mut self,

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,44 @@
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};
11-
12-
/// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
13-
///
14-
/// - "Covariant" `a <: b`
15-
/// - "Invariant" `a == b`
16-
/// - "Contravariant" `a :> b`
17-
///
18-
/// N.B., the type `a` is permitted to have unresolved inference
19-
/// variables, but not the type `b`.
20-
#[instrument(skip(infcx, param_env, borrowck_context), level = "debug")]
21-
pub(super) fn relate_types<'tcx>(
22-
infcx: &InferCtxt<'_, 'tcx>,
23-
param_env: ty::ParamEnv<'tcx>,
24-
a: Ty<'tcx>,
25-
v: ty::Variance,
26-
b: Ty<'tcx>,
27-
locations: Locations,
28-
category: ConstraintCategory,
29-
borrowck_context: &mut BorrowCheckContext<'_, 'tcx>,
30-
) -> Fallible<()> {
31-
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-
),
41-
v,
42-
)
43-
.relate(a, b)?;
44-
Ok(())
10+
use crate::type_check::{Locations, TypeChecker};
11+
12+
impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13+
/// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
14+
///
15+
/// - "Covariant" `a <: b`
16+
/// - "Invariant" `a == b`
17+
/// - "Contravariant" `a :> b`
18+
///
19+
/// N.B., the type `a` is permitted to have unresolved inference
20+
/// variables, but not the type `b`.
21+
#[instrument(skip(self), level = "debug")]
22+
pub(super) fn relate_types(
23+
&mut self,
24+
a: Ty<'tcx>,
25+
v: ty::Variance,
26+
b: Ty<'tcx>,
27+
locations: Locations,
28+
category: ConstraintCategory,
29+
) -> Fallible<()> {
30+
TypeRelating::new(
31+
self.infcx,
32+
NllTypeRelatingDelegate::new(self, locations, category, UniverseInfo::relate(a, b)),
33+
v,
34+
)
35+
.relate(a, b)?;
36+
Ok(())
37+
}
4538
}
4639

4740
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>,
41+
type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
5242

5343
/// Where (and why) is this relation taking place?
5444
locations: Locations,
@@ -63,25 +53,24 @@ struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
6353

6454
impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
6555
fn new(
66-
infcx: &'me InferCtxt<'me, 'tcx>,
67-
borrowck_context: &'me mut BorrowCheckContext<'bccx, 'tcx>,
68-
param_env: ty::ParamEnv<'tcx>,
56+
type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
6957
locations: Locations,
7058
category: ConstraintCategory,
7159
universe_info: UniverseInfo<'tcx>,
7260
) -> Self {
73-
Self { infcx, borrowck_context, param_env, locations, category, universe_info }
61+
Self { type_checker, locations, category, universe_info }
7462
}
7563
}
7664

7765
impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
7866
fn param_env(&self) -> ty::ParamEnv<'tcx> {
79-
self.param_env
67+
self.type_checker.param_env
8068
}
8169

8270
fn create_next_universe(&mut self) -> ty::UniverseIndex {
83-
let universe = self.infcx.create_next_universe();
84-
self.borrowck_context
71+
let universe = self.type_checker.infcx.create_next_universe();
72+
self.type_checker
73+
.borrowck_context
8574
.constraints
8675
.universe_causes
8776
.insert(universe, self.universe_info.clone());
@@ -90,15 +79,18 @@ impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
9079

9180
fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
9281
let origin = NllRegionVariableOrigin::Existential { from_forall };
93-
self.infcx.next_nll_region_var(origin)
82+
self.type_checker.infcx.next_nll_region_var(origin)
9483
}
9584

9685
fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
97-
self.borrowck_context.constraints.placeholder_region(self.infcx, placeholder)
86+
self.type_checker
87+
.borrowck_context
88+
.constraints
89+
.placeholder_region(self.type_checker.infcx, placeholder)
9890
}
9991

10092
fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
101-
self.infcx.next_nll_region_var_in_universe(
93+
self.type_checker.infcx.next_nll_region_var_in_universe(
10294
NllRegionVariableOrigin::Existential { from_forall: false },
10395
universe,
10496
)
@@ -110,15 +102,17 @@ impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
110102
sub: ty::Region<'tcx>,
111103
info: ty::VarianceDiagInfo<'tcx>,
112104
) {
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-
});
105+
let sub = self.type_checker.borrowck_context.universal_regions.to_region_vid(sub);
106+
let sup = self.type_checker.borrowck_context.universal_regions.to_region_vid(sup);
107+
self.type_checker.borrowck_context.constraints.outlives_constraints.push(
108+
OutlivesConstraint {
109+
sup,
110+
sub,
111+
locations: self.locations,
112+
category: self.category,
113+
variance_info: info,
114+
},
115+
);
122116
}
123117

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

0 commit comments

Comments
 (0)