Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eb218fc

Browse files
committedDec 30, 2019
Get rid of ErrorReportingCtx
1 parent 2ba0d2a commit eb218fc

File tree

6 files changed

+122
-243
lines changed

6 files changed

+122
-243
lines changed
 

‎src/librustc_mir/borrow_check/diagnostics/explain_borrow.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
289289

290290
None => {
291291
if let Some(region) = regioncx.to_error_region_vid(borrow_region_vid) {
292-
let (category, from_closure, span, region_name) =
293-
self.nonlexical_regioncx.free_region_constraint_info(
294-
&self.body,
295-
&self.local_names,
296-
&self.upvars,
297-
self.mir_def_id,
298-
self.infcx,
299-
borrow_region_vid,
300-
region,
301-
);
292+
let (category, from_closure, span, region_name) = self
293+
.nonlexical_regioncx
294+
.free_region_constraint_info(self, borrow_region_vid, region);
302295
if let Some(region_name) = region_name {
303296
let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
304297
BorrowExplanation::MustBeValidFor {

‎src/librustc_mir/borrow_check/diagnostics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod region_errors;
3232

3333
crate use mutability_errors::AccessKind;
3434
crate use outlives_suggestion::OutlivesSuggestionBuilder;
35-
crate use region_errors::{ErrorConstraintInfo, ErrorReportingCtx, RegionErrorKind, RegionErrors};
35+
crate use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
3636
crate use region_name::{RegionErrorNamingCtx, RegionName, RegionNameSource};
3737

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

‎src/librustc_mir/borrow_check/diagnostics/outlives_suggestion.rs

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44
use std::collections::BTreeMap;
55

66
use log::debug;
7-
use rustc::mir::{Body, Local};
8-
use rustc::{hir::def_id::DefId, infer::InferCtxt, ty::RegionVid};
7+
use rustc::ty::RegionVid;
98
use rustc_data_structures::fx::FxHashSet;
10-
use rustc_errors::{Diagnostic, DiagnosticBuilder};
11-
use rustc_index::vec::IndexVec;
12-
use syntax_pos::symbol::Symbol;
9+
use rustc_errors::DiagnosticBuilder;
1310

1411
use smallvec::SmallVec;
1512

16-
use crate::borrow_check::region_infer::RegionInferenceContext;
13+
use crate::borrow_check::MirBorrowckCtxt;
1714

18-
use super::{
19-
ErrorConstraintInfo, ErrorReportingCtx, RegionErrorNamingCtx, RegionName, RegionNameSource,
20-
};
15+
use super::{ErrorConstraintInfo, RegionErrorNamingCtx, RegionName, RegionNameSource};
2116

2217
/// The different things we could suggest.
2318
enum SuggestedConstraint {
@@ -35,12 +30,8 @@ enum SuggestedConstraint {
3530
/// corresponding to a function definition.
3631
///
3732
/// Adds a help note suggesting adding a where clause with the needed constraints.
38-
pub struct OutlivesSuggestionBuilder<'a> {
39-
/// The MIR DefId of the fn with the lifetime error.
40-
mir_def_id: DefId,
41-
42-
local_names: &'a IndexVec<Local, Option<Symbol>>,
43-
33+
#[derive(Default)]
34+
pub struct OutlivesSuggestionBuilder {
4435
/// The list of outlives constraints that need to be added. Specifically, we map each free
4536
/// region to all other regions that it must outlive. I will use the shorthand `fr:
4637
/// outlived_frs`. Not all of these regions will already have names necessarily. Some could be
@@ -49,16 +40,7 @@ pub struct OutlivesSuggestionBuilder<'a> {
4940
constraints_to_add: BTreeMap<RegionVid, Vec<RegionVid>>,
5041
}
5142

52-
impl OutlivesSuggestionBuilder<'a> {
53-
/// Create a new builder for the given MIR node representing a fn definition.
54-
crate fn new(mir_def_id: DefId, local_names: &'a IndexVec<Local, Option<Symbol>>) -> Self {
55-
OutlivesSuggestionBuilder {
56-
mir_def_id,
57-
local_names,
58-
constraints_to_add: BTreeMap::default(),
59-
}
60-
}
61-
43+
impl OutlivesSuggestionBuilder {
6244
/// Returns `true` iff the `RegionNameSource` is a valid source for an outlives
6345
/// suggestion.
6446
//
@@ -94,22 +76,19 @@ impl OutlivesSuggestionBuilder<'a> {
9476
/// Returns a name for the region if it is suggestable. See `region_name_is_suggestable`.
9577
fn region_vid_to_name(
9678
&self,
97-
errctx: &ErrorReportingCtx<'_, '_, '_>,
79+
mbcx: &MirBorrowckCtxt<'_, '_>,
9880
renctx: &mut RegionErrorNamingCtx,
9981
region: RegionVid,
10082
) -> Option<RegionName> {
101-
errctx
102-
.region_infcx
103-
.give_region_a_name(errctx, renctx, region)
83+
mbcx.nonlexical_regioncx
84+
.give_region_a_name(mbcx, renctx, region)
10485
.filter(Self::region_name_is_suggestable)
10586
}
10687

10788
/// Compiles a list of all suggestions to be printed in the final big suggestion.
108-
fn compile_all_suggestions<'tcx>(
89+
fn compile_all_suggestions(
10990
&self,
110-
body: &Body<'tcx>,
111-
region_infcx: &RegionInferenceContext<'tcx>,
112-
infcx: &InferCtxt<'_, 'tcx>,
91+
mbcx: &MirBorrowckCtxt<'_, '_>,
11392
renctx: &mut RegionErrorNamingCtx,
11493
) -> SmallVec<[SuggestedConstraint; 2]> {
11594
let mut suggested = SmallVec::new();
@@ -118,20 +97,8 @@ impl OutlivesSuggestionBuilder<'a> {
11897
// out silly duplicate messages.
11998
let mut unified_already = FxHashSet::default();
12099

121-
let errctx = ErrorReportingCtx {
122-
region_infcx,
123-
infcx,
124-
body,
125-
mir_def_id: self.mir_def_id,
126-
local_names: self.local_names,
127-
128-
// We should not be suggesting naming upvars, so we pass in a dummy set of upvars that
129-
// should never be used.
130-
upvars: &[],
131-
};
132-
133100
for (fr, outlived) in &self.constraints_to_add {
134-
let fr_name = if let Some(fr_name) = self.region_vid_to_name(&errctx, renctx, *fr) {
101+
let fr_name = if let Some(fr_name) = self.region_vid_to_name(mbcx, renctx, *fr) {
135102
fr_name
136103
} else {
137104
continue;
@@ -141,7 +108,7 @@ impl OutlivesSuggestionBuilder<'a> {
141108
.iter()
142109
// if there is a `None`, we will just omit that constraint
143110
.filter_map(|fr| {
144-
self.region_vid_to_name(&errctx, renctx, *fr).map(|rname| (fr, rname))
111+
self.region_vid_to_name(mbcx, renctx, *fr).map(|rname| (fr, rname))
145112
})
146113
.collect::<Vec<_>>();
147114

@@ -204,14 +171,14 @@ impl OutlivesSuggestionBuilder<'a> {
204171
/// suggestable.
205172
crate fn intermediate_suggestion(
206173
&mut self,
207-
errctx: &ErrorReportingCtx<'_, '_, '_>,
174+
mbcx: &MirBorrowckCtxt<'_, '_>,
208175
errci: &ErrorConstraintInfo,
209176
renctx: &mut RegionErrorNamingCtx,
210177
diag: &mut DiagnosticBuilder<'_>,
211178
) {
212179
// Emit an intermediate note.
213-
let fr_name = self.region_vid_to_name(errctx, renctx, errci.fr);
214-
let outlived_fr_name = self.region_vid_to_name(errctx, renctx, errci.outlived_fr);
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);
215182

216183
if let (Some(fr_name), Some(outlived_fr_name)) = (fr_name, outlived_fr_name) {
217184
if let RegionNameSource::Static = outlived_fr_name.source {
@@ -227,12 +194,9 @@ impl OutlivesSuggestionBuilder<'a> {
227194

228195
/// If there is a suggestion to emit, add a diagnostic to the buffer. This is the final
229196
/// suggestion including all collected constraints.
230-
crate fn add_suggestion<'tcx>(
197+
crate fn add_suggestion(
231198
&self,
232-
body: &Body<'tcx>,
233-
region_infcx: &RegionInferenceContext<'tcx>,
234-
infcx: &InferCtxt<'_, 'tcx>,
235-
errors_buffer: &mut Vec<Diagnostic>,
199+
mbcx: &mut MirBorrowckCtxt<'_, '_>,
236200
renctx: &mut RegionErrorNamingCtx,
237201
) {
238202
// No constraints to add? Done.
@@ -251,7 +215,7 @@ impl OutlivesSuggestionBuilder<'a> {
251215
}
252216

253217
// Get all suggestable constraints.
254-
let suggested = self.compile_all_suggestions(body, region_infcx, infcx, renctx);
218+
let suggested = self.compile_all_suggestions(mbcx, renctx);
255219

256220
// If there are no suggestable constraints...
257221
if suggested.is_empty() {
@@ -262,7 +226,7 @@ impl OutlivesSuggestionBuilder<'a> {
262226
// If there is exactly one suggestable constraints, then just suggest it. Otherwise, emit a
263227
// list of diagnostics.
264228
let mut diag = if suggested.len() == 1 {
265-
infcx.tcx.sess.diagnostic().struct_help(&match suggested.last().unwrap() {
229+
mbcx.infcx.tcx.sess.diagnostic().struct_help(&match suggested.last().unwrap() {
266230
SuggestedConstraint::Outlives(a, bs) => {
267231
let bs: SmallVec<[String; 2]> = bs.iter().map(|r| format!("{}", r)).collect();
268232
format!("add bound `{}: {}`", a, bs.join(" + "))
@@ -275,7 +239,8 @@ impl OutlivesSuggestionBuilder<'a> {
275239
})
276240
} else {
277241
// Create a new diagnostic.
278-
let mut diag = infcx
242+
let mut diag = mbcx
243+
.infcx
279244
.tcx
280245
.sess
281246
.diagnostic()
@@ -305,10 +270,10 @@ impl OutlivesSuggestionBuilder<'a> {
305270
};
306271

307272
// We want this message to appear after other messages on the mir def.
308-
let mir_span = infcx.tcx.def_span(self.mir_def_id);
273+
let mir_span = mbcx.infcx.tcx.def_span(mbcx.mir_def_id);
309274
diag.sort_span = mir_span.shrink_to_hi();
310275

311276
// Buffer the diagnostic
312-
diag.buffer(errors_buffer);
277+
diag.buffer(&mut mbcx.errors_buffer);
313278
}
314279
}

‎src/librustc_mir/borrow_check/diagnostics/region_errors.rs

Lines changed: 41 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ use rustc::infer::{
55
error_reporting::nice_region_error::NiceRegionError, region_constraints::GenericKind,
66
InferCtxt, NLLRegionVariableOrigin,
77
};
8-
use rustc::mir::{Body, ConstraintCategory, Local, Location};
8+
use rustc::mir::{Body, ConstraintCategory, Location};
99
use rustc::ty::{self, RegionVid, Ty};
1010
use rustc_errors::DiagnosticBuilder;
1111
use rustc_index::vec::IndexVec;
1212
use std::collections::VecDeque;
1313
use syntax::errors::Applicability;
1414
use syntax::symbol::kw;
15-
use syntax_pos::symbol::Symbol;
1615
use syntax_pos::Span;
1716

1817
use crate::util::borrowck_errors;
1918

2019
use crate::borrow_check::{
2120
constraints::OutlivesConstraint, nll::ConstraintDescription,
2221
region_infer::RegionInferenceContext, type_check::Locations, universal_regions::DefiningTy,
23-
Upvar,
22+
MirBorrowckCtxt,
2423
};
2524

2625
use super::{OutlivesSuggestionBuilder, RegionErrorNamingCtx, RegionName, RegionNameSource};
@@ -116,27 +115,6 @@ crate enum RegionErrorKind<'tcx> {
116115
},
117116
}
118117

119-
/// Various pieces of state used when reporting borrow checker errors.
120-
pub struct ErrorReportingCtx<'a, 'b, 'tcx> {
121-
/// The region inference context used for borrow chekcing this MIR body.
122-
pub(super) region_infcx: &'b RegionInferenceContext<'tcx>,
123-
124-
/// The inference context used for type checking.
125-
pub(super) infcx: &'b InferCtxt<'a, 'tcx>,
126-
127-
/// The MIR def we are reporting errors on.
128-
pub(super) mir_def_id: DefId,
129-
130-
/// The MIR body we are reporting errors on (for convenience).
131-
pub(super) body: &'b Body<'tcx>,
132-
133-
/// User variable names for MIR locals (where applicable).
134-
pub(super) local_names: &'b IndexVec<Local, Option<Symbol>>,
135-
136-
/// Any upvars for the MIR body we have kept track of during borrow checking.
137-
pub(super) upvars: &'b [Upvar],
138-
}
139-
140118
/// Information about the various region constraints involved in a borrow checker error.
141119
#[derive(Clone, Debug)]
142120
pub struct ErrorConstraintInfo {
@@ -454,28 +432,24 @@ impl<'tcx> RegionInferenceContext<'tcx> {
454432
/// Here we would be invoked with `fr = 'a` and `outlived_fr = `'b`.
455433
pub(in crate::borrow_check) fn report_error<'a>(
456434
&'a self,
457-
body: &Body<'tcx>,
458-
local_names: &IndexVec<Local, Option<Symbol>>,
459-
upvars: &[Upvar],
460-
infcx: &'a InferCtxt<'a, 'tcx>,
461-
mir_def_id: DefId,
435+
mbcx: &MirBorrowckCtxt<'a, 'tcx>,
462436
fr: RegionVid,
463437
fr_origin: NLLRegionVariableOrigin,
464438
outlived_fr: RegionVid,
465-
outlives_suggestion: &mut OutlivesSuggestionBuilder<'_>,
439+
outlives_suggestion: &mut OutlivesSuggestionBuilder,
466440
renctx: &mut RegionErrorNamingCtx,
467441
) -> DiagnosticBuilder<'a> {
468442
debug!("report_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
469443

470-
let (category, _, span) = self.best_blame_constraint(body, fr, fr_origin, |r| {
444+
let (category, _, span) = self.best_blame_constraint(&mbcx.body, fr, fr_origin, |r| {
471445
self.provides_universal_region(r, fr, outlived_fr)
472446
});
473447

474448
debug!("report_error: category={:?} {:?}", category, span);
475449
// Check if we can use one of the "nice region errors".
476450
if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
477-
let tables = infcx.tcx.typeck_tables_of(mir_def_id);
478-
let nice = NiceRegionError::new_from_span(infcx, span, o, f, Some(tables));
451+
let tables = mbcx.infcx.tcx.typeck_tables_of(mbcx.mir_def_id);
452+
let nice = NiceRegionError::new_from_span(mbcx.infcx, span, o, f, Some(tables));
479453
if let Some(diag) = nice.try_report_from_nll() {
480454
return diag;
481455
}
@@ -491,9 +465,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
491465
fr_is_local, outlived_fr_is_local, category
492466
);
493467

494-
let errctx =
495-
ErrorReportingCtx { region_infcx: self, infcx, mir_def_id, body, local_names, upvars };
496-
497468
let errci = ErrorConstraintInfo {
498469
fr,
499470
outlived_fr,
@@ -504,22 +475,22 @@ impl<'tcx> RegionInferenceContext<'tcx> {
504475
};
505476

506477
match (category, fr_is_local, outlived_fr_is_local) {
507-
(ConstraintCategory::Return, true, false) if self.is_closure_fn_mut(infcx, fr) => {
508-
self.report_fnmut_error(&errctx, &errci, renctx)
478+
(ConstraintCategory::Return, true, false) if self.is_closure_fn_mut(mbcx.infcx, fr) => {
479+
self.report_fnmut_error(mbcx, &errci, renctx)
509480
}
510481
(ConstraintCategory::Assignment, true, false)
511482
| (ConstraintCategory::CallArgument, true, false) => {
512-
let mut db = self.report_escaping_data_error(&errctx, &errci, renctx);
483+
let mut db = self.report_escaping_data_error(mbcx, &errci, renctx);
513484

514-
outlives_suggestion.intermediate_suggestion(&errctx, &errci, renctx, &mut db);
485+
outlives_suggestion.intermediate_suggestion(mbcx, &errci, renctx, &mut db);
515486
outlives_suggestion.collect_constraint(fr, outlived_fr);
516487

517488
db
518489
}
519490
_ => {
520-
let mut db = self.report_general_error(&errctx, &errci, renctx);
491+
let mut db = self.report_general_error(mbcx, &errci, renctx);
521492

522-
outlives_suggestion.intermediate_suggestion(&errctx, &errci, renctx, &mut db);
493+
outlives_suggestion.intermediate_suggestion(mbcx, &errci, renctx, &mut db);
523494
outlives_suggestion.collect_constraint(fr, outlived_fr);
524495

525496
db
@@ -569,13 +540,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
569540
/// ```
570541
fn report_fnmut_error(
571542
&self,
572-
errctx: &ErrorReportingCtx<'_, '_, 'tcx>,
543+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
573544
errci: &ErrorConstraintInfo,
574545
renctx: &mut RegionErrorNamingCtx,
575546
) -> DiagnosticBuilder<'_> {
576547
let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
577548

578-
let mut diag = errctx
549+
let mut diag = mbcx
579550
.infcx
580551
.tcx
581552
.sess
@@ -593,7 +564,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
593564

594565
diag.span_label(*span, message);
595566

596-
match self.give_region_a_name(errctx, renctx, *outlived_fr).unwrap().source {
567+
match self.give_region_a_name(mbcx, renctx, *outlived_fr).unwrap().source {
597568
RegionNameSource::NamedEarlyBoundRegion(fr_span)
598569
| RegionNameSource::NamedFreeRegion(fr_span)
599570
| RegionNameSource::SynthesizedFreeEnvRegion(fr_span, _)
@@ -630,21 +601,24 @@ impl<'tcx> RegionInferenceContext<'tcx> {
630601
/// ```
631602
fn report_escaping_data_error(
632603
&self,
633-
errctx: &ErrorReportingCtx<'_, '_, 'tcx>,
604+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
634605
errci: &ErrorConstraintInfo,
635606
renctx: &mut RegionErrorNamingCtx,
636607
) -> DiagnosticBuilder<'_> {
637-
let ErrorReportingCtx { infcx, body, upvars, local_names, .. } = errctx;
638-
639608
let ErrorConstraintInfo { span, category, .. } = errci;
640609

641-
let fr_name_and_span =
642-
self.get_var_name_and_span_for_region(infcx.tcx, body, local_names, upvars, errci.fr);
610+
let fr_name_and_span = self.get_var_name_and_span_for_region(
611+
mbcx.infcx.tcx,
612+
&mbcx.body,
613+
&mbcx.local_names,
614+
&mbcx.upvars,
615+
errci.fr,
616+
);
643617
let outlived_fr_name_and_span = self.get_var_name_and_span_for_region(
644-
infcx.tcx,
645-
body,
646-
local_names,
647-
upvars,
618+
mbcx.infcx.tcx,
619+
&mbcx.body,
620+
&mbcx.local_names,
621+
&mbcx.upvars,
648622
errci.outlived_fr,
649623
);
650624

@@ -662,14 +636,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
662636
|| escapes_from == "const"
663637
{
664638
return self.report_general_error(
665-
errctx,
639+
mbcx,
666640
&ErrorConstraintInfo { fr_is_local: true, outlived_fr_is_local: false, ..*errci },
667641
renctx,
668642
);
669643
}
670644

671645
let mut diag =
672-
borrowck_errors::borrowed_data_escapes_closure(infcx.tcx, *span, escapes_from);
646+
borrowck_errors::borrowed_data_escapes_closure(mbcx.infcx.tcx, *span, escapes_from);
673647

674648
if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
675649
diag.span_label(
@@ -713,11 +687,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
713687
/// ```
714688
fn report_general_error(
715689
&self,
716-
errctx: &ErrorReportingCtx<'_, '_, 'tcx>,
690+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
717691
errci: &ErrorConstraintInfo,
718692
renctx: &mut RegionErrorNamingCtx,
719693
) -> DiagnosticBuilder<'_> {
720-
let ErrorReportingCtx { infcx, mir_def_id, .. } = errctx;
721694
let ErrorConstraintInfo {
722695
fr,
723696
fr_is_local,
@@ -728,13 +701,15 @@ impl<'tcx> RegionInferenceContext<'tcx> {
728701
..
729702
} = errci;
730703

731-
let mut diag = infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
704+
let mut diag =
705+
mbcx.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
732706

733-
let mir_def_name = if infcx.tcx.is_closure(*mir_def_id) { "closure" } else { "function" };
707+
let mir_def_name =
708+
if mbcx.infcx.tcx.is_closure(mbcx.mir_def_id) { "closure" } else { "function" };
734709

735-
let fr_name = self.give_region_a_name(errctx, renctx, *fr).unwrap();
710+
let fr_name = self.give_region_a_name(mbcx, renctx, *fr).unwrap();
736711
fr_name.highlight_region_name(&mut diag);
737-
let outlived_fr_name = self.give_region_a_name(errctx, renctx, *outlived_fr).unwrap();
712+
let outlived_fr_name = self.give_region_a_name(mbcx, renctx, *outlived_fr).unwrap();
738713
outlived_fr_name.highlight_region_name(&mut diag);
739714

740715
match (category, outlived_fr_is_local, fr_is_local) {
@@ -761,7 +736,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
761736
}
762737
}
763738

764-
self.add_static_impl_trait_suggestion(infcx, &mut diag, *fr, fr_name, *outlived_fr);
739+
self.add_static_impl_trait_suggestion(mbcx.infcx, &mut diag, *fr, fr_name, *outlived_fr);
765740

766741
diag
767742
}
@@ -854,25 +829,19 @@ impl<'tcx> RegionInferenceContext<'tcx> {
854829

855830
crate fn free_region_constraint_info(
856831
&self,
857-
body: &Body<'tcx>,
858-
local_names: &IndexVec<Local, Option<Symbol>>,
859-
upvars: &[Upvar],
860-
mir_def_id: DefId,
861-
infcx: &InferCtxt<'_, 'tcx>,
832+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
862833
borrow_region: RegionVid,
863834
outlived_region: RegionVid,
864835
) -> (ConstraintCategory, bool, Span, Option<RegionName>) {
865836
let (category, from_closure, span) = self.best_blame_constraint(
866-
body,
837+
&mbcx.body,
867838
borrow_region,
868839
NLLRegionVariableOrigin::FreeRegion,
869840
|r| self.provides_universal_region(r, borrow_region, outlived_region),
870841
);
871842

872843
let mut renctx = RegionErrorNamingCtx::new();
873-
let errctx =
874-
ErrorReportingCtx { infcx, body, local_names, upvars, mir_def_id, region_infcx: self };
875-
let outlived_fr_name = self.give_region_a_name(&errctx, &mut renctx, outlived_region);
844+
let outlived_fr_name = self.give_region_a_name(mbcx, &mut renctx, outlived_region);
876845

877846
(category, from_closure, span, outlived_fr_name)
878847
}

‎src/librustc_mir/borrow_check/diagnostics/region_name.rs

Lines changed: 49 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ use std::fmt::{self, Display};
22

33
use rustc::hir;
44
use rustc::hir::def::{DefKind, Res};
5-
use rustc::hir::def_id::DefId;
6-
use rustc::infer::InferCtxt;
7-
use rustc::mir::{Body, Local};
85
use rustc::ty::print::RegionHighlightMode;
96
use rustc::ty::subst::{GenericArgKind, SubstsRef};
107
use rustc::ty::{self, RegionVid, Ty, TyCtxt};
118
use rustc_data_structures::fx::FxHashMap;
129
use rustc_errors::DiagnosticBuilder;
13-
use rustc_index::vec::IndexVec;
1410
use syntax::symbol::kw;
1511
use syntax_pos::{symbol::Symbol, Span, DUMMY_SP};
1612

1713
use crate::borrow_check::{
18-
diagnostics::region_errors::ErrorReportingCtx, nll::ToRegionVid,
19-
region_infer::RegionInferenceContext, universal_regions::DefiningTy, Upvar,
14+
nll::ToRegionVid, region_infer::RegionInferenceContext, universal_regions::DefiningTy,
15+
MirBorrowckCtxt,
2016
};
2117

2218
/// A name for a particular region used in emitting diagnostics. This name could be a generated
@@ -193,12 +189,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
193189
/// and then return the name `'1` for us to use.
194190
crate fn give_region_a_name(
195191
&self,
196-
errctx: &ErrorReportingCtx<'_, '_, 'tcx>,
192+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
197193
renctx: &mut RegionErrorNamingCtx,
198194
fr: RegionVid,
199195
) -> Option<RegionName> {
200-
let ErrorReportingCtx { infcx, body, mir_def_id, local_names, upvars, .. } = errctx;
201-
202196
debug!("give_region_a_name(fr={:?}, counter={:?})", fr, renctx.counter);
203197

204198
assert!(self.universal_regions.is_universal_region(fr));
@@ -208,38 +202,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
208202
}
209203

210204
let value = self
211-
.give_name_from_error_region(infcx.tcx, *mir_def_id, fr, renctx)
212-
.or_else(|| {
213-
self.give_name_if_anonymous_region_appears_in_arguments(
214-
infcx,
215-
body,
216-
local_names,
217-
*mir_def_id,
218-
fr,
219-
renctx,
220-
)
221-
})
222-
.or_else(|| {
223-
self.give_name_if_anonymous_region_appears_in_upvars(infcx.tcx, upvars, fr, renctx)
224-
})
225-
.or_else(|| {
226-
self.give_name_if_anonymous_region_appears_in_output(
227-
infcx,
228-
body,
229-
*mir_def_id,
230-
fr,
231-
renctx,
232-
)
233-
})
234-
.or_else(|| {
235-
self.give_name_if_anonymous_region_appears_in_yield_ty(
236-
infcx,
237-
body,
238-
*mir_def_id,
239-
fr,
240-
renctx,
241-
)
242-
});
205+
.give_name_from_error_region(mbcx, fr, renctx)
206+
.or_else(|| self.give_name_if_anonymous_region_appears_in_arguments(mbcx, fr, renctx))
207+
.or_else(|| self.give_name_if_anonymous_region_appears_in_upvars(mbcx, fr, renctx))
208+
.or_else(|| self.give_name_if_anonymous_region_appears_in_output(mbcx, fr, renctx))
209+
.or_else(|| self.give_name_if_anonymous_region_appears_in_yield_ty(mbcx, fr, renctx));
243210

244211
if let Some(ref value) = value {
245212
renctx.insert(fr, value.clone());
@@ -255,8 +222,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
255222
/// named variants.
256223
fn give_name_from_error_region(
257224
&self,
258-
tcx: TyCtxt<'tcx>,
259-
mir_def_id: DefId,
225+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
260226
fr: RegionVid,
261227
renctx: &mut RegionErrorNamingCtx,
262228
) -> Option<RegionName> {
@@ -266,7 +232,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
266232
match error_region {
267233
ty::ReEarlyBound(ebr) => {
268234
if ebr.has_name() {
269-
let span = tcx.hir().span_if_local(ebr.def_id).unwrap_or(DUMMY_SP);
235+
let span = mbcx.infcx.tcx.hir().span_if_local(ebr.def_id).unwrap_or(DUMMY_SP);
270236
Some(RegionName {
271237
name: ebr.name,
272238
source: RegionNameSource::NamedEarlyBoundRegion(span),
@@ -283,7 +249,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
283249
ty::ReFree(free_region) => match free_region.bound_region {
284250
ty::BoundRegion::BrNamed(region_def_id, name) => {
285251
// Get the span to point to, even if we don't use the name.
286-
let span = tcx.hir().span_if_local(region_def_id).unwrap_or(DUMMY_SP);
252+
let span =
253+
mbcx.infcx.tcx.hir().span_if_local(region_def_id).unwrap_or(DUMMY_SP);
287254
debug!(
288255
"bound region named: {:?}, is_named: {:?}",
289256
name,
@@ -308,20 +275,25 @@ impl<'tcx> RegionInferenceContext<'tcx> {
308275
}
309276

310277
ty::BoundRegion::BrEnv => {
311-
let mir_hir_id = tcx.hir().as_local_hir_id(mir_def_id).expect("non-local mir");
278+
let mir_hir_id = mbcx
279+
.infcx
280+
.tcx
281+
.hir()
282+
.as_local_hir_id(mbcx.mir_def_id)
283+
.expect("non-local mir");
312284
let def_ty = self.universal_regions.defining_ty;
313285

314286
if let DefiningTy::Closure(def_id, substs) = def_ty {
315287
let args_span = if let hir::ExprKind::Closure(_, _, _, span, _) =
316-
tcx.hir().expect_expr(mir_hir_id).kind
288+
mbcx.infcx.tcx.hir().expect_expr(mir_hir_id).kind
317289
{
318290
span
319291
} else {
320292
bug!("Closure is not defined by a closure expr");
321293
};
322294
let region_name = renctx.synthesize_region_name();
323295

324-
let closure_kind_ty = substs.as_closure().kind_ty(def_id, tcx);
296+
let closure_kind_ty = substs.as_closure().kind_ty(def_id, mbcx.infcx.tcx);
325297
let note = match closure_kind_ty.to_opt_closure_kind() {
326298
Some(ty::ClosureKind::Fn) => {
327299
"closure implements `Fn`, so references to captured variables \
@@ -373,21 +345,17 @@ impl<'tcx> RegionInferenceContext<'tcx> {
373345
/// ```
374346
fn give_name_if_anonymous_region_appears_in_arguments(
375347
&self,
376-
infcx: &InferCtxt<'_, 'tcx>,
377-
body: &Body<'tcx>,
378-
local_names: &IndexVec<Local, Option<Symbol>>,
379-
mir_def_id: DefId,
348+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
380349
fr: RegionVid,
381350
renctx: &mut RegionErrorNamingCtx,
382351
) -> Option<RegionName> {
383352
let implicit_inputs = self.universal_regions.defining_ty.implicit_inputs();
384-
let argument_index = self.get_argument_index_for_region(infcx.tcx, fr)?;
353+
let argument_index = self.get_argument_index_for_region(mbcx.infcx.tcx, fr)?;
385354

386355
let arg_ty =
387356
self.universal_regions.unnormalized_input_tys[implicit_inputs + argument_index];
388357
if let Some(region_name) = self.give_name_if_we_can_match_hir_ty_from_argument(
389-
infcx,
390-
mir_def_id,
358+
mbcx,
391359
fr,
392360
arg_ty,
393361
argument_index,
@@ -396,20 +364,19 @@ impl<'tcx> RegionInferenceContext<'tcx> {
396364
return Some(region_name);
397365
}
398366

399-
self.give_name_if_we_cannot_match_hir_ty(infcx, body, local_names, fr, arg_ty, renctx)
367+
self.give_name_if_we_cannot_match_hir_ty(mbcx, fr, arg_ty, renctx)
400368
}
401369

402370
fn give_name_if_we_can_match_hir_ty_from_argument(
403371
&self,
404-
infcx: &InferCtxt<'_, 'tcx>,
405-
mir_def_id: DefId,
372+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
406373
needle_fr: RegionVid,
407374
argument_ty: Ty<'tcx>,
408375
argument_index: usize,
409376
renctx: &mut RegionErrorNamingCtx,
410377
) -> Option<RegionName> {
411-
let mir_hir_id = infcx.tcx.hir().as_local_hir_id(mir_def_id)?;
412-
let fn_decl = infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?;
378+
let mir_hir_id = mbcx.infcx.tcx.hir().as_local_hir_id(mbcx.mir_def_id)?;
379+
let fn_decl = mbcx.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?;
413380
let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?;
414381
match argument_hir_ty.kind {
415382
// This indicates a variable with no type annotation, like
@@ -420,7 +387,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
420387
hir::TyKind::Infer => None,
421388

422389
_ => self.give_name_if_we_can_match_hir_ty(
423-
infcx.tcx,
390+
mbcx.infcx.tcx,
424391
needle_fr,
425392
argument_ty,
426393
argument_hir_ty,
@@ -442,27 +409,28 @@ impl<'tcx> RegionInferenceContext<'tcx> {
442409
/// ```
443410
fn give_name_if_we_cannot_match_hir_ty(
444411
&self,
445-
infcx: &InferCtxt<'_, 'tcx>,
446-
body: &Body<'tcx>,
447-
local_names: &IndexVec<Local, Option<Symbol>>,
412+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
448413
needle_fr: RegionVid,
449414
argument_ty: Ty<'tcx>,
450415
renctx: &mut RegionErrorNamingCtx,
451416
) -> Option<RegionName> {
452417
let counter = renctx.counter;
453418
let mut highlight = RegionHighlightMode::default();
454419
highlight.highlighting_region_vid(needle_fr, counter);
455-
let type_name = infcx.extract_type_name(&argument_ty, Some(highlight)).0;
420+
let type_name = mbcx.infcx.extract_type_name(&argument_ty, Some(highlight)).0;
456421

457422
debug!(
458423
"give_name_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
459424
type_name, needle_fr
460425
);
461426
let assigned_region_name = if type_name.find(&format!("'{}", counter)).is_some() {
462427
// Only add a label if we can confirm that a region was labelled.
463-
let argument_index = self.get_argument_index_for_region(infcx.tcx, needle_fr)?;
464-
let (_, span) =
465-
self.get_argument_name_and_span_for_region(body, local_names, argument_index);
428+
let argument_index = self.get_argument_index_for_region(mbcx.infcx.tcx, needle_fr)?;
429+
let (_, span) = self.get_argument_name_and_span_for_region(
430+
&mbcx.body,
431+
&mbcx.local_names,
432+
argument_index,
433+
);
466434

467435
Some(RegionName {
468436
// This counter value will already have been used, so this function will increment
@@ -696,14 +664,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
696664
/// ```
697665
fn give_name_if_anonymous_region_appears_in_upvars(
698666
&self,
699-
tcx: TyCtxt<'tcx>,
700-
upvars: &[Upvar],
667+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
701668
fr: RegionVid,
702669
renctx: &mut RegionErrorNamingCtx,
703670
) -> Option<RegionName> {
704-
let upvar_index = self.get_upvar_index_for_region(tcx, fr)?;
671+
let upvar_index = self.get_upvar_index_for_region(mbcx.infcx.tcx, fr)?;
705672
let (upvar_name, upvar_span) =
706-
self.get_upvar_name_and_span_for_region(tcx, upvars, upvar_index);
673+
self.get_upvar_name_and_span_for_region(mbcx.infcx.tcx, &mbcx.upvars, upvar_index);
707674
let region_name = renctx.synthesize_region_name();
708675

709676
Some(RegionName {
@@ -718,13 +685,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
718685
/// or be early bound (named, not in argument).
719686
fn give_name_if_anonymous_region_appears_in_output(
720687
&self,
721-
infcx: &InferCtxt<'_, 'tcx>,
722-
body: &Body<'tcx>,
723-
mir_def_id: DefId,
688+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
724689
fr: RegionVid,
725690
renctx: &mut RegionErrorNamingCtx,
726691
) -> Option<RegionName> {
727-
let tcx = infcx.tcx;
692+
let tcx = mbcx.infcx.tcx;
728693

729694
let return_ty = self.universal_regions.unnormalized_output_ty;
730695
debug!("give_name_if_anonymous_region_appears_in_output: return_ty = {:?}", return_ty);
@@ -734,9 +699,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
734699

735700
let mut highlight = RegionHighlightMode::default();
736701
highlight.highlighting_region_vid(fr, renctx.counter);
737-
let type_name = infcx.extract_type_name(&return_ty, Some(highlight)).0;
702+
let type_name = mbcx.infcx.extract_type_name(&return_ty, Some(highlight)).0;
738703

739-
let mir_hir_id = tcx.hir().as_local_hir_id(mir_def_id).expect("non-local mir");
704+
let mir_hir_id = tcx.hir().as_local_hir_id(mbcx.mir_def_id).expect("non-local mir");
740705

741706
let (return_span, mir_description) = match tcx.hir().get(mir_hir_id) {
742707
hir::Node::Expr(hir::Expr {
@@ -753,7 +718,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
753718
kind: hir::ImplItemKind::Method(method_sig, _),
754719
..
755720
}) => (method_sig.decl.output.span(), ""),
756-
_ => (body.span, ""),
721+
_ => (mbcx.body.span, ""),
757722
};
758723

759724
Some(RegionName {
@@ -771,9 +736,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
771736

772737
fn give_name_if_anonymous_region_appears_in_yield_ty(
773738
&self,
774-
infcx: &InferCtxt<'_, 'tcx>,
775-
body: &Body<'tcx>,
776-
mir_def_id: DefId,
739+
mbcx: &MirBorrowckCtxt<'_, 'tcx>,
777740
fr: RegionVid,
778741
renctx: &mut RegionErrorNamingCtx,
779742
) -> Option<RegionName> {
@@ -782,23 +745,23 @@ impl<'tcx> RegionInferenceContext<'tcx> {
782745
let yield_ty = self.universal_regions.yield_ty?;
783746
debug!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}", yield_ty,);
784747

785-
let tcx = infcx.tcx;
748+
let tcx = mbcx.infcx.tcx;
786749

787750
if !tcx.any_free_region_meets(&yield_ty, |r| r.to_region_vid() == fr) {
788751
return None;
789752
}
790753

791754
let mut highlight = RegionHighlightMode::default();
792755
highlight.highlighting_region_vid(fr, renctx.counter);
793-
let type_name = infcx.extract_type_name(&yield_ty, Some(highlight)).0;
756+
let type_name = mbcx.infcx.extract_type_name(&yield_ty, Some(highlight)).0;
794757

795-
let mir_hir_id = tcx.hir().as_local_hir_id(mir_def_id).expect("non-local mir");
758+
let mir_hir_id = tcx.hir().as_local_hir_id(mbcx.mir_def_id).expect("non-local mir");
796759

797760
let yield_span = match tcx.hir().get(mir_hir_id) {
798761
hir::Node::Expr(hir::Expr {
799762
kind: hir::ExprKind::Closure(_, _, _, span, _), ..
800763
}) => (tcx.sess.source_map().end_point(*span)),
801-
_ => body.span,
764+
_ => mbcx.body.span,
802765
};
803766

804767
debug!(

‎src/librustc_mir/borrow_check/mod.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,8 +1482,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14821482

14831483
// FIXME(mark-i-m): Would be great to get rid of the naming context.
14841484
let mut region_naming = RegionErrorNamingCtx::new();
1485-
let mut outlives_suggestion =
1486-
OutlivesSuggestionBuilder::new(self.mir_def_id, &self.local_names);
1485+
let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
14871486

14881487
for nll_error in nll_errors.into_iter() {
14891488
match nll_error {
@@ -1561,11 +1560,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15611560
RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
15621561
if is_reported {
15631562
let db = self.nonlexical_regioncx.report_error(
1564-
&self.body,
1565-
&self.local_names,
1566-
&self.upvars,
1567-
self.infcx,
1568-
self.mir_def_id,
1563+
self,
15691564
longer_fr,
15701565
fr_origin,
15711566
shorter_fr,
@@ -1591,13 +1586,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15911586
}
15921587

15931588
// Emit one outlives suggestions for each MIR def we borrowck
1594-
outlives_suggestion.add_suggestion(
1595-
&self.body,
1596-
&self.nonlexical_regioncx,
1597-
self.infcx,
1598-
&mut self.errors_buffer,
1599-
&mut region_naming,
1600-
);
1589+
outlives_suggestion.add_suggestion(self, &mut region_naming);
16011590
}
16021591
}
16031592

0 commit comments

Comments
 (0)
Please sign in to comment.