Skip to content

Commit 22566ed

Browse files
committed
Use code snippet to get lifetime literal name instead of lifetime.ident
Signed-off-by: xizheyin <[email protected]>
1 parent ca50320 commit 22566ed

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,15 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
30883088
lifetime_ref: &ast::Lifetime,
30893089
outer_lifetime_ref: Option<Ident>,
30903090
) {
3091+
// Lifetime may be `'r#fn` or `'a`, so we need to get the actual name by code snippet
3092+
let lifetime_name = if let Ok(lifetime_name) =
3093+
self.r.tcx.sess.source_map().span_to_snippet(lifetime_ref.ident.span)
3094+
{
3095+
lifetime_name
3096+
} else {
3097+
lifetime_ref.ident.to_string()
3098+
};
3099+
30913100
debug_assert_ne!(lifetime_ref.ident.name, kw::UnderscoreLifetime);
30923101
let mut err = if let Some(outer) = outer_lifetime_ref {
30933102
struct_span_code_err!(
@@ -3104,13 +3113,13 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
31043113
lifetime_ref.ident.span,
31053114
E0261,
31063115
"use of undeclared lifetime name `{}`",
3107-
lifetime_ref.ident
3116+
lifetime_name
31083117
)
31093118
.with_span_label(lifetime_ref.ident.span, "undeclared lifetime")
31103119
};
31113120

31123121
// Check if this is a typo of `'static`.
3113-
if edit_distance(lifetime_ref.ident.name.as_str(), "'static", 2).is_some() {
3122+
if edit_distance(lifetime_name.as_str(), "'static", 2).is_some() {
31143123
err.span_suggestion_verbose(
31153124
lifetime_ref.ident.span,
31163125
"you may have misspelled the `'static` lifetime",
@@ -3120,7 +3129,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
31203129
} else {
31213130
self.suggest_introducing_lifetime(
31223131
&mut err,
3123-
Some(lifetime_ref.ident.name.as_str()),
3132+
Some(lifetime_name.as_str()),
31243133
|err, _, span, message, suggestion, span_suggs| {
31253134
err.multipart_suggestion_with_style(
31263135
message,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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) {
55
| - ^^^^^ undeclared lifetime
66
| |
7-
| help: consider introducing lifetime `'fn` here: `<'fn>`
7+
| help: consider introducing lifetime `'r#fn` here: `<'r#fn>`
88

99
error: aborting due to 1 previous error
1010

0 commit comments

Comments
 (0)