Skip to content

Remove false label when self resolve failure does not relate to macro #143177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,15 +1183,23 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
_ => "`self` value is a keyword only available in methods with a `self` parameter",
},
);

// using `let self` is wrong even if we're not in an associated method or if we're in a macro expansion.
// So, we should return early if we're in a pattern, see issue #143134.
if matches!(source, PathSource::Pat) {
return true;
}

Comment on lines +1186 to +1192
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I return early when the source is Pat.

let is_assoc_fn = self.self_type_is_available();
let self_from_macro = "a `self` parameter, but a macro invocation can only \
access identifiers it receives from parameters";
if let Some((fn_kind, span)) = &self.diag_metadata.current_function {
if let Some((fn_kind, fn_span)) = &self.diag_metadata.current_function {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep the fn_span change because span shadow make it confused.

// The current function has a `self` parameter, but we were unable to resolve
// a reference to `self`. This can only happen if the `self` identifier we
// are resolving came from a different hygiene context.
// are resolving came from a different hygiene context or a variable binding.
// But variable binding error is returned early above.
if fn_kind.decl().inputs.get(0).is_some_and(|p| p.is_self()) {
err.span_label(*span, format!("this function has {self_from_macro}"));
err.span_label(*fn_span, format!("this function has {self_from_macro}"));
} else {
let doesnt = if is_assoc_fn {
let (span, sugg) = fn_kind
Expand All @@ -1204,7 +1212,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
// This avoids placing the suggestion into the visibility specifier.
let span = fn_kind
.ident()
.map_or(*span, |ident| span.with_lo(ident.span.hi()));
.map_or(*fn_span, |ident| fn_span.with_lo(ident.span.hi()));
(
self.r
.tcx
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/error-codes/E0424.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ LL | fn qux(&self) {
error[E0424]: expected unit struct, unit variant or constant, found module `self`
--> $DIR/E0424.rs:20:9
|
LL | fn main () {
| ---- this function can't have a `self` parameter
Comment on lines -43 to -44
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It indeed bring some benefits. :)

LL | let self = "self";
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/resolve/false-self-in-macro-issue-143134.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
trait T {
fn f(self);
}
impl T for () {
fn f(self) {
let self = (); //~ ERROR expected unit struct, unit variant or constant, found local variable `self`
}
}

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/resolve/false-self-in-macro-issue-143134.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0424]: expected unit struct, unit variant or constant, found local variable `self`
--> $DIR/false-self-in-macro-issue-143134.rs:6:13
|
LL | let self = ();
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0424`.
Loading