Skip to content

Commit 2da698e

Browse files
committed
Remove false label when self failure does not related to macro
Signed-off-by: xizheyin <[email protected]>
1 parent 000f038 commit 2da698e

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,12 +1186,17 @@ 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+
// When the `self` is from a macro, we should add a label
1195+
// If the `self` resolved failed, and it is not from a macro,
1196+
// it's a variable binding, e.g. `let self = 1;`, we do nothing, see issue #143134
1197+
if span.from_expansion() {
1198+
err.span_label(*fn_span, format!("this function has {self_from_macro}"));
1199+
}
11951200
} else {
11961201
let doesnt = if is_assoc_fn {
11971202
let (span, sugg) = fn_kind
@@ -1204,7 +1209,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
12041209
// This avoids placing the suggestion into the visibility specifier.
12051210
let span = fn_kind
12061211
.ident()
1207-
.map_or(*span, |ident| span.with_lo(ident.span.hi()));
1212+
.map_or(*fn_span, |ident| fn_span.with_lo(ident.span.hi()));
12081213
(
12091214
self.r
12101215
.tcx

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
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
96

107
error: aborting due to 1 previous error
118

0 commit comments

Comments
 (0)