88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11+ use std:: fmt:: { self , Display } ;
1112use borrow_check:: nll:: region_infer:: RegionInferenceContext ;
1213use borrow_check:: nll:: universal_regions:: DefiningTy ;
1314use borrow_check:: nll:: ToRegionVid ;
@@ -23,6 +24,24 @@ use syntax::ast::{Name, DUMMY_NODE_ID};
2324use syntax:: symbol:: keywords;
2425use syntax_pos:: symbol:: InternedString ;
2526
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+
2645impl < ' tcx > RegionInferenceContext < ' tcx > {
2746 /// Maps from an internal MIR region vid to something that we can
2847 /// report to the user. In some cases, the region vids will map
@@ -57,7 +76,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
5776 fr : RegionVid ,
5877 counter : & mut usize ,
5978 diag : & mut DiagnosticBuilder ,
60- ) -> InternedString {
79+ ) -> RegionName {
6180 debug ! ( "give_region_a_name(fr={:?}, counter={})" , fr, counter) ;
6281
6382 assert ! ( self . universal_regions. is_universal_region( fr) ) ;
@@ -95,27 +114,29 @@ impl<'tcx> RegionInferenceContext<'tcx> {
95114 fr : RegionVid ,
96115 counter : & mut usize ,
97116 diag : & mut DiagnosticBuilder < ' _ > ,
98- ) -> Option < InternedString > {
117+ ) -> Option < RegionName > {
99118 let error_region = self . to_error_region ( fr) ?;
100119
101120 debug ! ( "give_region_a_name: error_region = {:?}" , error_region) ;
102121 match error_region {
103122 ty:: ReEarlyBound ( ebr) => {
104123 if ebr. has_name ( ) {
105124 self . highlight_named_span ( tcx, error_region, & ebr. name , diag) ;
106- Some ( ebr. name )
125+ Some ( RegionName :: Named ( ebr. name ) )
107126 } else {
108127 None
109128 }
110129 }
111130
112- ty:: ReStatic => Some ( keywords:: StaticLifetime . name ( ) . as_interned_str ( ) ) ,
131+ ty:: ReStatic => Some ( RegionName :: Named (
132+ keywords:: StaticLifetime . name ( ) . as_interned_str ( )
133+ ) ) ,
113134
114135 ty:: ReFree ( free_region) => match free_region. bound_region {
115136 ty:: BoundRegion :: BrNamed ( _, name) => {
116137 self . highlight_named_span ( tcx, error_region, & name, diag) ;
117- Some ( name)
118- }
138+ Some ( RegionName :: Named ( name) )
139+ } ,
119140
120141 ty:: BoundRegion :: BrEnv => {
121142 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> {
132153 let region_name = self . synthesize_region_name ( counter) ;
133154 diag. span_label (
134155 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+ ) ,
136160 ) ;
137161
138162 let closure_kind_ty = substs. closure_kind_ty ( def_id, tcx) ;
@@ -227,7 +251,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
227251 fr : RegionVid ,
228252 counter : & mut usize ,
229253 diag : & mut DiagnosticBuilder < ' _ > ,
230- ) -> Option < InternedString > {
254+ ) -> Option < RegionName > {
231255 let implicit_inputs = self . universal_regions . defining_ty . implicit_inputs ( ) ;
232256 let argument_index = self . get_argument_index_for_region ( infcx. tcx , fr) ?;
233257
@@ -259,7 +283,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
259283 argument_index : usize ,
260284 counter : & mut usize ,
261285 diag : & mut DiagnosticBuilder < ' _ > ,
262- ) -> Option < InternedString > {
286+ ) -> Option < RegionName > {
263287 let mir_node_id = infcx. tcx . hir . as_local_node_id ( mir_def_id) ?;
264288 let fn_decl = infcx. tcx . hir . fn_decl ( mir_node_id) ?;
265289 let argument_hir_ty: & hir:: Ty = & fn_decl. inputs [ argument_index] ;
@@ -306,7 +330,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
306330 argument_ty : Ty < ' tcx > ,
307331 counter : & mut usize ,
308332 diag : & mut DiagnosticBuilder < ' _ > ,
309- ) -> Option < InternedString > {
333+ ) -> Option < RegionName > {
310334 let type_name = with_highlight_region ( needle_fr, * counter, || {
311335 infcx. extract_type_name ( & argument_ty)
312336 } ) ;
@@ -361,7 +385,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
361385 argument_hir_ty : & hir:: Ty ,
362386 counter : & mut usize ,
363387 diag : & mut DiagnosticBuilder < ' _ > ,
364- ) -> Option < InternedString > {
388+ ) -> Option < RegionName > {
365389 let search_stack: & mut Vec < ( Ty < ' tcx > , & hir:: Ty ) > = & mut Vec :: new ( ) ;
366390
367391 search_stack. push ( ( argument_ty, argument_hir_ty) ) ;
@@ -457,7 +481,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
457481 counter : & mut usize ,
458482 diag : & mut DiagnosticBuilder < ' _ > ,
459483 search_stack : & mut Vec < ( Ty < ' tcx > , & ' hir hir:: Ty ) > ,
460- ) -> Option < InternedString > {
484+ ) -> Option < RegionName > {
461485 // Did the user give explicit arguments? (e.g., `Foo<..>`)
462486 let args = last_segment. args . as_ref ( ) ?;
463487 let lifetime = self . try_match_adt_and_generic_args ( substs, needle_fr, args, search_stack) ?;
@@ -467,7 +491,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
467491 | hir:: LifetimeName :: Underscore => {
468492 let region_name = self . synthesize_region_name ( counter) ;
469493 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+ ) ;
471498 return Some ( region_name) ;
472499 }
473500
@@ -544,7 +571,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
544571 fr : RegionVid ,
545572 counter : & mut usize ,
546573 diag : & mut DiagnosticBuilder < ' _ > ,
547- ) -> Option < InternedString > {
574+ ) -> Option < RegionName > {
548575 let upvar_index = self . get_upvar_index_for_region ( tcx, fr) ?;
549576 let ( upvar_name, upvar_span) =
550577 self . get_upvar_name_and_span_for_region ( tcx, mir, upvar_index) ;
@@ -573,7 +600,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
573600 fr : RegionVid ,
574601 counter : & mut usize ,
575602 diag : & mut DiagnosticBuilder < ' _ > ,
576- ) -> Option < InternedString > {
603+ ) -> Option < RegionName > {
577604 let tcx = infcx. tcx ;
578605
579606 let return_ty = self . universal_regions . unnormalized_output_ty ;
@@ -622,10 +649,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
622649
623650 /// Create a synthetic region named `'1`, incrementing the
624651 /// counter.
625- fn synthesize_region_name ( & self , counter : & mut usize ) -> InternedString {
652+ fn synthesize_region_name ( & self , counter : & mut usize ) -> RegionName {
626653 let c = * counter;
627654 * counter += 1 ;
628655
629- Name :: intern ( & format ! ( "'{:?}" , c) ) . as_interned_str ( )
656+ RegionName :: Synthesized ( Name :: intern ( & format ! ( "'{:?}" , c) ) . as_interned_str ( ) )
630657 }
631658}
0 commit comments