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:: { self , Display } ;
11
12
use borrow_check:: nll:: region_infer:: RegionInferenceContext ;
12
13
use borrow_check:: nll:: universal_regions:: DefiningTy ;
13
14
use borrow_check:: nll:: ToRegionVid ;
@@ -23,6 +24,24 @@ use syntax::ast::{Name, DUMMY_NODE_ID};
23
24
use syntax:: symbol:: keywords;
24
25
use syntax_pos:: symbol:: InternedString ;
25
26
27
+ /// Name of a region used in error reporting. Variants denote the source of the region name -
28
+ /// whether it was synthesized for the error message and therefore should not be used in
29
+ /// suggestions; or whether it was found from the region.
30
+ #[ derive( Debug ) ]
31
+ pub ( crate ) enum RegionName {
32
+ Named ( InternedString ) ,
33
+ Synthesized ( InternedString ) ,
34
+ }
35
+
36
+ impl Display for RegionName {
37
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
38
+ match self {
39
+ RegionName :: Named ( name) | RegionName :: Synthesized ( name) =>
40
+ write ! ( f, "{}" , name) ,
41
+ }
42
+ }
43
+ }
44
+
26
45
impl < ' tcx > RegionInferenceContext < ' tcx > {
27
46
/// Maps from an internal MIR region vid to something that we can
28
47
/// report to the user. In some cases, the region vids will map
@@ -57,7 +76,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
57
76
fr : RegionVid ,
58
77
counter : & mut usize ,
59
78
diag : & mut DiagnosticBuilder ,
60
- ) -> InternedString {
79
+ ) -> RegionName {
61
80
debug ! ( "give_region_a_name(fr={:?}, counter={})" , fr, counter) ;
62
81
63
82
assert ! ( self . universal_regions. is_universal_region( fr) ) ;
@@ -95,27 +114,29 @@ impl<'tcx> RegionInferenceContext<'tcx> {
95
114
fr : RegionVid ,
96
115
counter : & mut usize ,
97
116
diag : & mut DiagnosticBuilder < ' _ > ,
98
- ) -> Option < InternedString > {
117
+ ) -> Option < RegionName > {
99
118
let error_region = self . to_error_region ( fr) ?;
100
119
101
120
debug ! ( "give_region_a_name: error_region = {:?}" , error_region) ;
102
121
match error_region {
103
122
ty:: ReEarlyBound ( ebr) => {
104
123
if ebr. has_name ( ) {
105
124
self . highlight_named_span ( tcx, error_region, & ebr. name , diag) ;
106
- Some ( ebr. name )
125
+ Some ( RegionName :: Named ( ebr. name ) )
107
126
} else {
108
127
None
109
128
}
110
129
}
111
130
112
- ty:: ReStatic => Some ( keywords:: StaticLifetime . name ( ) . as_interned_str ( ) ) ,
131
+ ty:: ReStatic => Some ( RegionName :: Named (
132
+ keywords:: StaticLifetime . name ( ) . as_interned_str ( )
133
+ ) ) ,
113
134
114
135
ty:: ReFree ( free_region) => match free_region. bound_region {
115
136
ty:: BoundRegion :: BrNamed ( _, name) => {
116
137
self . highlight_named_span ( tcx, error_region, & name, diag) ;
117
- Some ( name)
118
- }
138
+ Some ( RegionName :: Named ( name) )
139
+ } ,
119
140
120
141
ty:: BoundRegion :: BrEnv => {
121
142
let mir_node_id = tcx. hir . as_local_node_id ( mir_def_id) . expect ( "non-local mir" ) ;
@@ -132,7 +153,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
132
153
let region_name = self . synthesize_region_name ( counter) ;
133
154
diag. span_label (
134
155
args_span,
135
- format ! ( "lifetime `{}` represents this closure's body" , region_name) ,
156
+ format ! (
157
+ "lifetime `{}` represents this closure's body" ,
158
+ region_name
159
+ ) ,
136
160
) ;
137
161
138
162
let closure_kind_ty = substs. closure_kind_ty ( def_id, tcx) ;
@@ -227,7 +251,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
227
251
fr : RegionVid ,
228
252
counter : & mut usize ,
229
253
diag : & mut DiagnosticBuilder < ' _ > ,
230
- ) -> Option < InternedString > {
254
+ ) -> Option < RegionName > {
231
255
let implicit_inputs = self . universal_regions . defining_ty . implicit_inputs ( ) ;
232
256
let argument_index = self . get_argument_index_for_region ( infcx. tcx , fr) ?;
233
257
@@ -259,7 +283,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
259
283
argument_index : usize ,
260
284
counter : & mut usize ,
261
285
diag : & mut DiagnosticBuilder < ' _ > ,
262
- ) -> Option < InternedString > {
286
+ ) -> Option < RegionName > {
263
287
let mir_node_id = infcx. tcx . hir . as_local_node_id ( mir_def_id) ?;
264
288
let fn_decl = infcx. tcx . hir . fn_decl ( mir_node_id) ?;
265
289
let argument_hir_ty: & hir:: Ty = & fn_decl. inputs [ argument_index] ;
@@ -306,7 +330,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
306
330
argument_ty : Ty < ' tcx > ,
307
331
counter : & mut usize ,
308
332
diag : & mut DiagnosticBuilder < ' _ > ,
309
- ) -> Option < InternedString > {
333
+ ) -> Option < RegionName > {
310
334
let type_name = with_highlight_region ( needle_fr, * counter, || {
311
335
infcx. extract_type_name ( & argument_ty)
312
336
} ) ;
@@ -361,7 +385,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
361
385
argument_hir_ty : & hir:: Ty ,
362
386
counter : & mut usize ,
363
387
diag : & mut DiagnosticBuilder < ' _ > ,
364
- ) -> Option < InternedString > {
388
+ ) -> Option < RegionName > {
365
389
let search_stack: & mut Vec < ( Ty < ' tcx > , & hir:: Ty ) > = & mut Vec :: new ( ) ;
366
390
367
391
search_stack. push ( ( argument_ty, argument_hir_ty) ) ;
@@ -457,7 +481,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
457
481
counter : & mut usize ,
458
482
diag : & mut DiagnosticBuilder < ' _ > ,
459
483
search_stack : & mut Vec < ( Ty < ' tcx > , & ' hir hir:: Ty ) > ,
460
- ) -> Option < InternedString > {
484
+ ) -> Option < RegionName > {
461
485
// Did the user give explicit arguments? (e.g., `Foo<..>`)
462
486
let args = last_segment. args . as_ref ( ) ?;
463
487
let lifetime = self . try_match_adt_and_generic_args ( substs, needle_fr, args, search_stack) ?;
@@ -467,7 +491,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
467
491
| hir:: LifetimeName :: Underscore => {
468
492
let region_name = self . synthesize_region_name ( counter) ;
469
493
let ampersand_span = lifetime. span ;
470
- diag. span_label ( ampersand_span, format ! ( "let's call this `{}`" , region_name) ) ;
494
+ diag. span_label (
495
+ ampersand_span,
496
+ format ! ( "let's call this `{}`" , region_name)
497
+ ) ;
471
498
return Some ( region_name) ;
472
499
}
473
500
@@ -544,7 +571,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
544
571
fr : RegionVid ,
545
572
counter : & mut usize ,
546
573
diag : & mut DiagnosticBuilder < ' _ > ,
547
- ) -> Option < InternedString > {
574
+ ) -> Option < RegionName > {
548
575
let upvar_index = self . get_upvar_index_for_region ( tcx, fr) ?;
549
576
let ( upvar_name, upvar_span) =
550
577
self . get_upvar_name_and_span_for_region ( tcx, mir, upvar_index) ;
@@ -573,7 +600,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
573
600
fr : RegionVid ,
574
601
counter : & mut usize ,
575
602
diag : & mut DiagnosticBuilder < ' _ > ,
576
- ) -> Option < InternedString > {
603
+ ) -> Option < RegionName > {
577
604
let tcx = infcx. tcx ;
578
605
579
606
let return_ty = self . universal_regions . unnormalized_output_ty ;
@@ -622,10 +649,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
622
649
623
650
/// Create a synthetic region named `'1`, incrementing the
624
651
/// counter.
625
- fn synthesize_region_name ( & self , counter : & mut usize ) -> InternedString {
652
+ fn synthesize_region_name ( & self , counter : & mut usize ) -> RegionName {
626
653
let c = * counter;
627
654
* counter += 1 ;
628
655
629
- Name :: intern ( & format ! ( "'{:?}" , c) ) . as_interned_str ( )
656
+ RegionName :: Synthesized ( Name :: intern ( & format ! ( "'{:?}" , c) ) . as_interned_str ( ) )
630
657
}
631
658
}
0 commit comments