Skip to content

Commit 79a8c6d

Browse files
authored
Rollup merge of rust-lang#138115 - compiler-errors:static-typo, r=BoxyUwU
Suggest typo fix for static lifetime ...and don't try to introduce a new lifetime param named something like `'statoc`.
2 parents 9e16082 + ac40ea7 commit 79a8c6d

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

Diff for: compiler/rustc_resolve/src/late/diagnostics.rs

+30-18
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
2424
use rustc_hir::{MissingLifetimeKind, PrimTy};
2525
use rustc_middle::ty;
2626
use rustc_session::{Session, lint};
27-
use rustc_span::edit_distance::find_best_match_for_name;
27+
use rustc_span::edit_distance::{edit_distance, find_best_match_for_name};
2828
use rustc_span::edition::Edition;
2929
use rustc_span::hygiene::MacroKind;
3030
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
@@ -2919,23 +2919,35 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
29192919
)
29202920
.with_span_label(lifetime_ref.ident.span, "undeclared lifetime")
29212921
};
2922-
self.suggest_introducing_lifetime(
2923-
&mut err,
2924-
Some(lifetime_ref.ident.name.as_str()),
2925-
|err, _, span, message, suggestion, span_suggs| {
2926-
err.multipart_suggestion_with_style(
2927-
message,
2928-
std::iter::once((span, suggestion)).chain(span_suggs.clone()).collect(),
2929-
Applicability::MaybeIncorrect,
2930-
if span_suggs.is_empty() {
2931-
SuggestionStyle::ShowCode
2932-
} else {
2933-
SuggestionStyle::ShowAlways
2934-
},
2935-
);
2936-
true
2937-
},
2938-
);
2922+
2923+
// Check if this is a typo of `'static`.
2924+
if edit_distance(lifetime_ref.ident.name.as_str(), "'static", 2).is_some() {
2925+
err.span_suggestion_verbose(
2926+
lifetime_ref.ident.span,
2927+
"you may have misspelled the `'static` lifetime",
2928+
"'static",
2929+
Applicability::MachineApplicable,
2930+
);
2931+
} else {
2932+
self.suggest_introducing_lifetime(
2933+
&mut err,
2934+
Some(lifetime_ref.ident.name.as_str()),
2935+
|err, _, span, message, suggestion, span_suggs| {
2936+
err.multipart_suggestion_with_style(
2937+
message,
2938+
std::iter::once((span, suggestion)).chain(span_suggs.clone()).collect(),
2939+
Applicability::MaybeIncorrect,
2940+
if span_suggs.is_empty() {
2941+
SuggestionStyle::ShowCode
2942+
} else {
2943+
SuggestionStyle::ShowAlways
2944+
},
2945+
);
2946+
true
2947+
},
2948+
);
2949+
}
2950+
29392951
err.emit();
29402952
}
29412953

Diff for: tests/ui/lifetimes/static-typos.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn stati<T: 'stati>() {}
2+
//~^ ERROR use of undeclared lifetime name `'stati`
3+
4+
fn statoc<T: 'statoc>() {}
5+
//~^ ERROR use of undeclared lifetime name `'statoc`
6+
7+
fn main() {}

Diff for: tests/ui/lifetimes/static-typos.stderr

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0261]: use of undeclared lifetime name `'stati`
2+
--> $DIR/static-typos.rs:1:13
3+
|
4+
LL | fn stati<T: 'stati>() {}
5+
| ^^^^^^ undeclared lifetime
6+
|
7+
help: you may have misspelled the `'static` lifetime
8+
|
9+
LL | fn stati<T: 'static>() {}
10+
| +
11+
12+
error[E0261]: use of undeclared lifetime name `'statoc`
13+
--> $DIR/static-typos.rs:4:14
14+
|
15+
LL | fn statoc<T: 'statoc>() {}
16+
| ^^^^^^^ undeclared lifetime
17+
|
18+
help: you may have misspelled the `'static` lifetime
19+
|
20+
LL - fn statoc<T: 'statoc>() {}
21+
LL + fn statoc<T: 'static>() {}
22+
|
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)