Skip to content

Commit 105267e

Browse files
committed
Add lifetime-aware support for Display impl of Ident
Signed-off-by: xizheyin <[email protected]>
1 parent 131581a commit 105267e

File tree

6 files changed

+68
-32
lines changed

6 files changed

+68
-32
lines changed

compiler/rustc_parse/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ pub(crate) struct KeywordLifetime {
21912191
pub(crate) struct InvalidLabel {
21922192
#[primary_span]
21932193
pub span: Span,
2194-
pub name: Symbol,
2194+
pub name: String,
21952195
}
21962196

21972197
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3077,7 +3077,10 @@ impl<'a> Parser<'a> {
30773077
if let Some((ident, is_raw)) = self.token.lifetime() {
30783078
// Disallow `'fn`, but with a better error message than `expect_lifetime`.
30793079
if matches!(is_raw, IdentIsRaw::No) && ident.without_first_quote().is_reserved() {
3080-
self.dcx().emit_err(errors::InvalidLabel { span: ident.span, name: ident.name });
3080+
self.dcx().emit_err(errors::InvalidLabel {
3081+
span: ident.span,
3082+
name: ident.name.to_string(),
3083+
});
30813084
}
30823085

30833086
self.bump();

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,7 +3127,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
31273127
} else {
31283128
self.suggest_introducing_lifetime(
31293129
&mut err,
3130-
Some(lifetime_ref.ident.name.as_str()),
3130+
Some(lifetime_ref.ident),
31313131
|err, _, span, message, suggestion, span_suggs| {
31323132
err.multipart_suggestion_verbose(
31333133
message,
@@ -3145,7 +3145,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
31453145
fn suggest_introducing_lifetime(
31463146
&self,
31473147
err: &mut Diag<'_>,
3148-
name: Option<&str>,
3148+
name: Option<Ident>,
31493149
suggest: impl Fn(
31503150
&mut Diag<'_>,
31513151
bool,
@@ -3192,7 +3192,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
31923192
let mut rm_inner_binders: FxIndexSet<Span> = Default::default();
31933193
let (span, sugg) = if span.is_empty() {
31943194
let mut binder_idents: FxIndexSet<Ident> = Default::default();
3195-
binder_idents.insert(Ident::from_str(name.unwrap_or("'a")));
3195+
binder_idents.insert(name.unwrap_or(Ident::from_str("'a")));
31963196

31973197
// We need to special case binders in the following situation:
31983198
// Change `T: for<'a> Trait<T> + 'b` to `for<'a, 'b> T: Trait<T> + 'b`
@@ -3228,7 +3228,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32283228
if i != 0 {
32293229
binders += ", ";
32303230
}
3231-
binders += x.as_str();
3231+
binders += &x.to_string();
32323232
binders
32333233
},
32343234
);
@@ -3247,15 +3247,15 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32473247
.source_map()
32483248
.span_through_char(span, '<')
32493249
.shrink_to_hi();
3250-
let sugg = format!("{}, ", name.unwrap_or("'a"));
3250+
let sugg = format!("{}, ", name.unwrap_or(Ident::from_str("'a")));
32513251
(span, sugg)
32523252
};
32533253

32543254
if higher_ranked {
32553255
let message = Cow::from(format!(
32563256
"consider making the {} lifetime-generic with a new `{}` lifetime",
32573257
kind.descr(),
3258-
name.unwrap_or("'a"),
3258+
name.unwrap_or(Ident::from_str("'a")),
32593259
));
32603260
should_continue = suggest(
32613261
err,

compiler/rustc_span/src/symbol.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2477,6 +2477,10 @@ impl Ident {
24772477
pub fn as_str(&self) -> &str {
24782478
self.name.as_str()
24792479
}
2480+
2481+
pub fn as_lifetime(&self) -> Option<Ident> {
2482+
self.name.as_lifetime().map(|sym| Ident::with_dummy_span(sym))
2483+
}
24802484
}
24812485

24822486
impl PartialEq for Ident {
@@ -2546,6 +2550,14 @@ impl IdentPrinter {
25462550

25472551
impl fmt::Display for IdentPrinter {
25482552
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2553+
if let Some(lifetime) = self.symbol.as_lifetime() {
2554+
f.write_str("'")?;
2555+
if self.is_raw {
2556+
f.write_str("r#")?;
2557+
}
2558+
return fmt::Display::fmt(&lifetime, f);
2559+
}
2560+
25492561
if self.is_raw {
25502562
f.write_str("r#")?;
25512563
} else if self.symbol == kw::DollarCrate {
@@ -2640,6 +2652,11 @@ impl Symbol {
26402652
self == sym::empty
26412653
}
26422654

2655+
pub fn as_lifetime(self) -> Option<Symbol> {
2656+
let name = self.as_str();
2657+
name.strip_prefix("'").map(Symbol::intern)
2658+
}
2659+
26432660
/// This method is supposed to be used in error messages, so it's expected to be
26442661
/// identical to printing the original identifier token written in source code
26452662
/// (`token_to_string`, `Ident::to_string`), except that symbols don't keep the rawness flag
@@ -2946,7 +2963,13 @@ impl Ident {
29462963
/// We see this identifier in a normal identifier position, like variable name or a type.
29472964
/// How was it written originally? Did it use the raw form? Let's try to guess.
29482965
pub fn is_raw_guess(self) -> bool {
2949-
self.name.can_be_raw() && self.is_reserved()
2966+
if self.name == kw::StaticLifetime || self.name == kw::UnderscoreLifetime {
2967+
false
2968+
} else if let Some(lifetime) = self.as_lifetime() {
2969+
lifetime.is_raw_guess()
2970+
} else {
2971+
self.name.can_be_raw() && self.is_reserved()
2972+
}
29502973
}
29512974

29522975
/// Whether this would be the identifier for a tuple field like `self.0`, as

tests/ui/lifetimes/lifetime-errors/error-lifetime-name-issue-143150.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
//@ edition: 2021
2-
fn a(_: dyn Trait + 'r#fn) { //~ ERROR use of undeclared lifetime name `'fn` [E0261]
2+
fn a(_: dyn Trait + 'r#fn) { //~ ERROR use of undeclared lifetime name `'r#fn` [E0261]
33

44
}
55

66
trait Trait {}
77

88
#[derive(Eq, PartialEq)]
99
struct Test {
10-
a: &'r#fn str,
11-
//~^ ERROR use of undeclared lifetime name `'fn` [E0261]
12-
//~| ERROR use of undeclared lifetime name `'fn` [E0261]
10+
a: &'r#fn str,
11+
//~^ ERROR use of undeclared lifetime name `'r#fn` [E0261]
12+
//~| ERROR use of undeclared lifetime name `'r#fn` [E0261]
1313
}
1414

1515
trait Trait1<T>
16-
where T: for<'a> Trait1<T> + 'r#fn { } //~ ERROR use of undeclared lifetime name `'fn` [E0261]
16+
where T: for<'a> Trait1<T> + 'r#fn { } //~ ERROR use of undeclared lifetime name `'r#fn` [E0261]
1717

1818

1919

tests/ui/lifetimes/lifetime-errors/error-lifetime-name-issue-143150.stderr

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,55 @@
1-
error[E0261]: use of undeclared lifetime name `'fn`
1+
error[E0261]: use of undeclared lifetime name `'r#fn`
22
--> $DIR/error-lifetime-name-issue-143150.rs:2:21
33
|
44
LL | fn a(_: dyn Trait + 'r#fn) {
5-
| - ^^^^^ undeclared lifetime
6-
| |
7-
| help: consider introducing lifetime `'fn` here: `<'fn>`
5+
| ^^^^^ undeclared lifetime
6+
|
7+
help: consider introducing lifetime `'r#fn` here
8+
|
9+
LL | fn a<'r#fn>(_: dyn Trait + 'r#fn) {
10+
| +++++++
811

9-
error[E0261]: use of undeclared lifetime name `'fn`
12+
error[E0261]: use of undeclared lifetime name `'r#fn`
1013
--> $DIR/error-lifetime-name-issue-143150.rs:10:9
1114
|
12-
LL | struct Test {
13-
| - help: consider introducing lifetime `'fn` here: `<'fn>`
14-
LL | a: &'r#fn str,
15+
LL | a: &'r#fn str,
1516
| ^^^^^ undeclared lifetime
17+
|
18+
help: consider introducing lifetime `'r#fn` here
19+
|
20+
LL | struct Test<'r#fn> {
21+
| +++++++
1622

17-
error[E0261]: use of undeclared lifetime name `'fn`
23+
error[E0261]: use of undeclared lifetime name `'r#fn`
1824
--> $DIR/error-lifetime-name-issue-143150.rs:10:9
1925
|
2026
LL | #[derive(Eq, PartialEq)]
21-
| -- lifetime `'fn` is missing in item created through this procedural macro
27+
| -- lifetime `'r#fn` is missing in item created through this procedural macro
2228
LL | struct Test {
23-
| - help: consider introducing lifetime `'fn` here: `<'fn>`
24-
LL | a: &'r#fn str,
29+
LL | a: &'r#fn str,
2530
| ^^^^^ undeclared lifetime
31+
|
32+
help: consider introducing lifetime `'r#fn` here
33+
|
34+
LL | struct Test<'r#fn> {
35+
| +++++++
2636

27-
error[E0261]: use of undeclared lifetime name `'fn`
37+
error[E0261]: use of undeclared lifetime name `'r#fn`
2838
--> $DIR/error-lifetime-name-issue-143150.rs:16:32
2939
|
3040
LL | where T: for<'a> Trait1<T> + 'r#fn { }
3141
| ^^^^^ undeclared lifetime
3242
|
3343
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
34-
help: consider making the bound lifetime-generic with a new `'fn` lifetime
44+
help: consider making the bound lifetime-generic with a new `'r#fn` lifetime
3545
|
3646
LL - where T: for<'a> Trait1<T> + 'r#fn { }
37-
LL + where for<'fn, 'a> T: Trait1<T> + 'r#fn { }
47+
LL + where for<'r#fn, 'a> T: Trait1<T> + 'r#fn { }
3848
|
39-
help: consider introducing lifetime `'fn` here
49+
help: consider introducing lifetime `'r#fn` here
4050
|
41-
LL | trait Trait1<'fn, T>
42-
| ++++
51+
LL | trait Trait1<'r#fn, T>
52+
| ++++++
4353

4454
error: aborting due to 4 previous errors
4555

0 commit comments

Comments
 (0)