Skip to content

Commit 4e02a92

Browse files
committed
Auto merge of #98041 - jackh726:remove-regionckmode, r=oli-obk
Remove RegionckMode in favor of calling new skip_region_resolution Simple cleanup. We can skip a bunch of stuff for places where NLL does the region checking, so skip earlier. r? rust-lang/types
2 parents a2ecbf8 + d716245 commit 4e02a92

File tree

8 files changed

+52
-80
lines changed

8 files changed

+52
-80
lines changed

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+5-28
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::infer::region_constraints::VarInfos;
77
use crate::infer::region_constraints::VerifyBound;
88
use crate::infer::RegionRelations;
99
use crate::infer::RegionVariableOrigin;
10-
use crate::infer::RegionckMode;
1110
use crate::infer::SubregionOrigin;
1211
use rustc_data_structures::fx::FxHashSet;
1312
use rustc_data_structures::graph::implementation::{
@@ -33,32 +32,23 @@ pub(crate) fn resolve<'tcx>(
3332
region_rels: &RegionRelations<'_, 'tcx>,
3433
var_infos: VarInfos,
3534
data: RegionConstraintData<'tcx>,
36-
mode: RegionckMode,
3735
) -> (LexicalRegionResolutions<'tcx>, Vec<RegionResolutionError<'tcx>>) {
3836
let mut errors = vec![];
3937
let mut resolver = LexicalResolver { region_rels, var_infos, data };
40-
match mode {
41-
RegionckMode::Solve => {
42-
let values = resolver.infer_variable_values(&mut errors);
43-
(values, errors)
44-
}
45-
RegionckMode::Erase => {
46-
// Skip region inference entirely.
47-
(resolver.erased_data(region_rels.tcx), Vec::new())
48-
}
49-
}
38+
let values = resolver.infer_variable_values(&mut errors);
39+
(values, errors)
5040
}
5141

5242
/// Contains the result of lexical region resolution. Offers methods
5343
/// to lookup up the final value of a region variable.
5444
#[derive(Clone)]
5545
pub struct LexicalRegionResolutions<'tcx> {
56-
values: IndexVec<RegionVid, VarValue<'tcx>>,
57-
error_region: ty::Region<'tcx>,
46+
pub(crate) values: IndexVec<RegionVid, VarValue<'tcx>>,
47+
pub(crate) error_region: ty::Region<'tcx>,
5848
}
5949

6050
#[derive(Copy, Clone, Debug)]
61-
enum VarValue<'tcx> {
51+
pub(crate) enum VarValue<'tcx> {
6252
Value(Region<'tcx>),
6353
ErrorValue,
6454
}
@@ -162,19 +152,6 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
162152
}
163153
}
164154

