Skip to content

Commit 717a575

Browse files
committed
Auto merge of rust-lang#132623 - nnethercote:rustc_borrowck-cleanups-2, r=<try>
`rustc_borrowck` cleanups, part 2 The code under `do_mir_borrowck` is pretty messy, especially the various types like `MirBorrowckCtxt`, `BorrowckInferCtxt`, `MirTypeckResults`, `MirTypeckRegionConstraints`, `CreateResult`, `TypeChecker`, `TypeVerifier`, `LivenessContext`, `LivenessResults`. This PR does some tidying up, though there's still plenty of mess left afterwards. A sequel to rust-lang#132250. r? `@compiler-errors`
2 parents 76fd471 + 976ba9e commit 717a575

22 files changed

+154
-213
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'tcx> UniverseInfo<'tcx> {
6060
UniverseInfo::RelateTys { expected, found } => {
6161
let err = mbcx.infcx.err_ctxt().report_mismatched_types(
6262
&cause,
63-
mbcx.param_env,
63+
mbcx.infcx.param_env,
6464
expected,
6565
found,
6666
TypeError::RegionsPlaceholderMismatch,

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
904904
let ty = moved_place.ty(self.body, self.infcx.tcx).ty;
905905
debug!("ty: {:?}, kind: {:?}", ty, ty.kind());
906906

907-
let Some(assign_value) = self.infcx.err_ctxt().ty_kind_suggestion(self.param_env, ty)
907+
let Some(assign_value) = self.infcx.err_ctxt().ty_kind_suggestion(self.infcx.param_env, ty)
908908
else {
909909
return;
910910
};
@@ -1304,7 +1304,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
13041304
pub(crate) fn implements_clone(&self, ty: Ty<'tcx>) -> bool {
13051305
let Some(clone_trait_def) = self.infcx.tcx.lang_items().clone_trait() else { return false };
13061306
self.infcx
1307-
.type_implements_trait(clone_trait_def, [ty], self.param_env)
1307+
.type_implements_trait(clone_trait_def, [ty], self.infcx.param_env)
13081308
.must_apply_modulo_regions()
13091309
}
13101310

@@ -1437,7 +1437,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
14371437
let ocx = ObligationCtxt::new_with_diagnostics(self.infcx);
14381438
let cause = ObligationCause::misc(span, self.mir_def_id());
14391439

1440-
ocx.register_bound(cause, self.param_env, ty, def_id);
1440+
ocx.register_bound(cause, self.infcx.param_env, ty, def_id);
14411441
let errors = ocx.select_all_or_error();
14421442

14431443
// Only emit suggestion if all required predicates are on generic
@@ -1957,7 +1957,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
19571957
&& let ty::Ref(_, inner, _) = rcvr_ty.kind()
19581958
&& let inner = inner.peel_refs()
19591959
&& (Holds { ty: inner }).visit_ty(local_ty).is_break()
1960-
&& let None = self.infcx.type_implements_trait_shallow(clone, inner, self.param_env)
1960+
&& let None =
1961+
self.infcx.type_implements_trait_shallow(clone, inner, self.infcx.param_env)
19611962
{
19621963
err.span_label(
19631964
span,
@@ -1989,7 +1990,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
19891990
let obligation = Obligation::new(
19901991
self.infcx.tcx,
19911992
ObligationCause::dummy(),
1992-
self.param_env,
1993+
self.infcx.param_env,
19931994
trait_ref,
19941995
);
19951996
self.infcx.err_ctxt().suggest_derive(
@@ -3398,7 +3399,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
33983399
if let Some(iter_trait) = tcx.get_diagnostic_item(sym::Iterator)
33993400
&& self
34003401
.infcx
3401-
.type_implements_trait(iter_trait, [return_ty], self.param_env)
3402+
.type_implements_trait(iter_trait, [return_ty], self.infcx.param_env)
34023403
.must_apply_modulo_regions()
34033404
{
34043405
err.span_suggestion_hidden(
@@ -3837,11 +3838,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
38373838
if tcx.is_diagnostic_item(sym::deref_method, method_did) {
38383839
let deref_target =
38393840
tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
3840-
Instance::try_resolve(tcx, self.param_env, deref_target, method_args)
3841+
Instance::try_resolve(tcx, self.infcx.param_env, deref_target, method_args)
38413842
.transpose()
38423843
});
38433844
if let Some(Ok(instance)) = deref_target {
3844-
let deref_target_ty = instance.ty(tcx, self.param_env);
3845+
let deref_target_ty = instance.ty(tcx, self.infcx.param_env);
38453846
err.note(format!("borrow occurs due to deref coercion to `{deref_target_ty}`"));
38463847
err.span_note(tcx.def_span(instance.def_id()), "deref defined here");
38473848
}

compiler/rustc_borrowck/src/diagnostics/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
864864

865865
let kind = call_kind(
866866
self.infcx.tcx,
867-
self.param_env,
867+
self.infcx.param_env,
868868
method_did,
869869
method_args,
870870
*fn_span,
@@ -1160,7 +1160,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11601160
let suggest = match tcx.get_diagnostic_item(sym::IntoIterator) {
11611161
Some(def_id) => type_known_to_meet_bound_modulo_regions(
11621162
self.infcx,
1163-
self.param_env,
1163+
self.infcx.param_env,
11641164
Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, ty),
11651165
def_id,
11661166
),
@@ -1224,7 +1224,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12241224
BoundRegionConversionTime::FnCall,
12251225
tcx.fn_sig(method_did).instantiate(tcx, method_args).input(0),
12261226
)
1227-
&& self.infcx.can_eq(self.param_env, ty, self_ty)
1227+
&& self.infcx.can_eq(self.infcx.param_env, ty, self_ty)
12281228
{
12291229
err.subdiagnostic(CaptureReasonSuggest::FreshReborrow {
12301230
span: move_span.shrink_to_hi(),
@@ -1258,7 +1258,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12581258
if let Some(errors) = self.infcx.type_implements_trait_shallow(
12591259
clone_trait,
12601260
ty,
1261-
self.param_env,
1261+
self.infcx.param_env,
12621262
) && !has_sugg
12631263
{
12641264
let msg = match &errors[..] {

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12421242
.type_implements_trait_shallow(
12431243
clone_trait,
12441244
ty.peel_refs(),
1245-
self.param_env,
1245+
self.infcx.param_env,
12461246
)
12471247
.as_deref()
12481248
{
@@ -1279,7 +1279,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12791279
let obligation = traits::Obligation::new(
12801280
self.infcx.tcx,
12811281
traits::ObligationCause::dummy(),
1282-
self.param_env,
1282+
self.infcx.param_env,
12831283
trait_ref,
12841284
);
12851285
self.infcx.err_ctxt().suggest_derive(

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
952952

953953
if let Ok(Some(instance)) = ty::Instance::try_resolve(
954954
tcx,
955-
self.param_env,
955+
self.infcx.param_env,
956956
*fn_did,
957957
self.infcx.resolve_vars_if_possible(args),
958958
) {
@@ -1091,7 +1091,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10911091
peeled_ty = ref_ty;
10921092
count += 1;
10931093
}
1094-
if !self.infcx.type_is_copy_modulo_regions(self.param_env, peeled_ty) {
1094+
if !self.infcx.type_is_copy_modulo_regions(self.infcx.param_env, peeled_ty) {
10951095
return;
10961096
}
10971097

@@ -1160,7 +1160,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11601160
let ocx = ObligationCtxt::new(&self.infcx);
11611161
ocx.register_obligations(preds.iter().map(|(pred, span)| {
11621162
trace!(?pred);
1163-
Obligation::misc(tcx, span, self.mir_def_id(), self.param_env, pred)
1163+
Obligation::misc(tcx, span, self.mir_def_id(), self.infcx.param_env, pred)
11641164
}));
11651165

11661166
if ocx.select_all_or_error().is_empty() && count > 0 {

compiler/rustc_borrowck/src/lib.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ fn do_mir_borrowck<'tcx>(
140140
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
141141
let def = input_body.source.def_id().expect_local();
142142
let infcx = BorrowckInferCtxt::new(tcx, def);
143-
let param_env = tcx.param_env(def);
144143

145144
let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
146145
for var_debug_info in &input_body.var_debug_info {
@@ -175,8 +174,7 @@ fn do_mir_borrowck<'tcx>(
175174
// will have a lifetime tied to the inference context.
176175
let mut body_owned = input_body.clone();
177176
let mut promoted = input_promoted.to_owned();
178-
let free_regions =
179-
nll::replace_regions_in_mir(&infcx, param_env, &mut body_owned, &mut promoted);
177+
let free_regions = nll::replace_regions_in_mir(&infcx, &mut body_owned, &mut promoted);
180178
let body = &body_owned; // no further changes
181179

182180
// FIXME(-Znext-solver): A bit dubious that we're only registering
@@ -192,7 +190,7 @@ fn do_mir_borrowck<'tcx>(
192190
.iter_enumerated()
193191
.map(|(idx, body)| (idx, MoveData::gather_moves(body, tcx, |_| true)));
194192

195-
let mut flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
193+
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
196194
.iterate_to_fixpoint(tcx, body, Some("borrowck"))
197195
.into_results_cursor(body);
198196

@@ -213,11 +211,9 @@ fn do_mir_borrowck<'tcx>(
213211
body,
214212
&promoted,
215213
&location_table,
216-
param_env,
217-
&mut flow_inits,
214+
flow_inits,
218215
&move_data,
219216
&borrow_set,
220-
tcx.closure_captures(def),
221217
consumer_options,
222218
);
223219

@@ -229,11 +225,6 @@ fn do_mir_borrowck<'tcx>(
229225
// information.
230226
nll::dump_annotation(&infcx, body, &regioncx, &opt_closure_req, &opaque_type_values, diags);
231227

232-
// The various `flow_*` structures can be large. We drop `flow_inits` here
233-
// so it doesn't overlap with the others below. This reduces peak memory
234-
// usage significantly on some benchmarks.
235-
drop(flow_inits);
236-
237228
let flow_borrows = Borrows::new(tcx, body, &regioncx, &borrow_set).iterate_to_fixpoint(
238229
tcx,
239230
body,
@@ -268,7 +259,6 @@ fn do_mir_borrowck<'tcx>(
268259
let promoted_body = &promoted[idx];
269260
let mut promoted_mbcx = MirBorrowckCtxt {
270261
infcx: &infcx,
271-
param_env,
272262
body: promoted_body,
273263
move_data: &move_data,
274264
location_table: &location_table, // no need to create a real one for the promoted, it is not used
@@ -308,7 +298,6 @@ fn do_mir_borrowck<'tcx>(
308298

309299
let mut mbcx = MirBorrowckCtxt {
310300
infcx: &infcx,
311-
param_env,
312301
body,
313302
move_data: &move_data,
314303
location_table: &location_table,
@@ -429,12 +418,14 @@ fn do_mir_borrowck<'tcx>(
429418
pub(crate) struct BorrowckInferCtxt<'tcx> {
430419
pub(crate) infcx: InferCtxt<'tcx>,
431420
pub(crate) reg_var_to_origin: RefCell<FxIndexMap<ty::RegionVid, RegionCtxt>>,
421+
pub(crate) param_env: ParamEnv<'tcx>,
432422
}
433423

434424
impl<'tcx> BorrowckInferCtxt<'tcx> {
435425
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
436426
let infcx = tcx.infer_ctxt().build(TypingMode::analysis_in_body(tcx, def_id));
437-
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) }
427+
let param_env = tcx.param_env(def_id);
428+
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()), param_env }
438429
}
439430

440431
pub(crate) fn next_region_var<F>(
@@ -513,7 +504,6 @@ impl<'tcx> Deref for BorrowckInferCtxt<'tcx> {
513504

514505
struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
515506
infcx: &'infcx BorrowckInferCtxt<'tcx>,
516-
param_env: ParamEnv<'tcx>,
517507
body: &'a Body<'tcx>,
518508
move_data: &'a MoveData<'tcx>,
519509

compiler/rustc_borrowck/src/nll.rs

+6-40
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::diagnostics::RegionErrors;
3030
use crate::facts::{AllFacts, AllFactsExt, RustcFacts};
3131
use crate::location::LocationTable;
3232
use crate::region_infer::RegionInferenceContext;
33-
use crate::type_check::{self, MirTypeckRegionConstraints, MirTypeckResults};
33+
use crate::type_check::{self, MirTypeckResults};
3434
use crate::universal_regions::UniversalRegions;
3535
use crate::{BorrowckInferCtxt, polonius, renumber};
3636

@@ -50,10 +50,9 @@ pub(crate) struct NllOutput<'tcx> {
5050
/// Rewrites the regions in the MIR to use NLL variables, also scraping out the set of universal
5151
/// regions (e.g., region parameters) declared on the function. That set will need to be given to
5252
/// `compute_regions`.
53-
#[instrument(skip(infcx, param_env, body, promoted), level = "debug")]
53+
#[instrument(skip(infcx, body, promoted), level = "debug")]
5454
pub(crate) fn replace_regions_in_mir<'tcx>(
5555
infcx: &BorrowckInferCtxt<'tcx>,
56-
param_env: ty::ParamEnv<'tcx>,
5756
body: &mut Body<'tcx>,
5857
promoted: &mut IndexSlice<Promoted, Body<'tcx>>,
5958
) -> UniversalRegions<'tcx> {
@@ -62,7 +61,7 @@ pub(crate) fn replace_regions_in_mir<'tcx>(
6261
debug!(?def);
6362

6463
// Compute named region information. This also renumbers the inputs/outputs.
65-
let universal_regions = UniversalRegions::new(infcx, def, param_env);
64+
let universal_regions = UniversalRegions::new(infcx, def);
6665

6766
// Replace all remaining regions with fresh inference variables.
6867
renumber::renumber_mir(infcx, body, promoted);
@@ -81,11 +80,9 @@ pub(crate) fn compute_regions<'a, 'tcx>(
8180
body: &Body<'tcx>,
8281
promoted: &IndexSlice<Promoted, Body<'tcx>>,
8382
location_table: &LocationTable,
84-
param_env: ty::ParamEnv<'tcx>,
85-
flow_inits: &mut ResultsCursor<'a, 'tcx, MaybeInitializedPlaces<'a, 'tcx>>,
83+
flow_inits: ResultsCursor<'a, 'tcx, MaybeInitializedPlaces<'a, 'tcx>>,
8684
move_data: &MoveData<'tcx>,
8785
borrow_set: &BorrowSet<'tcx>,
88-
upvars: &[&ty::CapturedPlace<'tcx>],
8986
consumer_options: Option<ConsumerOptions>,
9087
) -> NllOutput<'tcx> {
9188
let is_polonius_legacy_enabled = infcx.tcx.sess.opts.unstable_opts.polonius.is_legacy_enabled();
@@ -96,41 +93,27 @@ pub(crate) fn compute_regions<'a, 'tcx>(
9693
let mut all_facts =
9794
(polonius_input || AllFacts::enabled(infcx.tcx)).then_some(AllFacts::default());
9895

99-
let universal_regions = Rc::new(universal_regions);
100-
10196
let elements = Rc::new(DenseLocationMap::new(body));
10297

10398
// Run the MIR type-checker.
10499
let MirTypeckResults { constraints, universal_region_relations, opaque_type_values } =
105100
type_check::type_check(
106101
infcx,
107-
param_env,
108102
body,
109103
promoted,
110-
Rc::clone(&universal_regions),
104+
universal_regions,
111105
location_table,
112106
borrow_set,
113107
&mut all_facts,
114108
flow_inits,
115109
move_data,
116110
Rc::clone(&elements),
117-
upvars,
118111
);
119112

120113
// Create the region inference context, taking ownership of the
121114
// region inference data that was contained in `infcx`, and the
122115
// base constraints generated by the type-check.
123116
let var_origins = infcx.get_region_var_origins();
124-
let MirTypeckRegionConstraints {
125-
placeholder_indices,
126-
placeholder_index_to_region: _,
127-
liveness_constraints,
128-
mut outlives_constraints,
129-
mut member_constraints,
130-
universe_causes,
131-
type_tests,
132-
} = constraints;
133-
let placeholder_indices = Rc::new(placeholder_indices);
134117

135118
// If requested, emit legacy polonius facts.
136119
polonius::emit_facts(
@@ -140,31 +123,14 @@ pub(crate) fn compute_regions<'a, 'tcx>(
140123
body,
141124
borrow_set,
142125
move_data,
143-
&universal_regions,
144126
&universal_region_relations,
145127
);
146128

147-
if let Some(guar) = universal_regions.tainted_by_errors() {
148-
// Suppress unhelpful extra errors in `infer_opaque_types` by clearing out all
149-
// outlives bounds that we may end up checking.
150-
outlives_constraints = Default::default();
151-
member_constraints = Default::default();
152-
153-
// Also taint the entire scope.
154-
infcx.set_tainted_by_errors(guar);
155-
}
156-
157129
let mut regioncx = RegionInferenceContext::new(
158130
infcx,
159131
var_origins,
160-
universal_regions,
161-
placeholder_indices,
132+
constraints,
162133
universal_region_relations,
163-
outlives_constraints,
164-
member_constraints,
165-
universe_causes,
166-
type_tests,
167-
liveness_constraints,
168134
elements,
169135
);
170136

0 commit comments

Comments
 (0)