Skip to content

Commit d8dcb63

Browse files
committed
Auto merge of #67476 - mark-i-m:simplify-borrow_check-5, r=matthewjasper
Region naming refactoring [6/N] Followup to #67474 EDIT: this PR is probably best read commit-by-commit... The major changes in this PR include: - moving many functions around to modules that better suit them. In particular, a lot of methods were moved from `borrow_check::diagnostics::region_errors` to `borrow_check::region_infer`, and `report_region_errors` was moved from `borrow_check` to `borrow_check::diagnostics::region_errors`. - `borrow_check::diagnostics::{region_errors, region_name}` are now most comprised of methods on `MirBorrowckCtxt` instead of `RegionInferenceContext`, allowing us to get rid of the annoying `pub(in crate::borrow_check)` on most of the fields of the latter, along with a number of method arguments on many methods. - I renamed `MirBorrowckCtxt.nonlexical_regioncx` to just `regioncx` because their is no lexical lifetimes any more, and the old name was annoyingly verbose, causing many lines to wrap unnecessarily. - I got rid of `ErrorRegionNamingContext`. Region naming is implemented as inherent methods on `MirBorrowckCtxt`, so we just move the naming stuff into that struct. The PR is rather large, but the commits are fairly self-contained (though they don't all compile). There was one minor output change to one test with `compare-mode=nll`, which I think is acceptable. Between this PR and the last one, a net of 200 lines are removed, most of which was function parameters and context structs :tada: Some samples: ```diff - self.nonlexical_regioncx.free_region_constraint_info( - &self.body, - &self.local_names, - &self.upvars, - self.mir_def_id, - self.infcx, - borrow_region_vid, - region, - ); + self.free_region_constraint_info(borrow_region_vid, region); ``` ```diff - .or_else(|| { - self.give_name_if_anonymous_region_appears_in_yield_ty( - infcx, - body, - *mir_def_id, - fr, - renctx, - ) - }); + .or_else(|| self.give_name_if_anonymous_region_appears_in_arguments(fr)) ``` r? @matthewjasper cc @eddyb
2 parents 689fca0 + f05e40e commit d8dcb63

File tree

10 files changed

+789
-881
lines changed

10 files changed

+789
-881
lines changed

src/librustc_mir/borrow_check/diagnostics/explain_borrow.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
33
use std::collections::VecDeque;
44

5+
use rustc::infer::NLLRegionVariableOrigin;
56
use rustc::mir::{
67
Body, CastKind, ConstraintCategory, FakeReadCause, Local, Location, Operand, Place, Rvalue,
78
Statement, StatementKind, TerminatorKind,
89
};
910
use rustc::ty::adjustment::PointerCast;
10-
use rustc::ty::{self, TyCtxt};
11+
use rustc::ty::{self, RegionVid, TyCtxt};
1112
use rustc_data_structures::fx::FxHashSet;
1213
use rustc_errors::{Applicability, DiagnosticBuilder};
1314
use rustc_index::vec::IndexVec;
@@ -254,6 +255,23 @@ impl BorrowExplanation {
254255
}
255256

256257
impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
258+
fn free_region_constraint_info(
259+
&self,
260+
borrow_region: RegionVid,
261+
outlived_region: RegionVid,
262+
) -> (ConstraintCategory, bool, Span, Option<RegionName>) {
263+
let (category, from_closure, span) = self.regioncx.best_blame_constraint(
264+
&self.body,
265+
borrow_region,
266+
NLLRegionVariableOrigin::FreeRegion,
267+
|r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region),
268+
);
269+
270+
let outlived_fr_name = self.give_region_a_name(outlived_region);
271+
272+
(category, from_closure, span, outlived_fr_name)
273+
}
274+
257275
/// Returns structured explanation for *why* the borrow contains the
258276
/// point from `location`. This is key for the "3-point errors"
259277
/// [described in the NLL RFC][d].
@@ -278,14 +296,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
278296
location, borrow, kind_place
279297
);
280298

281-
let regioncx = &self.nonlexical_regioncx;
299+
let regioncx = &self.regioncx;
282300
let body: &Body<'_> = &self.body;
283301
let tcx = self.infcx.tcx;
284302

285303
let borrow_region_vid = borrow.region;
286304
debug!("explain_why_borrow_contains_point: borrow_region_vid={:?}", borrow_region_vid);
287305

288-
let region_sub = regioncx.find_sub_region_live_at(borrow_region_vid, location);
306+
let region_sub = self.regioncx.find_sub_region_live_at(borrow_region_vid, location);
289307
debug!("explain_why_borrow_contains_point: region_sub={:?}", region_sub);
290308

291309
match find_use::find(body, regioncx, tcx, region_sub, location) {
@@ -329,10 +347,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
329347
}
330348

