Skip to content

Commit 7d40450

Browse files
committed
Auto merge of rust-lang#132623 - nnethercote:rustc_borrowck-cleanups-2, r=Nadrieril
`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 e6c1e14 + 75108b6 commit 7d40450

23 files changed

+171
-226
lines changed

Diff for: 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,

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
266266
}
267267
}
268268

269-
if self.param_env.caller_bounds().iter().any(|c| {
269+
if self.infcx.param_env.caller_bounds().iter().any(|c| {
270270
c.as_trait_clause().is_some_and(|pred| {
271271
pred.skip_binder().self_ty() == ty && self.infcx.tcx.is_fn_trait(pred.def_id())
272272
})
@@ -682,13 +682,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
682682
// Normalize before comparing to see through type aliases and projections.
683683
let old_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, generic_args);
684684
let new_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, new_args);
685-
if let Ok(old_ty) =
686-
tcx.try_normalize_erasing_regions(self.infcx.typing_env(self.param_env), old_ty)
687-
&& let Ok(new_ty) = tcx.try_normalize_erasing_regions(
688-
self.infcx.typing_env(self.param_env),
689-
new_ty,
690-
)
691-
{
685+
if let Ok(old_ty) = tcx.try_normalize_erasing_regions(
686+
self.infcx.typing_env(self.infcx.param_env),
687+
old_ty,
688+
) && let Ok(new_ty) = tcx.try_normalize_erasing_regions(
689+
self.infcx.typing_env(self.infcx.param_env),
690+
new_ty,
691+
) {
692692
old_ty == new_ty
693693
} else {
694694
false
@@ -707,15 +707,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
707707
// Test the callee's predicates, substituting in `ref_ty` for the moved argument type.
708708
clauses.instantiate(tcx, new_args).predicates.iter().all(|&(mut clause)| {
709709
// Normalize before testing to see through type aliases and projections.
710-
if let Ok(normalized) =
711-
tcx.try_normalize_erasing_regions(self.infcx.typing_env(self.param_env), clause)
712-
{
710+
if let Ok(normalized) = tcx.try_normalize_erasing_regions(
711+
self.infcx.typing_env(self.infcx.param_env),
712+
clause,
713+
) {
713714
clause = normalized;
714715
}
715716
self.infcx.predicate_must_hold_modulo_regions(&Obligation::new(
716717
tcx,
717718
ObligationCause::dummy(),
718-
self.param_env,
719+
self.infcx.param_env,
719720
clause,
720721
))
721722
})
@@ -904,7 +905,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
904905
let ty = moved_place.ty(self.body, self.infcx.tcx).ty;
905906
debug!("ty: {:?}, kind: {:?}", ty, ty.kind());
906907

907-
let Some(assign_value) = self.infcx.err_ctxt().ty_kind_suggestion(self.param_env, ty)
908+
let Some(assign_value) = self.infcx.err_ctxt().ty_kind_suggestion(self.infcx.param_env, ty)
908909
else {
909910
return;
910911
};
@@ -1304,7 +1305,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
13041305
pub(crate) fn implements_clone(&self, ty: Ty<'tcx>) -> bool {
13051306
let Some(clone_trait_def) = self.infcx.tcx.lang_items().clone_trait() else { return false };
13061307
self.infcx
1307-
.type_implements_trait(clone_trait_def, [ty], self.param_env)
1308+
.type_implements_trait(clone_trait_def, [ty], self.infcx.param_env)
13081309
.must_apply_modulo_regions()
13091310
}
13101311

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

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

14431444
// Only emit suggestion if all required predicates are on generic
@@ -1957,7 +1958,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
19571958
&& let ty::Ref(_, inner, _) = rcvr_ty.kind()
19581959
&& let inner = inner.peel_refs()
19591960
&& (Holds { ty: inner }).visit_ty(local_ty).is_break()
1960-
&& let None = self.infcx.type_implements_trait_shallow(clone, inner, self.param_env)
1961+
&& let None =
1962+
self.infcx.type_implements_trait_shallow(clone, inner, self.infcx.param_env)
19611963
{
19621964
err.span_label(
19631965
span,
@@ -1989,7 +1991,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
19891991
let obligation = Obligation::new(
19901992
self.infcx.tcx,
19911993
ObligationCause::dummy(),
1992-
self.param_env,
1994+
self.infcx.param_env,
19931995
trait_ref,
19941996
);
19951997
self.infcx.err_ctxt().suggest_derive(
@@ -3398,7 +3400,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
33983400
if let Some(iter_trait) = tcx.get_diagnostic_item(sym::Iterator)
33993401
&& self
34003402
.infcx
3401-
.type_implements_trait(iter_trait, [return_ty], self.param_env)
3403+
.type_implements_trait(iter_trait, [return_ty], self.infcx.param_env)
34023404
.must_apply_modulo_regions()
34033405
{
34043406
err.span_suggestion_hidden(
@@ -3839,14 +3841,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
38393841
tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
38403842
Instance::try_resolve(
38413843
tcx,
3842-
self.infcx.typing_env(self.param_env),
3844+
self.infcx.typing_env(self.infcx.param_env),
38433845
deref_target,
38443846
method_args,
38453847
)
38463848
.transpose()
38473849
});
38483850
if let Some(Ok(instance)) = deref_target {
3849-
let deref_target_ty = instance.ty(tcx, self.infcx.typing_env(self.param_env));
3851+
let deref_target_ty =
3852+
instance.ty(tcx, self.infcx.typing_env(self.infcx.param_env));
38503853
err.note(format!("borrow occurs due to deref coercion to `{deref_target_ty}`"));
38513854
err.span_note(tcx.def_span(instance.def_id()), "deref defined here");
38523855
}

Diff for: 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.infcx.typing_env(self.param_env),
867+
self.infcx.typing_env(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[..] {

Diff for: compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
305305
let Some(copy_trait_def) = self.infcx.tcx.lang_items().copy_trait() else { return false };
306306
// This is only going to be ambiguous if there are incoherent impls, because otherwise
307307
// ambiguity should never happen in MIR.
308-
self.infcx.type_implements_trait(copy_trait_def, [ty], self.param_env).may_apply()
308+
self.infcx.type_implements_trait(copy_trait_def, [ty], self.infcx.param_env).may_apply()
309309
}
310310

311311
fn report_cannot_move_from_static(&mut self, place: Place<'tcx>, span: Span) -> Diag<'infcx> {

Diff for: 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(

Diff for: 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.infcx.typing_env(self.param_env),
955+
self.infcx.typing_env(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 {

Diff for: compiler/rustc_borrowck/src/lib.rs

+6-15
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,18 +211,12 @@ 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

224-
// `flow_inits` is large, so we drop it as soon as possible. This reduces
225-
// peak memory usage significantly on some benchmarks.
226-
drop(flow_inits);
227-
228220
// Dump MIR results into a file, if that is enabled. This let us
229221
// write unit-tests, as well as helping with debugging.
230222
nll::dump_nll_mir(&infcx, body, &regioncx, &opt_closure_req, &borrow_set);
@@ -251,7 +243,6 @@ fn do_mir_borrowck<'tcx>(
251243
let promoted_body = &promoted[idx];
252244
let mut promoted_mbcx = MirBorrowckCtxt {
253245
infcx: &infcx,
254-
param_env,
255246
body: promoted_body,
256247
move_data: &move_data,
257248
location_table: &location_table, // no need to create a real one for the promoted, it is not used
@@ -291,7 +282,6 @@ fn do_mir_borrowck<'tcx>(
291282

292283
let mut mbcx = MirBorrowckCtxt {
293284
infcx: &infcx,
294-
param_env,
295285
body,
296286
move_data: &move_data,
297287
location_table: &location_table,
@@ -448,12 +438,14 @@ fn get_flow_results<'a, 'tcx>(
448438
pub(crate) struct BorrowckInferCtxt<'tcx> {
449439
pub(crate) infcx: InferCtxt<'tcx>,
450440
pub(crate) reg_var_to_origin: RefCell<FxIndexMap<ty::RegionVid, RegionCtxt>>,
441+
pub(crate) param_env: ParamEnv<'tcx>,
451442
}
452443

453444
impl<'tcx> BorrowckInferCtxt<'tcx> {
454445
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
455446
let infcx = tcx.infer_ctxt().build(TypingMode::analysis_in_body(tcx, def_id));
456-
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) }
447+
let param_env = tcx.param_env(def_id);
448+
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()), param_env }
457449
}
458450

459451
pub(crate) fn next_region_var<F>(
@@ -532,7 +524,6 @@ impl<'tcx> Deref for BorrowckInferCtxt<'tcx> {
532524

533525
struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
534526
infcx: &'infcx BorrowckInferCtxt<'tcx>,
535-
param_env: ParamEnv<'tcx>,
536527
body: &'a Body<'tcx>,
537528
move_data: &'a MoveData<'tcx>,
538529

0 commit comments

Comments
 (0)