165-
/// An erased version of the lexical region resolutions. Used when we're
166-
/// erasing regions and suppressing errors: in item bodies with
167-
/// `-Zborrowck=mir`.
168-
fn erased_data(&self, tcx: TyCtxt<'tcx>) -> LexicalRegionResolutions<'tcx> {
169-
LexicalRegionResolutions {
170-
error_region: tcx.lifetimes.re_static,
171-
values: IndexVec::from_elem_n(
172-
VarValue::Value(tcx.lifetimes.re_erased),
173-
self.num_vars(),
174-
),
175-
}
176-
}
177-
178155
fn dump_constraints(&self, free_regions: &RegionRelations<'_, 'tcx>) {
179156
debug!("----() Start constraint listing (context={:?}) ()----", free_regions.context);
180157
for (idx, (constraint, _)) in self.data.constraints.iter().enumerate() {

compiler/rustc_infer/src/infer/mod.rs

+29-18
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,6 @@ pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
8484
ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
8585
>;
8686

87-
/// How we should handle region solving.
88-
///
89-
/// This is used so that the region values inferred by HIR region solving are
90-
/// not exposed, and so that we can avoid doing work in HIR typeck that MIR
91-
/// typeck will also do.
92-
#[derive(Copy, Clone, Debug, Default)]
93-
pub enum RegionckMode {
94-
/// The default mode: report region errors, don't erase regions.
95-
#[default]
96-
Solve,
97-
/// Erase the results of region after solving.
98-
Erase,
99-
}
100-
10187
/// This type contains all the things within `InferCtxt` that sit within a
10288
/// `RefCell` and are involved with taking/rolling back snapshots. Snapshot
10389
/// operations are hot enough that we want only one call to `borrow_mut` per
@@ -1248,6 +1234,33 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12481234
self.tainted_by_errors_flag.set(true)
12491235
}
12501236

1237+
pub fn skip_region_resolution(&self) {
1238+
let (var_infos, _) = {
1239+
let mut inner = self.inner.borrow_mut();
1240+
let inner = &mut *inner;
1241+
// Note: `inner.region_obligations` may not be empty, because we
1242+
// didn't necessarily call `process_registered_region_obligations`.
1243+
// This is okay, because that doesn't introduce new vars.
1244+
inner
1245+
.region_constraint_storage
1246+
.take()
1247+
.expect("regions already resolved")
1248+
.with_log(&mut inner.undo_log)
1249+
.into_infos_and_data()
1250+
};
1251+
1252+
let lexical_region_resolutions = LexicalRegionResolutions {
1253+
error_region: self.tcx.lifetimes.re_static,
1254+
values: rustc_index::vec::IndexVec::from_elem_n(
1255+
crate::infer::lexical_region_resolve::VarValue::Value(self.tcx.lifetimes.re_erased),
1256+
var_infos.len(),
1257+
),
1258+
};
1259+
1260+
let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
1261+
assert!(old_value.is_none());
1262+
}
1263+
12511264
/// Process the region constraints and return any any errors that
12521265
/// result. After this, no more unification operations should be
12531266
/// done -- or the compiler will panic -- but it is legal to use
@@ -1256,7 +1269,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12561269
&self,
12571270
region_context: DefId,
12581271
outlives_env: &OutlivesEnvironment<'tcx>,
1259-
mode: RegionckMode,
12601272
) -> Vec<RegionResolutionError<'tcx>> {
12611273
let (var_infos, data) = {
12621274
let mut inner = self.inner.borrow_mut();
@@ -1278,7 +1290,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12781290
&RegionRelations::new(self.tcx, region_context, outlives_env.free_region_map());
12791291

12801292
let (lexical_region_resolutions, errors) =
1281-
lexical_region_resolve::resolve(region_rels, var_infos, data, mode);
1293+
lexical_region_resolve::resolve(region_rels, var_infos, data);
12821294

12831295
let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
12841296
assert!(old_value.is_none());
@@ -1294,9 +1306,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12941306
&self,
12951307
region_context: DefId,
12961308
outlives_env: &OutlivesEnvironment<'tcx>,
1297-
mode: RegionckMode,
12981309
) {
1299-
let errors = self.resolve_regions(region_context, outlives_env, mode);
1310+
let errors = self.resolve_regions(region_context, outlives_env);
13001311

13011312
if !self.is_tainted_by_errors() {
13021313
// As a heuristic, just skip reporting region errors

compiler/rustc_trait_selection/src/traits/coherence.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! [trait-specialization]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html
66
77
use crate::infer::outlives::env::OutlivesEnvironment;
8-
use crate::infer::{CombinedSnapshot, InferOk, RegionckMode};
8+
use crate::infer::{CombinedSnapshot, InferOk};
99
use crate::traits::select::IntercrateAmbiguityCause;
1010
use crate::traits::util::impl_subject_and_oblig;
1111
use crate::traits::SkipLeakCheck;
@@ -413,7 +413,7 @@ fn resolve_negative_obligation<'cx, 'tcx>(
413413
param_env,
414414
);
415415

416-
let errors = infcx.resolve_regions(region_context, &outlives_env, RegionckMode::default());
416+
let errors = infcx.resolve_regions(region_context, &outlives_env);
417417

418418
if !errors.is_empty() {
419419
return false;

compiler/rustc_trait_selection/src/traits/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mod util;
2323
pub mod wf;
2424

2525
use crate::infer::outlives::env::OutlivesEnvironment;
26-
use crate::infer::{InferCtxt, RegionckMode, TyCtxtInferExt};
26+
use crate::infer::{InferCtxt, TyCtxtInferExt};
2727
use crate::traits::error_reporting::InferCtxtExt as _;
2828
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
2929
use rustc_errors::ErrorGuaranteed;
@@ -240,11 +240,7 @@ fn do_normalize_predicates<'tcx>(
240240
// cares about declarations like `'a: 'b`.
241241
let outlives_env = OutlivesEnvironment::new(elaborated_env);
242242

243-
infcx.resolve_regions_and_report_errors(
244-
region_context,
245-
&outlives_env,
246-
RegionckMode::default(),
247-
);
243+
infcx.resolve_regions_and_report_errors(region_context, &outlives_env);
248244

249245
let predicates = match infcx.fully_resolve(predicates) {
250246
Ok(predicates) => predicates,

compiler/rustc_typeck/src/check/regionck.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ use rustc_hir::def_id::LocalDefId;
8282
use rustc_hir::intravisit::{self, Visitor};
8383
use rustc_hir::PatKind;
8484
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
85-
use rustc_infer::infer::{self, InferCtxt, RegionObligation, RegionckMode};
85+
use rustc_infer::infer::{self, InferCtxt, RegionObligation};
8686
use rustc_middle::hir::place::{PlaceBase, PlaceWithHirId};
8787
use rustc_middle::ty::adjustment;
8888
use rustc_middle::ty::{self, Ty};
@@ -163,7 +163,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
163163
rcx.visit_body(body);
164164
rcx.visit_region_obligations(id);
165165
}
166-
rcx.resolve_regions_and_report_errors(RegionckMode::Erase);
166+
// Checked by NLL
167+
rcx.fcx.skip_region_resolution();
167168
}
168169

169170
/// Region checking during the WF phase for items. `wf_tys` are the
@@ -175,7 +176,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
175176
rcx.outlives_environment.add_implied_bounds(self, wf_tys, item_id, span);
176177
rcx.outlives_environment.save_implied_bounds(item_id);
177178
rcx.visit_region_obligations(item_id);
178-
rcx.resolve_regions_and_report_errors(RegionckMode::default());
179+
rcx.resolve_regions_and_report_errors();
179180
}
180181

181182
/// Region check a function body. Not invoked on closures, but
@@ -206,7 +207,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
206207
rcx.visit_fn_body(fn_id, body, self.tcx.hir().span(fn_id));
207208
}
208209

209-
rcx.resolve_regions_and_report_errors(RegionckMode::Erase);
210+
// Checked by NLL
211+
rcx.fcx.skip_region_resolution();
210212
}
211213
}
212214

