44use std:: collections:: BTreeMap ;
55
66use log:: debug;
7- use rustc:: mir:: { Body , Local } ;
8- use rustc:: { hir:: def_id:: DefId , infer:: InferCtxt , ty:: RegionVid } ;
7+ use rustc:: ty:: RegionVid ;
98use 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
1411use 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.
2318enum 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}
0 commit comments