@@ -34,13 +34,8 @@ crate enum RegionNameSource {
34
34
Static ,
35
35
/// The free region corresponding to the environment of a closure.
36
36
SynthesizedFreeEnvRegion ( Span , String ) ,
37
- /// The region name corresponds to a region where the type annotation is completely missing
38
- /// from the code, e.g. in a closure arguments `|x| { ... }`, where `x` is a reference.
39
- CannotMatchHirTy ( Span , String ) ,
40
- /// The region name corresponds a reference that was found by traversing the type in the HIR.
41
- MatchedHirTy ( Span ) ,
42
- /// A region name from the generics list of a struct/enum/union.
43
- MatchedAdtAndSegment ( Span ) ,
37
+ /// The region corresponding to an argument.
38
+ AnonRegionFromArgument ( RegionNameHighlight ) ,
44
39
/// The region corresponding to a closure upvar.
45
40
AnonRegionFromUpvar ( Span , String ) ,
46
41
/// The region corresponding to the return type of a closure.
@@ -51,16 +46,27 @@ crate enum RegionNameSource {
51
46
AnonRegionFromAsyncFn ( Span ) ,
52
47
}
53
48
49
+ /// Describes what to highlight to explain to the user that we're giving an anonymous region a
50
+ /// synthesized name, and how to highlight it.
51
+ #[ derive( Debug , Clone ) ]
52
+ crate enum RegionNameHighlight {
53
+ /// The anonymous region corresponds to a reference that was found by traversing the type in the HIR.
54
+ MatchedHirTy ( Span ) ,
55
+ /// The anonymous region corresponds to a `'_` in the generics list of a struct/enum/union.
56
+ MatchedAdtAndSegment ( Span ) ,
57
+ /// The anonymous region corresponds to a region where the type annotation is completely missing
58
+ /// from the code, e.g. in a closure arguments `|x| { ... }`, where `x` is a reference.
59
+ CannotMatchHirTy ( Span , String ) ,
60
+ }
61
+
54
62
impl RegionName {
55
63
crate fn was_named ( & self ) -> bool {
56
64
match self . source {
57
65
RegionNameSource :: NamedEarlyBoundRegion ( ..)
58
66
| RegionNameSource :: NamedFreeRegion ( ..)
59
67
| RegionNameSource :: Static => true ,
60
68
RegionNameSource :: SynthesizedFreeEnvRegion ( ..)
61
- | RegionNameSource :: CannotMatchHirTy ( ..)
62
- | RegionNameSource :: MatchedHirTy ( ..)
63
- | RegionNameSource :: MatchedAdtAndSegment ( ..)
69
+ | RegionNameSource :: AnonRegionFromArgument ( ..)
64
70
| RegionNameSource :: AnonRegionFromUpvar ( ..)
65
71
| RegionNameSource :: AnonRegionFromOutput ( ..)
66
72
| RegionNameSource :: AnonRegionFromYieldTy ( ..)
@@ -74,13 +80,15 @@ impl RegionName {
74
80
RegionNameSource :: NamedEarlyBoundRegion ( span)
75
81
| RegionNameSource :: NamedFreeRegion ( span)
76
82
| RegionNameSource :: SynthesizedFreeEnvRegion ( span, _)
77
- | RegionNameSource :: CannotMatchHirTy ( span, _)
78
- | RegionNameSource :: MatchedHirTy ( span)
79
- | RegionNameSource :: MatchedAdtAndSegment ( span)
80
83
| RegionNameSource :: AnonRegionFromUpvar ( span, _)
81
84
| RegionNameSource :: AnonRegionFromOutput ( span, _, _)
82
85
| RegionNameSource :: AnonRegionFromYieldTy ( span, _)
83
86
| RegionNameSource :: AnonRegionFromAsyncFn ( span) => Some ( span) ,
87
+ RegionNameSource :: AnonRegionFromArgument ( ref highlight) => match * highlight {
88
+ RegionNameHighlight :: MatchedHirTy ( span)
89
+ | RegionNameHighlight :: MatchedAdtAndSegment ( span)
90
+ | RegionNameHighlight :: CannotMatchHirTy ( span, _) => Some ( span) ,
91
+ } ,
84
92
}
85
93
}
86
94
@@ -97,17 +105,22 @@ impl RegionName {
97
105
) ;
98
106
diag. note ( & note) ;
99
107
}
100
- RegionNameSource :: CannotMatchHirTy ( span, type_name) => {
108
+ RegionNameSource :: AnonRegionFromArgument ( RegionNameHighlight :: CannotMatchHirTy (
109
+ span,
110
+ type_name,
111
+ ) ) => {
101
112
diag. span_label ( * span, format ! ( "has type `{}`" , type_name) ) ;
102
113
}
103
- RegionNameSource :: MatchedHirTy ( span)
114
+ RegionNameSource :: AnonRegionFromArgument ( RegionNameHighlight :: MatchedHirTy ( span) )
104
115
| RegionNameSource :: AnonRegionFromAsyncFn ( span) => {
105
116
diag. span_label (
106
117
* span,
107
118
format ! ( "let's call the lifetime of this reference `{}`" , self ) ,
108
119
) ;
109
120
}
110
- RegionNameSource :: MatchedAdtAndSegment ( span) => {
121
+ RegionNameSource :: AnonRegionFromArgument (
122
+ RegionNameHighlight :: MatchedAdtAndSegment ( span) ,
123
+ ) => {
111
124
diag. span_label ( * span, format ! ( "let's call this `{}`" , self ) ) ;
112
125
}
113
126
RegionNameSource :: AnonRegionFromUpvar ( span, upvar_name) => {
@@ -393,7 +406,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
393
406
// it so the next value will be used next and return the region name that would
394
407
// have been used.
395
408
name : self . synthesize_region_name ( ) ,
396
- source : RegionNameSource :: CannotMatchHirTy ( span, type_name) ,
409
+ source : RegionNameSource :: AnonRegionFromArgument (
410
+ RegionNameHighlight :: CannotMatchHirTy ( span, type_name) ,
411
+ ) ,
397
412
} )
398
413
} else {
399
414
None
@@ -453,7 +468,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
453
468
454
469
return Some ( RegionName {
455
470
name : region_name,
456
- source : RegionNameSource :: MatchedHirTy ( ampersand_span) ,
471
+ source : RegionNameSource :: AnonRegionFromArgument (
472
+ RegionNameHighlight :: MatchedHirTy ( ampersand_span) ,
473
+ ) ,
457
474
} ) ;
458
475
}
459
476
@@ -534,10 +551,12 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
534
551
| hir:: LifetimeName :: Static
535
552
| hir:: LifetimeName :: Underscore => {
536
553
let region_name = self . synthesize_region_name ( ) ;
537
- let ampersand_span = lifetime. span ;
554
+ let lifetime_span = lifetime. span ;
538
555
Some ( RegionName {
539
556
name : region_name,
540
- source : RegionNameSource :: MatchedAdtAndSegment ( ampersand_span) ,
557
+ source : RegionNameSource :: AnonRegionFromArgument (
558
+ RegionNameHighlight :: MatchedAdtAndSegment ( lifetime_span) ,
559
+ ) ,
541
560
} )
542
561
}
543
562
0 commit comments