Skip to content

Commit 9cb6bb8

Browse files
committed
Auto merge of rust-lang#125284 - compiler-errors:uplift-misc, r=lcnr
Uplift `RegionVid`, `TermKind` to `rustc_type_ir`, and `EagerResolver` to `rustc_next_trait_solver` - Uplift `RegionVid`. This was complicated due to the fact that we implement `polonius_engine::Atom` for `RegionVid` -- but I just separated that into `PoloniusRegionVid`, and added `From`/`Into` impls so it can be defined in `rustc_borrowck` separately. Coherence 😵 - Change `InferCtxtLike` to expose `opportunistically_resolve_{ty,ct,lt,int,float}_var` so that we can uplift `EagerResolver` for use in the canonicalization methods. - Uplift `TermKind` much like `GenericArgKind` All of this is miscellaneous dependencies for making more `EvalCtxt` methods generic.
2 parents 8e7517d + 9fa07a4 commit 9cb6bb8

File tree

25 files changed

+309
-238
lines changed

25 files changed

+309
-238
lines changed

compiler/rustc_borrowck/src/facts.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,31 @@ use std::path::Path;
1515
#[derive(Copy, Clone, Debug)]
1616
pub struct RustcFacts;
1717

18+
rustc_index::newtype_index! {
19+
/// A (kinda) newtype of `RegionVid` so we can implement `Atom` on it.
20+
#[orderable]
21+
#[debug_format = "'?{}"]
22+
pub struct PoloniusRegionVid {}
23+
}
24+
25+
impl polonius_engine::Atom for PoloniusRegionVid {
26+
fn index(self) -> usize {
27+
self.as_usize()
28+
}
29+
}
30+
impl From<RegionVid> for PoloniusRegionVid {
31+
fn from(value: RegionVid) -> Self {
32+
Self::from_usize(value.as_usize())
33+
}
34+
}
35+
impl From<PoloniusRegionVid> for RegionVid {
36+
fn from(value: PoloniusRegionVid) -> Self {
37+
Self::from_usize(value.as_usize())
38+
}
39+
}
40+
1841
impl polonius_engine::FactTypes for RustcFacts {
19-
type Origin = RegionVid;
42+
type Origin = PoloniusRegionVid;
2043
type Loan = BorrowIndex;
2144
type Point = LocationIndex;
2245
type Variable = Local;
@@ -119,7 +142,7 @@ trait FactRow {
119142
) -> Result<(), Box<dyn Error>>;
120143
}
121144

