Skip to content

Commit 20fdc39

Browse files
committed
Suggest to rename self when it occur in let bindings
Signed-off-by: xizheyin <[email protected]>
1 parent 0b5f039 commit 20fdc39

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,12 +1186,18 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11861186
let is_assoc_fn = self.self_type_is_available();
11871187
let self_from_macro = "a `self` parameter, but a macro invocation can only \
11881188
access identifiers it receives from parameters";
1189-
if let Some((fn_kind, span)) = &self.diag_metadata.current_function {
1189+
if let Some((fn_kind, fn_span)) = &self.diag_metadata.current_function {
11901190
// The current function has a `self` parameter, but we were unable to resolve
11911191
// a reference to `self`. This can only happen if the `self` identifier we
1192-
// are resolving came from a different hygiene context.
1192+
// are resolving came from a different hygiene context or a variable binding.
11931193
if fn_kind.decl().inputs.get(0).is_some_and(|p| p.is_self()) {
1194-
err.span_label(*span, format!("this function has {self_from_macro}"));
1194+
if span.from_expansion() {
1195+
err.span_label(*fn_span, format!("this function has {self_from_macro}"));
1196+
} else {
1197+
// If the `self` is neither from a macro nor from a function self parameter,
1198+
// It is a local variable, we should suggest to rename `self` to `self_`
1199+
err.span_help(span, format!("rename `self` to `self_`"));
1200+
}
11951201
} else {
11961202
let doesnt = if is_assoc_fn {
11971203
let (span, sugg) = fn_kind
@@ -1204,7 +1210,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
12041210
// This avoids placing the suggestion into the visibility specifier.
12051211
let span = fn_kind
12061212
.ident()
1207-
.map_or(*span, |ident| span.with_lo(ident.span.hi()));
1213+
.map_or(*fn_span, |ident| fn_span.with_lo(ident.span.hi()));
12081214
(
12091215
self.r
12101216
.tcx

tests/ui/resolve/false-self-in-macro-issue-143134.stderr

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
error[E0424]: expected unit struct, unit variant or constant, found local variable `self`
22
--> $DIR/false-self-in-macro-issue-143134.rs:6:13
33
|
4-
LL | / fn f(self) {
5-
LL | | let self = ();
6-
| | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
7-
LL | | }
8-
| |_____- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters
4+
LL | let self = ();
5+
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
6+
|
7+
help: rename `self` to `self_`
8+
--> $DIR/false-self-in-macro-issue-143134.rs:6:13
9+
|
10+
LL | let self = ();
11+
| ^^^^
912

1013
error: aborting due to 1 previous error
1114

0 commit comments

Comments
 (0)