4
4
use std:: collections:: BTreeMap ;
5
5
6
6
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 ;
9
8
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 ;
13
10
14
11
use smallvec:: SmallVec ;
15
12
16
- use crate :: borrow_check:: region_infer :: RegionInferenceContext ;
13
+ use crate :: borrow_check:: MirBorrowckCtxt ;
17
14
18
- use super :: {
19
- ErrorConstraintInfo , ErrorReportingCtx , RegionErrorNamingCtx , RegionName , RegionNameSource ,
20
- } ;
15
+ use super :: { ErrorConstraintInfo , RegionErrorNamingCtx , RegionName , RegionNameSource } ;
21
16
22
17
/// The different things we could suggest.
23
18
enum SuggestedConstraint {
@@ -35,12 +30,8 @@ enum SuggestedConstraint {
35
30
/// corresponding to a function definition.
36
31
///
37
32
/// 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 {
44
35
/// The list of outlives constraints that need to be added. Specifically, we map each free
45
36
/// region to all other regions that it must outlive. I will use the shorthand `fr:
46
37
/// outlived_frs`. Not all of these regions will already have names necessarily. Some could be
@@ -49,16 +40,7 @@ pub struct OutlivesSuggestionBuilder<'a> {
49
40
constraints_to_add : BTreeMap < RegionVid , Vec < RegionVid > > ,
50
41
}
51
42
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 {
62
44
/// Returns `true` iff the `RegionNameSource` is a valid source for an outlives
63
45
/// suggestion.
64
46
//
@@ -94,22 +76,19 @@ impl OutlivesSuggestionBuilder<'a> {
94
76
/// Returns a name for the region if it is suggestable. See `region_name_is_suggestable`.
95
77
fn region_vid_to_name (
96
78
& self ,
97
- errctx : & ErrorReportingCtx < ' _ , ' _ , ' _ > ,
79
+ mbcx : & MirBorrowckCtxt < ' _ , ' _ > ,
98
80
renctx : & mut RegionErrorNamingCtx ,
99
81
region : RegionVid ,
100
82
) -> 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)
104
85
. filter ( Self :: region_name_is_suggestable)
105
86
}
106
87
107
88
/// 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 (
109
90
& self ,
110
- body : & Body < ' tcx > ,
111
- region_infcx : & RegionInferenceContext < ' tcx > ,
112
- infcx : & InferCtxt < ' _ , ' tcx > ,
91
+ mbcx : & MirBorrowckCtxt < ' _ , ' _ > ,
113
92
renctx : & mut RegionErrorNamingCtx ,
114
93
) -> SmallVec < [ SuggestedConstraint ; 2 ] > {
115
94
let mut suggested = SmallVec :: new ( ) ;
@@ -118,20 +97,8 @@ impl OutlivesSuggestionBuilder<'a> {
118
97
// out silly duplicate messages.
119
98
let mut unified_already = FxHashSet :: default ( ) ;
120
99
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
-
133
100
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) {
135
102
fr_name
136
103
} else {
137
104
continue ;
@@ -141,7 +108,7 @@ impl OutlivesSuggestionBuilder<'a> {
141
108
. iter ( )
142
109
// if there is a `None`, we will just omit that constraint
143
110
. 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) )
145
112
} )
146
113
. collect :: < Vec < _ > > ( ) ;
147
114
@@ -204,14 +171,14 @@ impl OutlivesSuggestionBuilder<'a> {
204
171
/// suggestable.
205
172
crate fn intermediate_suggestion (
206
173
& mut self ,
207
- errctx : & ErrorReportingCtx < ' _ , ' _ , ' _ > ,
174
+ mbcx : & MirBorrowckCtxt < ' _ , ' _ > ,
208
175
errci : & ErrorConstraintInfo ,
209
176
renctx : & mut RegionErrorNamingCtx ,
210
177
diag : & mut DiagnosticBuilder < ' _ > ,
211
178
) {
212
179
// 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 ) ;
215
182
216
183
if let ( Some ( fr_name) , Some ( outlived_fr_name) ) = ( fr_name, outlived_fr_name) {
217
184
if let RegionNameSource :: Static = outlived_fr_name. source {
@@ -227,12 +194,9 @@ impl OutlivesSuggestionBuilder<'a> {
227
194
228
195
/// If there is a suggestion to emit, add a diagnostic to the buffer. This is the final
229
196
/// suggestion including all collected constraints.
230
- crate fn add_suggestion < ' tcx > (
197
+ crate fn add_suggestion (
231
198
& self ,
232
- body : & Body < ' tcx > ,
233
- region_infcx : & RegionInferenceContext < ' tcx > ,
234
- infcx : & InferCtxt < ' _ , ' tcx > ,
235
- errors_buffer : & mut Vec < Diagnostic > ,
199
+ mbcx : & mut MirBorrowckCtxt < ' _ , ' _ > ,
236
200
renctx : & mut RegionErrorNamingCtx ,
237
201
) {
238
202
// No constraints to add? Done.
@@ -251,7 +215,7 @@ impl OutlivesSuggestionBuilder<'a> {
251
215
}
252
216
253
217
// 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) ;
255
219
256
220
// If there are no suggestable constraints...
257
221
if suggested. is_empty ( ) {
@@ -262,7 +226,7 @@ impl OutlivesSuggestionBuilder<'a> {
262
226
// If there is exactly one suggestable constraints, then just suggest it. Otherwise, emit a
263
227
// list of diagnostics.
264
228
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 ( ) {
266
230
SuggestedConstraint :: Outlives ( a, bs) => {
267
231
let bs: SmallVec < [ String ; 2 ] > = bs. iter ( ) . map ( |r| format ! ( "{}" , r) ) . collect ( ) ;
268
232
format ! ( "add bound `{}: {}`" , a, bs. join( " + " ) )
@@ -275,7 +239,8 @@ impl OutlivesSuggestionBuilder<'a> {
275
239
} )
276
240
} else {
277
241
// Create a new diagnostic.
278
- let mut diag = infcx
242
+ let mut diag = mbcx
243
+ . infcx
279
244
. tcx
280
245
. sess
281
246
. diagnostic ( )
@@ -305,10 +270,10 @@ impl OutlivesSuggestionBuilder<'a> {
305
270
} ;
306
271
307
272
// 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 ) ;
309
274
diag. sort_span = mir_span. shrink_to_hi ( ) ;
310
275
311
276
// Buffer the diagnostic
312
- diag. buffer ( errors_buffer) ;
277
+ diag. buffer ( & mut mbcx . errors_buffer ) ;
313
278
}
314
279
}
0 commit comments