122-
impl FactRow for RegionVid {
145+
impl FactRow for PoloniusRegionVid {
123146
fn write(
124147
&self,
125148
out: &mut dyn Write,

compiler/rustc_borrowck/src/polonius/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::TyCtxt;
88
use rustc_mir_dataflow::move_paths::{InitKind, InitLocation, MoveData};
99

1010
use crate::borrow_set::BorrowSet;
11-
use crate::facts::AllFacts;
11+
use crate::facts::{AllFacts, PoloniusRegionVid};
1212
use crate::location::LocationTable;
1313
use crate::type_check::free_region_relations::UniversalRegionRelations;
1414
use crate::universal_regions::UniversalRegions;
@@ -137,7 +137,9 @@ fn emit_universal_region_facts(
137137
// the `borrow_set`, their `BorrowIndex` are synthesized as the universal region index
138138
// added to the existing number of loans, as if they succeeded them in the set.
139139
//
140-
all_facts.universal_region.extend(universal_regions.universal_regions());
140+
all_facts
141+
.universal_region
142+
.extend(universal_regions.universal_regions().map(PoloniusRegionVid::from));
141143
let borrow_count = borrow_set.len();
142144
debug!(
143145
"emit_universal_region_facts: polonius placeholders, num_universals={}, borrow_count={}",
@@ -148,7 +150,7 @@ fn emit_universal_region_facts(
148150
for universal_region in universal_regions.universal_regions() {
149151
let universal_region_idx = universal_region.index();
150152
let placeholder_loan_idx = borrow_count + universal_region_idx;
151-
all_facts.placeholder.push((universal_region, placeholder_loan_idx.into()));
153+
all_facts.placeholder.push((universal_region.into(), placeholder_loan_idx.into()));
152154
}
153155

154156
// 2: the universal region relations `outlives` constraints are emitted as
@@ -160,7 +162,7 @@ fn emit_universal_region_facts(
160162
fr1={:?}, fr2={:?}",
161163
fr1, fr2
162164
);
163-
all_facts.known_placeholder_subset.push((fr1, fr2));
165+
all_facts.known_placeholder_subset.push((fr1.into(), fr2.into()));
164166
}
165167
}
166168
}

compiler/rustc_borrowck/src/region_infer/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1506,22 +1506,22 @@ impl<'tcx> RegionInferenceContext<'tcx> {
15061506
subset_errors.sort();
15071507
subset_errors.dedup();
15081508

1509-
for (longer_fr, shorter_fr) in subset_errors.into_iter() {
1509+
for &(longer_fr, shorter_fr) in subset_errors.into_iter() {
15101510
debug!(
15111511
"check_polonius_subset_errors: subset_error longer_fr={:?},\
15121512
shorter_fr={:?}",
15131513
longer_fr, shorter_fr
15141514
);
15151515

15161516
let propagated = self.try_propagate_universal_region_error(
1517-
*longer_fr,
1518-
*shorter_fr,
1517+
longer_fr.into(),
1518+
shorter_fr.into(),
15191519
&mut propagated_outlives_requirements,
15201520
);
15211521
if propagated == RegionRelationCheckResult::Error {
15221522
errors_buffer.push(RegionErrorKind::RegionError {
1523-
longer_fr: *longer_fr,
1524-
shorter_fr: *shorter_fr,
1523+
longer_fr: longer_fr.into(),
1524+
shorter_fr: shorter_fr.into(),
15251525
fr_origin: NllRegionVariableOrigin::FreeRegion,
15261526
is_reported: true,
15271527
});

compiler/rustc_borrowck/src/type_check/liveness/polonius.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub(super) fn populate_access_facts<'a, 'tcx>(
117117
let universal_regions = &typeck.borrowck_context.universal_regions;
118118
typeck.infcx.tcx.for_each_free_region(&local_decl.ty, |region| {
119119
let region_vid = universal_regions.to_region_vid(region);
120-
facts.use_of_var_derefs_origin.push((local, region_vid));
120+
facts.use_of_var_derefs_origin.push((local, region_vid.into()));
121121
});
122122
}
123123
}
@@ -136,7 +136,7 @@ pub(super) fn add_drop_of_var_derefs_origin<'tcx>(
136136
let universal_regions = &typeck.borrowck_context.universal_regions;
137137
typeck.infcx.tcx.for_each_free_region(kind, |drop_live_region| {
138138
let region_vid = universal_regions.to_region_vid(drop_live_region);
139-
facts.drop_of_var_derefs_origin.push((local, region_vid));
139+
facts.drop_of_var_derefs_origin.push((local, region_vid.into()));
140140
});
141141
}
142142
}

compiler/rustc_borrowck/src/type_check/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,14 @@ fn translate_outlives_facts(typeck: &mut TypeChecker<'_, '_>) {
260260
|constraint: &OutlivesConstraint<'_>| {
261261
if let Some(from_location) = constraint.locations.from_location() {
262262
Either::Left(iter::once((
263-
constraint.sup,
264-
constraint.sub,
263+
constraint.sup.into(),
264+
constraint.sub.into(),
265265
location_table.mid_index(from_location),
266266
)))
267267
} else {
268-
Either::Right(
269-
location_table
270-
.all_points()
271-
.map(move |location| (constraint.sup, constraint.sub, location)),
272-
)
268+
Either::Right(location_table.all_points().map(move |location| {
269+
(constraint.sup.into(), constraint.sub.into(), location)
270+
}))
273271
}
274272
},
275273
));
@@ -2545,7 +2543,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25452543
if let Some(borrow_index) = borrow_set.get_index_of(&location) {
25462544
let region_vid = borrow_region.as_var();
25472545
all_facts.loan_issued_at.push((
2548-
region_vid,
2546+
region_vid.into(),
25492547
borrow_index,
25502548
location_table.mid_index(location),
25512549
));

compiler/rustc_infer/src/infer/mod.rs

+27-17
Original file line numberDiff line numberDiff line change
@@ -369,33 +369,43 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
369369
}
370370
}
371371

372-
fn root_ty_var(&self, vid: TyVid) -> TyVid {
373-
self.root_var(vid)
372+
fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> ty::Region<'tcx> {
373+
self.inner.borrow_mut().unwrap_region_constraints().opportunistic_resolve_var(self.tcx, vid)
374374
}
375375

376-
fn probe_ty_var(&self, vid: TyVid) -> Option<Ty<'tcx>> {
377-
self.probe_ty_var(vid).ok()
376+
fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> {
377+
self.defining_opaque_types
378378
}
379379

380-
fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> Option<ty::Region<'tcx>> {
381-
let re = self
382-
.inner
383-
.borrow_mut()
384-
.unwrap_region_constraints()
385-
.opportunistic_resolve_var(self.tcx, vid);
386-
if *re == ty::ReVar(vid) { None } else { Some(re) }
380+
fn opportunistic_resolve_ty_var(&self, vid: TyVid) -> Ty<'tcx> {
381+
match self.probe_ty_var(vid) {
382+
Ok(ty) => ty,
383+
Err(_) => Ty::new_var(self.tcx, self.root_var(vid)),
384+
}
387385
}
388386

