Skip to content

Commit c8b2c78

Browse files
authored
Rollup merge of #143177 - xizheyin:143134, r=lcnr
Remove false label when `self` resolve failure does not relate to macro Fixes #143134 In the first commit, I did some code-clean, moving `suggest*` to `suggestions/` dir. In the second, commit, I added ui test. In the third, I change the code, and present the test result. r? compiler
2 parents ab68b0f + 236b392 commit c8b2c78

16 files changed

+31
-6
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,15 +1183,23 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11831183
_ => "`self` value is a keyword only available in methods with a `self` parameter",
11841184
},
11851185
);
1186+
1187+
// using `let self` is wrong even if we're not in an associated method or if we're in a macro expansion.
1188+
// So, we should return early if we're in a pattern, see issue #143134.
1189+
if matches!(source, PathSource::Pat) {
1190+
return true;
1191+
}
1192+
11861193
let is_assoc_fn = self.self_type_is_available();
11871194
let self_from_macro = "a `self` parameter, but a macro invocation can only \
11881195
access identifiers it receives from parameters";
1189-
if let Some((fn_kind, span)) = &self.diag_metadata.current_function {
1196+
if let Some((fn_kind, fn_span)) = &self.diag_metadata.current_function {
11901197
// The current function has a `self` parameter, but we were unable to resolve
11911198
// a reference to `self`. This can only happen if the `self` identifier we
1192-
// are resolving came from a different hygiene context.
1199+
// are resolving came from a different hygiene context or a variable binding.
1200+
// But variable binding error is returned early above.
11931201
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}"));
1202+
err.span_label(*fn_span, format!("this function has {self_from_macro}"));
11951203
} else {
11961204
let doesnt = if is_assoc_fn {
11971205
let (span, sugg) = fn_kind
@@ -1204,7 +1212,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
12041212
// This avoids placing the suggestion into the visibility specifier.
12051213
let span = fn_kind
12061214
.ident()
1207-
.map_or(*span, |ident| span.with_lo(ident.span.hi()));
1215+
.map_or(*fn_span, |ident| fn_span.with_lo(ident.span.hi()));
12081216
(
12091217
self.r
12101218
.tcx

tests/ui/error-codes/E0424.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ LL | fn qux(&self) {
4040
error[E0424]: expected unit struct, unit variant or constant, found module `self`
4141
--> $DIR/E0424.rs:20:9
4242
|
43-
LL | fn main () {
44-
| ---- this function can't have a `self` parameter
4543
LL | let self = "self";
4644
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
4745

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait T {
2+
fn f(self);
3+
}
4+
impl T for () {
5+
fn f(self) {
6+
let self = (); //~ ERROR expected unit struct, unit variant or constant, found local variable `self`
7+
}
8+
}
9+
10+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0424]: expected unit struct, unit variant or constant, found local variable `self`
2+
--> $DIR/false-self-in-macro-issue-143134.rs:6:13
3+
|
4+
LL | let self = ();
5+
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0424`.

0 commit comments

Comments
 (0)