331349
None => {
332-
if let Some(region) = regioncx.to_error_region_vid(borrow_region_vid) {
333-
let (category, from_closure, span, region_name) = self
334-
.nonlexical_regioncx
335-
.free_region_constraint_info(self, borrow_region_vid, region);
350+
if let Some(region) = self.to_error_region_vid(borrow_region_vid) {
351+
let (category, from_closure, span, region_name) =
352+
self.free_region_constraint_info(borrow_region_vid, region);
336353
if let Some(region_name) = region_name {
337354
let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
338355
BorrowExplanation::MustBeValidFor {
@@ -345,14 +362,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
345362
} else {
346363
debug!(
347364
"explain_why_borrow_contains_point: \
348-
Could not generate a region name"
365+
Could not generate a region name"
349366
);
350367
BorrowExplanation::Unexplained
351368
}
352369
} else {
353370
debug!(
354371
"explain_why_borrow_contains_point: \
355-
Could not generate an error region vid"
372+
Could not generate an error region vid"
356373
);
357374
BorrowExplanation::Unexplained
358375
}

src/librustc_mir/borrow_check/diagnostics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod region_errors;
3232
crate use mutability_errors::AccessKind;
3333
crate use outlives_suggestion::OutlivesSuggestionBuilder;
3434
crate use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
35-
crate use region_name::{RegionErrorNamingCtx, RegionName, RegionNameSource};
35+
crate use region_name::{RegionName, RegionNameSource};
3636

3737
pub(super) struct IncludingDowncast(pub(super) bool);
3838

src/librustc_mir/borrow_check/diagnostics/outlives_suggestion.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use smallvec::SmallVec;
1212

1313
use crate::borrow_check::MirBorrowckCtxt;
1414

15-
use super::{ErrorConstraintInfo, RegionErrorNamingCtx, RegionName, RegionNameSource};
15+
use super::{ErrorConstraintInfo, RegionName, RegionNameSource};
1616

1717
/// The different things we could suggest.
1818
enum SuggestedConstraint {
@@ -77,19 +77,15 @@ impl OutlivesSuggestionBuilder {
7777
fn region_vid_to_name(
7878
&self,
7979
mbcx: &MirBorrowckCtxt<'_, '_>,
80-
renctx: &mut RegionErrorNamingCtx,
8180
region: RegionVid,
8281
) -> Option<RegionName> {
83-
mbcx.nonlexical_regioncx
84-
.give_region_a_name(mbcx, renctx, region)
85-
.filter(Self::region_name_is_suggestable)
82+
mbcx.give_region_a_name(region).filter(Self::region_name_is_suggestable)
8683
}
8784

8885
/// Compiles a list of all suggestions to be printed in the final big suggestion.
8986
fn compile_all_suggestions(
9087
&self,
9188
mbcx: &MirBorrowckCtxt<'_, '_>,
92-
renctx: &mut RegionErrorNamingCtx,
9389
) -> SmallVec<[SuggestedConstraint; 2]> {
9490
let mut suggested = SmallVec::new();
9591

@@ -98,7 +94,7 @@ impl OutlivesSuggestionBuilder {
9894
let mut unified_already = FxHashSet::default();
9995

10096
for (fr, outlived) in &self.constraints_to_add {
101-
let fr_name = if let Some(fr_name) = self.region_vid_to_name(mbcx, renctx, *fr) {
97+
let fr_name = if let Some(fr_name) = self.region_vid_to_name(mbcx, *fr) {
10298
fr_name
10399
} else {
104100
continue;
@@ -107,9 +103,7 @@ impl OutlivesSuggestionBuilder {
107103
let outlived = outlived
108104
.iter()
109105
// if there is a `None`, we will just omit that constraint
110-
.filter_map(|fr| {
111-
self.region_vid_to_name(mbcx, renctx, *fr).map(|rname| (fr, rname))
112-
})
106+
.filter_map(|fr| self.region_vid_to_name(mbcx, *fr).map(|rname| (fr, rname)))
113107
.collect::<Vec<_>>();
114108

115109
// No suggestable outlived lifetimes.
@@ -173,12 +167,11 @@ impl OutlivesSuggestionBuilder {
173167
&mut self,
174168
mbcx: &MirBorrowckCtxt<'_, '_>,
175169
errci: &ErrorConstraintInfo,
176-
renctx: &mut RegionErrorNamingCtx,
177170
diag: &mut DiagnosticBuilder<'_>,
178171
) {
179172
// Emit an intermediate note.
180-
let fr_name = self.region_vid_to_name(mbcx, renctx, errci.fr);
181-
let outlived_fr_name = self.region_vid_to_name(mbcx, renctx, errci.outlived_fr);
173+
let fr_name = self.region_vid_to_name(mbcx, errci.fr);
174+
let outlived_fr_name = self.region_vid_to_name(mbcx, errci.outlived_fr);
182175

183176
if let (Some(fr_name), Some(outlived_fr_name)) = (fr_name, outlived_fr_name) {
184177
if let RegionNameSource::Static = outlived_fr_name.source {
@@ -194,11 +187,7 @@ impl OutlivesSuggestionBuilder {
194187

195188
/// If there is a suggestion to emit, add a diagnostic to the buffer. This is the final
196189
/// suggestion including all collected constraints.
197-
crate fn add_suggestion(
198-
&self,
199-
mbcx: &mut MirBorrowckCtxt<'_, '_>,
200-
renctx: &mut RegionErrorNamingCtx,
201-
) {
190+
crate fn add_suggestion(&self, mbcx: &mut MirBorrowckCtxt<'_, '_>) {
202191
// No constraints to add? Done.
203192
if self.constraints_to_add.is_empty() {
204193
debug!("No constraints to suggest.");
@@ -215,7 +204,7 @@ impl OutlivesSuggestionBuilder {
215204
}
216205

217206
// Get all suggestable constraints.
218-
let suggested = self.compile_all_suggestions(mbcx, renctx);
207+
let suggested = self.compile_all_suggestions(mbcx);
219208

220209
// If there are no suggestable constraints...
221210
if suggested.is_empty() {

0 commit comments

Comments
 (0)