389-
fn root_ct_var(&self, vid: ConstVid) -> ConstVid {
390-
self.root_const_var(vid)
387+
fn opportunistic_resolve_int_var(&self, vid: IntVid) -> Ty<'tcx> {
388+
self.opportunistic_resolve_int_var(vid)
391389
}
392390

393-
fn probe_ct_var(&self, vid: ConstVid) -> Option<ty::Const<'tcx>> {
394-
self.probe_const_var(vid).ok()
391+
fn opportunistic_resolve_float_var(&self, vid: FloatVid) -> Ty<'tcx> {
392+
self.opportunistic_resolve_float_var(vid)
395393
}
396394

397-
fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> {
398-
self.defining_opaque_types
395+
fn opportunistic_resolve_ct_var(&self, vid: ConstVid, ty: Ty<'tcx>) -> ty::Const<'tcx> {
396+
match self.probe_const_var(vid) {
397+
Ok(ct) => ct,
398+
Err(_) => ty::Const::new_var(self.tcx, self.root_const_var(vid), ty),
399+
}
400+
}
401+
402+
fn opportunistic_resolve_effect_var(&self, vid: EffectVid, ty: Ty<'tcx>) -> ty::Const<'tcx> {
403+
match self.probe_effect_var(vid) {
404+
Some(ct) => ct,
405+
None => {
406+
ty::Const::new_infer(self.tcx, InferConst::EffectVar(self.root_effect_var(vid)), ty)
407+
}
408+
}
399409
}
400410
}
401411

compiler/rustc_infer/src/infer/resolve.rs

-81
Original file line numberDiff line numberDiff line change
@@ -173,84 +173,3 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for FullTypeResolver<'a, 'tcx> {
173173
}
174174
}
175175
}
176-
177-
///////////////////////////////////////////////////////////////////////////
178-
// EAGER RESOLUTION
179-
180-
/// Resolves ty, region, and const vars to their inferred values or their root vars.
181-
pub struct EagerResolver<'a, 'tcx> {
182-
infcx: &'a InferCtxt<'tcx>,
183-
}
184-
185-
impl<'a, 'tcx> EagerResolver<'a, 'tcx> {
186-
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
187-
EagerResolver { infcx }
188-
}
189-
}
190-
191-
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for EagerResolver<'_, 'tcx> {
192-
fn interner(&self) -> TyCtxt<'tcx> {
193-
self.infcx.tcx
194-
}
195-
196-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
197-
match *t.kind() {
198-
ty::Infer(ty::TyVar(vid)) => match self.infcx.probe_ty_var(vid) {
199-
Ok(t) => t.fold_with(self),
200-
Err(_) => Ty::new_var(self.infcx.tcx, self.infcx.root_var(vid)),
201-
},
202-
ty::Infer(ty::IntVar(vid)) => self.infcx.opportunistic_resolve_int_var(vid),
203-
ty::Infer(ty::FloatVar(vid)) => self.infcx.opportunistic_resolve_float_var(vid),
204-
_ => {
205-
if t.has_infer() {
206-
t.super_fold_with(self)
207-
} else {
208-
t
209-
}
210-
}
211-
}
212-
}
213-
214-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
215-
match *r {
216-
ty::ReVar(vid) => self
217-
.infcx
218-
.inner
219-
.borrow_mut()
220-
.unwrap_region_constraints()
221-
.opportunistic_resolve_var(self.infcx.tcx, vid),
222-
_ => r,
223-
}
224-
}
225-
226-
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
227-
match c.kind() {
228-
ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
229-
// FIXME: we need to fold the ty too, I think.
230-
match self.infcx.probe_const_var(vid) {
231-
Ok(c) => c.fold_with(self),
232-
Err(_) => {
233-
ty::Const::new_var(self.infcx.tcx, self.infcx.root_const_var(vid), c.ty())
234-
}
235-
}
236-
}
237-
ty::ConstKind::Infer(ty::InferConst::EffectVar(vid)) => {
238-
debug_assert_eq!(c.ty(), self.infcx.tcx.types.bool);
239-
self.infcx.probe_effect_var(vid).unwrap_or_else(|| {
240-
ty::Const::new_infer(
241-
self.infcx.tcx,
242-
ty::InferConst::EffectVar(self.infcx.root_effect_var(vid)),
243-
self.infcx.tcx.types.bool,
244-
)
245-
})
246-
}
247-
_ => {
248-
if c.has_infer() {
249-
c.super_fold_with(self)
250-
} else {
251-
c
252-
}
253-
}
254-
}
255-
}
256-
}