@@ -361,7 +363,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
361363
self.select_all_obligations_or_error();
362364
}
363365

364-
fn resolve_regions_and_report_errors(&self, mode: RegionckMode) {
366+
fn resolve_regions_and_report_errors(&self) {
365367
self.infcx.process_registered_region_obligations(
366368
self.outlives_environment.region_bound_pairs_map(),
367369
Some(self.tcx.lifetimes.re_root_empty),
@@ -371,7 +373,6 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
371373
self.fcx.resolve_regions_and_report_errors(
372374
self.subject_def_id.to_def_id(),
373375
&self.outlives_environment,
374-
mode,
375376
);
376377
}
377378

compiler/rustc_typeck/src/check/wfcheck.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use rustc_hir::ItemKind;
1515
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1616
use rustc_infer::infer::outlives::obligations::TypeOutlives;
1717
use rustc_infer::infer::region_constraints::GenericKind;
18-
use rustc_infer::infer::{self, RegionckMode};
19-
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
18+
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
2019
use rustc_middle::hir::nested_filter;
2120
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts, Subst};
2221
use rustc_middle::ty::trait_def::TraitSpecializationKind;
@@ -650,11 +649,7 @@ fn resolve_regions_with_wf_tys<'tcx>(
650649

651650
add_constraints(&infcx, region_bound_pairs);
652651

653-
let errors = infcx.resolve_regions(
654-
id.expect_owner().to_def_id(),
655-
&outlives_environment,
656-
RegionckMode::default(),
657-
);
652+
let errors = infcx.resolve_regions(id.expect_owner().to_def_id(), &outlives_environment);
658653

659654
debug!(?errors, "errors");
660655

compiler/rustc_typeck/src/coherence/builtin.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::lang_items::LangItem;
99
use rustc_hir::ItemKind;
1010
use rustc_infer::infer;
1111
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
12-
use rustc_infer::infer::{RegionckMode, TyCtxtInferExt};
12+
use rustc_infer::infer::TyCtxtInferExt;
1313
use rustc_middle::ty::adjustment::CoerceUnsizedInfo;
1414
use rustc_middle::ty::{self, suggest_constraining_type_params, Ty, TyCtxt, TypeFoldable};
1515
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
@@ -349,11 +349,7 @@ fn visit_implementation_of_dispatch_from_dyn<'tcx>(tcx: TyCtxt<'tcx>, impl_did:
349349

350350
// Finally, resolve all regions.
351351
let outlives_env = OutlivesEnvironment::new(param_env);
352-
infcx.resolve_regions_and_report_errors(
353-
impl_did.to_def_id(),
354-
&outlives_env,
355-
RegionckMode::default(),
356-
);
352+
infcx.resolve_regions_and_report_errors(impl_did.to_def_id(), &outlives_env);
357353
}
358354
}
359355
_ => {
@@ -610,11 +606,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
610606

611607
// Finally, resolve all regions.
612608
let outlives_env = OutlivesEnvironment::new(param_env);
613-
infcx.resolve_regions_and_report_errors(
614-
impl_did.to_def_id(),
615-
&outlives_env,
616-
RegionckMode::default(),
617-
);
609+
infcx.resolve_regions_and_report_errors(impl_did.to_def_id(), &outlives_env);
618610

619611
CoerceUnsizedInfo { custom_kind: kind }
620612
})

compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use crate::errors::SubstsOnOverriddenImpl;
7171
use rustc_data_structures::fx::FxHashSet;
7272
use rustc_hir::def_id::{DefId, LocalDefId};
7373
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
74-
use rustc_infer::infer::{InferCtxt, RegionckMode, TyCtxtInferExt};
74+
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
7575
use rustc_infer::traits::specialization_graph::Node;
7676
use rustc_middle::ty::subst::{GenericArg, InternalSubsts, SubstsRef};
7777
use rustc_middle::ty::trait_def::TraitSpecializationKind;
@@ -164,7 +164,7 @@ fn get_impl_substs<'tcx>(
164164

165165
// Conservatively use an empty `ParamEnv`.
166166
let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty());
167-
infcx.resolve_regions_and_report_errors(impl1_def_id, &outlives_env, RegionckMode::default());
167+
infcx.resolve_regions_and_report_errors(impl1_def_id, &outlives_env);
168168
let Ok(impl2_substs) = infcx.fully_resolve(impl2_substs) else {
169169
tcx.sess.emit_err(SubstsOnOverriddenImpl { span });
170170
return None;

0 commit comments

Comments
 (0)