8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use std:: fmt;
12
- use borrow_check:: nll:: region_infer:: { ConstraintIndex , RegionInferenceContext } ;
13
11
use borrow_check:: nll:: region_infer:: values:: ToElementIndex ;
12
+ use borrow_check:: nll:: region_infer:: { Cause , ConstraintIndex , RegionInferenceContext } ;
13
+ use borrow_check:: nll:: region_infer:: { ConstraintIndex , RegionInferenceContext } ;
14
14
use borrow_check:: nll:: type_check:: Locations ;
15
15
use rustc:: hir:: def_id:: DefId ;
16
- use rustc:: infer:: InferCtxt ;
17
16
use rustc:: infer:: error_reporting:: nice_region_error:: NiceRegionError ;
18
- use rustc:: mir:: { self , Location , Mir , Place , StatementKind , TerminatorKind , Rvalue } ;
17
+ use rustc:: infer:: InferCtxt ;
18
+ use rustc:: mir:: { self , Location , Mir , Place , Rvalue , StatementKind , TerminatorKind } ;
19
19
use rustc:: ty:: RegionVid ;
20
20
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
21
21
use rustc_data_structures:: indexed_vec:: IndexVec ;
22
+ use std:: fmt;
22
23
use syntax_pos:: Span ;
23
24
24
25
/// Constraints that are considered interesting can be categorized to
@@ -50,16 +51,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
50
51
/// When reporting an error, it is useful to be able to determine which constraints influenced
51
52
/// the region being reported as an error. This function finds all of the paths from the
52
53
/// constraint.
53
- fn find_constraint_paths_from_region (
54
- & self ,
55
- r0 : RegionVid
56
- ) -> Vec < Vec < ConstraintIndex > > {
54
+ fn find_constraint_paths_from_region ( & self , r0 : RegionVid ) -> Vec < Vec < ConstraintIndex > > {
57
55
let constraints = self . constraints . clone ( ) ;
58
56
59
57
// Mapping of regions to the previous region and constraint index that led to it.
60
58
let mut previous = FxHashMap ( ) ;
61
59
// Regions yet to be visited.
62
- let mut next = vec ! [ r0 ] ;
60
+ let mut next = vec ! [ r0 ] ;
63
61
// Regions that have been visited.
64
62
let mut visited = FxHashSet ( ) ;
65
63
// Ends of paths.
@@ -68,8 +66,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
68
66
// When we've still got points to visit...
69
67
while let Some ( current) = next. pop ( ) {
70
68
// ...take the next point...
71
- debug ! ( "find_constraint_paths_from_region: current={:?} visited={:?} next={:?}" ,
72
- current, visited, next) ;
69
+ debug ! (
70
+ "find_constraint_paths_from_region: current={:?} visited={:?} next={:?}" ,
71
+ current, visited, next
72
+ ) ;
73
73
// ...but make sure not to visit this point again...
74
74
visited. insert ( current) ;
75
75
@@ -78,8 +78,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
78
78
for ( index, constraint) in constraints. iter_enumerated ( ) {
79
79
if constraint. sub == current {
80
80
// ...add the regions that join us with to the path we've taken...
81
- debug ! ( "find_constraint_paths_from_region: index={:?} constraint={:?}" ,
82
- index, constraint) ;
81
+ debug ! (
82
+ "find_constraint_paths_from_region: index={:?} constraint={:?}" ,
83
+ index, constraint
84
+ ) ;
83
85
let next_region = constraint. sup . clone ( ) ;
84
86
85
87
// ...unless we've visited it since this was added...
@@ -95,27 +97,41 @@ impl<'tcx> RegionInferenceContext<'tcx> {
95
97
96
98
if upcoming. is_empty ( ) {
97
99
// If we didn't find any edges then this is the end of a path...
98
- debug ! ( "find_constraint_paths_from_region: new end region current={:?}" , current) ;
100
+ debug ! (
101
+ "find_constraint_paths_from_region: new end region current={:?}" ,
102
+ current
103
+ ) ;
99
104
end_regions. insert ( current) ;
100
105
} else {
101
106
// ...but, if we did find edges, then add these to the regions yet to visit.
102
- debug ! ( "find_constraint_paths_from_region: extend next upcoming={:?}" , upcoming) ;
107
+ debug ! (
108
+ "find_constraint_paths_from_region: extend next upcoming={:?}" ,
109
+ upcoming
110
+ ) ;
103
111
next. extend ( upcoming) ;
104
112
}
105
-
106
113
}
107
114
108
115
// Now we've visited each point, compute the final paths.
109
116
let mut paths: Vec < Vec < ConstraintIndex > > = Vec :: new ( ) ;
110
- debug ! ( "find_constraint_paths_from_region: end_regions={:?}" , end_regions) ;
117
+ debug ! (
118
+ "find_constraint_paths_from_region: end_regions={:?}" ,
119
+ end_regions
120
+ ) ;
111
121
for end_region in end_regions {
112
- debug ! ( "find_constraint_paths_from_region: end_region={:?}" , end_region) ;
122
+ debug ! (
123
+ "find_constraint_paths_from_region: end_region={:?}" ,
124
+ end_region
125
+ ) ;
113
126
114
127
// Get the constraint and region that led to this end point.
115
128
// We can unwrap as we know if end_point was in the vector that it
116
129
// must also be in our previous map.
117
130
let ( mut index, mut region) = previous. get ( & end_region) . unwrap ( ) ;
118
- debug ! ( "find_constraint_paths_from_region: index={:?} region={:?}" , index, region) ;
131
+ debug ! (
132
+ "find_constraint_paths_from_region: index={:?} region={:?}" ,
133
+ index, region
134
+ ) ;
119
135
120
136
// Keep track of the indices.
121
137
let mut path: Vec < ConstraintIndex > = vec ! [ index] ;
@@ -125,7 +141,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
125
141
index = p. 0 ;
126
142
region = p. 1 ;
127
143
128
- debug ! ( "find_constraint_paths_from_region: index={:?} region={:?}" , index, region) ;
144
+ debug ! (
145
+ "find_constraint_paths_from_region: index={:?} region={:?}" ,
146
+ index, region
147
+ ) ;
129
148
path. push ( index) ;
130
149
}
131
150
@@ -140,16 +159,28 @@ impl<'tcx> RegionInferenceContext<'tcx> {
140
159
/// This function will return true if a constraint is interesting and false if a constraint
141
160
/// is not. It is useful in filtering constraint paths to only interesting points.
142
161
fn constraint_is_interesting ( & self , index : & ConstraintIndex ) -> bool {
143
- self . constraints . get ( * index) . filter ( |constraint| {
144
- debug ! ( "constraint_is_interesting: locations={:?} constraint={:?}" ,
145
- constraint. locations, constraint) ;
146
- if let Locations :: Interesting ( _) = constraint. locations { true } else { false }
147
- } ) . is_some ( )
162
+ self . constraints
163
+ . get ( * index)
164
+ . filter ( |constraint| {
165
+ debug ! (
166
+ "constraint_is_interesting: locations={:?} constraint={:?}" ,
167
+ constraint. locations, constraint
168
+ ) ;
169
+ if let Locations :: Interesting ( _) = constraint. locations {
170
+ true
171
+ } else {
172
+ false
173
+ }
174
+ } )
175
+ . is_some ( )
148
176
}
149
177
150
178
/// This function classifies a constraint from a location.
151
- fn classify_constraint ( & self , index : & ConstraintIndex ,
152
- mir : & Mir < ' tcx > ) -> Option < ( ConstraintCategory , Span ) > {
179
+ fn classify_constraint (
180
+ & self ,
181
+ index : & ConstraintIndex ,
182
+ mir : & Mir < ' tcx > ,
183
+ ) -> Option < ( ConstraintCategory , Span ) > {
153
184
let constraint = self . constraints . get ( * index) ?;
154
185
let span = constraint. locations . span ( mir) ;
155
186
let location = constraint. locations . from_location ( ) ?;
@@ -182,7 +213,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
182
213
_ => ConstraintCategory :: Other ,
183
214
}
184
215
}
185
- } ,
216
+ }
186
217
_ => ConstraintCategory :: Other ,
187
218
}
188
219
} ;
@@ -234,18 +265,21 @@ impl<'tcx> RegionInferenceContext<'tcx> {
234
265
let path = constraints. iter ( ) . min_by_key ( |p| p. len ( ) ) . unwrap ( ) ;
235
266
debug ! ( "report_error: shortest_path={:?}" , path) ;
236
267
237
- let mut categorized_path = path. iter ( ) . filter_map ( |index| {
238
- self . classify_constraint ( index, mir)
239
- } ) . collect :: < Vec < ( ConstraintCategory , Span ) > > ( ) ;
268
+ let mut categorized_path = path. iter ( )
269
+ . filter_map ( |index| self . classify_constraint ( index, mir) )
270
+ . collect :: < Vec < ( ConstraintCategory , Span ) > > ( ) ;
240
271
debug ! ( "report_error: categorized_path={:?}" , categorized_path) ;
241
272
242
273
categorized_path. sort_by ( |p0, p1| p0. 0 . cmp ( & p1. 0 ) ) ;
243
274
debug ! ( "report_error: sorted_path={:?}" , categorized_path) ;
244
275
245
276
if let Some ( ( category, span) ) = & categorized_path. first ( ) {
246
277
let mut diag = infcx. tcx . sess . struct_span_err (
247
- * span, & format ! ( "{} requires that data must outlive {}" ,
248
- category, outlived_fr_string) ,
278
+ * span,
279
+ & format ! (
280
+ "{} requires that data must outlive {}" ,
281
+ category, outlived_fr_string
282
+ ) ,
249
283
) ;
250
284
251
285
diag. emit ( ) ;
@@ -269,8 +303,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
269
303
270
304
/// Tries to finds a good span to blame for the fact that `fr1`
271
305
/// contains `fr2`.
272
- pub ( super ) fn blame_constraint ( & self , fr1 : RegionVid ,
273
- elem : impl ToElementIndex ) -> ConstraintIndex {
306
+ pub ( super ) fn blame_constraint (
307
+ & self ,
308
+ fr1 : RegionVid ,
309
+ elem : impl ToElementIndex ,
310
+ ) -> ConstraintIndex {
274
311
// Find everything that influenced final value of `fr`.
275
312
let influenced_fr1 = self . dependencies ( fr1) ;
276
313
0 commit comments