compiler/rustc_middle/src/ty/consts.rs

+8
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ impl<'tcx> Const<'tcx> {
175175
}
176176

177177
impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
178+
fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst, ty: Ty<'tcx>) -> Self {
179+
Const::new_infer(tcx, infer, ty)
180+
}
181+
182+
fn new_var(tcx: TyCtxt<'tcx>, vid: ty::ConstVid, ty: Ty<'tcx>) -> Self {
183+
Const::new_var(tcx, vid, ty)
184+
}
185+
178186
fn new_anon_bound(
179187
tcx: TyCtxt<'tcx>,
180188
debruijn: ty::DebruijnIndex,

compiler/rustc_middle/src/ty/context.rs

-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
134134
type EarlyParamRegion = ty::EarlyParamRegion;
135135
type LateParamRegion = ty::LateParamRegion;
136136
type BoundRegion = ty::BoundRegion;
137-
type InferRegion = ty::RegionVid;
138137
type PlaceholderRegion = ty::PlaceholderRegion;
139138

140139
type ParamEnv = ty::ParamEnv<'tcx>;

compiler/rustc_middle/src/ty/generic_args.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::ops::Deref;
2727
use std::ptr::NonNull;
2828

2929
pub type GenericArgKind<'tcx> = rustc_type_ir::GenericArgKind<TyCtxt<'tcx>>;
30+
pub type TermKind<'tcx> = rustc_type_ir::TermKind<TyCtxt<'tcx>>;
3031

3132
/// An entity in the Rust type system, which can be one of
3233
/// several kinds (types, lifetimes, and consts).

0 commit comments

Comments
 (0)