Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 51af5af

Browse files
committed
extract RegionNameHighlight
1 parent c276334 commit 51af5af

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

src/librustc_mir/borrow_check/diagnostics/outlives_suggestion.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ impl OutlivesSuggestionBuilder {
6060
// Don't give suggestions for upvars, closure return types, or other unnamable
6161
// regions.
6262
RegionNameSource::SynthesizedFreeEnvRegion(..)
63-
| RegionNameSource::CannotMatchHirTy(..)
64-
| RegionNameSource::MatchedHirTy(..)
65-
| RegionNameSource::MatchedAdtAndSegment(..)
63+
| RegionNameSource::AnonRegionFromArgument(..)
6664
| RegionNameSource::AnonRegionFromUpvar(..)
6765
| RegionNameSource::AnonRegionFromOutput(..)
6866
| RegionNameSource::AnonRegionFromYieldTy(..)

src/librustc_mir/borrow_check/diagnostics/region_name.rs

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@ crate enum RegionNameSource {
3434
Static,
3535
/// The free region corresponding to the environment of a closure.
3636
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),
4439
/// The region corresponding to a closure upvar.
4540
AnonRegionFromUpvar(Span, String),
4641
/// The region corresponding to the return type of a closure.
@@ -51,16 +46,27 @@ crate enum RegionNameSource {
5146
AnonRegionFromAsyncFn(Span),
5247
}
5348

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+
5462
impl RegionName {
5563
crate fn was_named(&self) -> bool {
5664
match self.source {
5765
RegionNameSource::NamedEarlyBoundRegion(..)
5866
| RegionNameSource::NamedFreeRegion(..)
5967
| RegionNameSource::Static => true,
6068
RegionNameSource::SynthesizedFreeEnvRegion(..)
61-
| RegionNameSource::CannotMatchHirTy(..)
62-
| RegionNameSource::MatchedHirTy(..)
63-
| RegionNameSource::MatchedAdtAndSegment(..)
69+
| RegionNameSource::AnonRegionFromArgument(..)
6470
| RegionNameSource::AnonRegionFromUpvar(..)
6571
| RegionNameSource::AnonRegionFromOutput(..)
6672
| RegionNameSource::AnonRegionFromYieldTy(..)
@@ -74,13 +80,15 @@ impl RegionName {
7480
RegionNameSource::NamedEarlyBoundRegion(span)
7581
| RegionNameSource::NamedFreeRegion(span)
7682
| RegionNameSource::SynthesizedFreeEnvRegion(span, _)
77-
| RegionNameSource::CannotMatchHirTy(span, _)
78-
| RegionNameSource::MatchedHirTy(span)
79-
| RegionNameSource::MatchedAdtAndSegment(span)
8083
| RegionNameSource::AnonRegionFromUpvar(span, _)
8184
| RegionNameSource::AnonRegionFromOutput(span, _, _)
8285
| RegionNameSource::AnonRegionFromYieldTy(span, _)
8386
| 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+
},
8492
}
8593
}
8694

@@ -97,17 +105,22 @@ impl RegionName {
97105
);
98106
diag.note(&note);
99107
}
100-
RegionNameSource::CannotMatchHirTy(span, type_name) => {
108+
RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::CannotMatchHirTy(
109+
span,
110+
type_name,
111+
)) => {
101112
diag.span_label(*span, format!("has type `{}`", type_name));
102113
}
103-
RegionNameSource::MatchedHirTy(span)
114+
RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::MatchedHirTy(span))
104115
| RegionNameSource::AnonRegionFromAsyncFn(span) => {
105116
diag.span_label(
106117
*span,
107118
format!("let's call the lifetime of this reference `{}`", self),
108119
);
109120
}
110-
RegionNameSource::MatchedAdtAndSegment(span) => {
121+
RegionNameSource::AnonRegionFromArgument(
122+
RegionNameHighlight::MatchedAdtAndSegment(span),
123+
) => {
111124
diag.span_label(*span, format!("let's call this `{}`", self));
112125
}
113126
RegionNameSource::AnonRegionFromUpvar(span, upvar_name) => {
@@ -393,7 +406,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
393406
// it so the next value will be used next and return the region name that would
394407
// have been used.
395408
name: self.synthesize_region_name(),
396-
source: RegionNameSource::CannotMatchHirTy(span, type_name),
409+
source: RegionNameSource::AnonRegionFromArgument(
410+
RegionNameHighlight::CannotMatchHirTy(span, type_name),
411+
),
397412
})
398413
} else {
399414
None
@@ -453,7 +468,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
453468

454469
return Some(RegionName {
455470
name: region_name,
456-
source: RegionNameSource::MatchedHirTy(ampersand_span),
471+
source: RegionNameSource::AnonRegionFromArgument(
472+
RegionNameHighlight::MatchedHirTy(ampersand_span),
473+
),
457474
});
458475
}
459476

@@ -534,10 +551,12 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
534551
| hir::LifetimeName::Static
535552
| hir::LifetimeName::Underscore => {
536553
let region_name = self.synthesize_region_name();
537-
let ampersand_span = lifetime.span;
554+
let lifetime_span = lifetime.span;
538555
Some(RegionName {
539556
name: region_name,
540-
source: RegionNameSource::MatchedAdtAndSegment(ampersand_span),
557+
source: RegionNameSource::AnonRegionFromArgument(
558+
RegionNameHighlight::MatchedAdtAndSegment(lifetime_span),
559+
),
541560
})
542561
}
543562

0 commit comments

Comments
 (0)