From 25ab022f2951318e8ffdec2d8566c3a7d164e00d Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sun, 29 Jun 2025 15:37:28 +0800 Subject: [PATCH 1/3] move `tests/ui/resolve/suggest*` to `tests/ui/resolve/suggestions/` Signed-off-by: xizheyin --- .../auxiliary/suggest-constructor-cycle-error.rs | 0 tests/ui/resolve/{ => suggestions}/suggest-builder-fn.rs | 0 tests/ui/resolve/{ => suggestions}/suggest-builder-fn.stderr | 0 .../resolve/{ => suggestions}/suggest-constructor-cycle-error.rs | 0 .../{ => suggestions}/suggest-constructor-cycle-error.stderr | 0 .../suggest-import-without-clobbering-attrs.fixed | 0 .../{ => suggestions}/suggest-import-without-clobbering-attrs.rs | 0 .../suggest-import-without-clobbering-attrs.stderr | 0 .../ui/resolve/{ => suggestions}/suggest-path-for-tuple-struct.rs | 0 .../{ => suggestions}/suggest-path-for-tuple-struct.stderr | 0 .../{ => suggestions}/suggest-path-instead-of-mod-dot-item.rs | 0 .../{ => suggestions}/suggest-path-instead-of-mod-dot-item.stderr | 0 12 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/resolve/{ => suggestions}/auxiliary/suggest-constructor-cycle-error.rs (100%) rename tests/ui/resolve/{ => suggestions}/suggest-builder-fn.rs (100%) rename tests/ui/resolve/{ => suggestions}/suggest-builder-fn.stderr (100%) rename tests/ui/resolve/{ => suggestions}/suggest-constructor-cycle-error.rs (100%) rename tests/ui/resolve/{ => suggestions}/suggest-constructor-cycle-error.stderr (100%) rename tests/ui/resolve/{ => suggestions}/suggest-import-without-clobbering-attrs.fixed (100%) rename tests/ui/resolve/{ => suggestions}/suggest-import-without-clobbering-attrs.rs (100%) rename tests/ui/resolve/{ => suggestions}/suggest-import-without-clobbering-attrs.stderr (100%) rename tests/ui/resolve/{ => suggestions}/suggest-path-for-tuple-struct.rs (100%) rename tests/ui/resolve/{ => suggestions}/suggest-path-for-tuple-struct.stderr (100%) rename tests/ui/resolve/{ => suggestions}/suggest-path-instead-of-mod-dot-item.rs (100%) rename tests/ui/resolve/{ => suggestions}/suggest-path-instead-of-mod-dot-item.stderr (100%) diff --git a/tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs b/tests/ui/resolve/suggestions/auxiliary/suggest-constructor-cycle-error.rs similarity index 100% rename from tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs rename to tests/ui/resolve/suggestions/auxiliary/suggest-constructor-cycle-error.rs diff --git a/tests/ui/resolve/suggest-builder-fn.rs b/tests/ui/resolve/suggestions/suggest-builder-fn.rs similarity index 100% rename from tests/ui/resolve/suggest-builder-fn.rs rename to tests/ui/resolve/suggestions/suggest-builder-fn.rs diff --git a/tests/ui/resolve/suggest-builder-fn.stderr b/tests/ui/resolve/suggestions/suggest-builder-fn.stderr similarity index 100% rename from tests/ui/resolve/suggest-builder-fn.stderr rename to tests/ui/resolve/suggestions/suggest-builder-fn.stderr diff --git a/tests/ui/resolve/suggest-constructor-cycle-error.rs b/tests/ui/resolve/suggestions/suggest-constructor-cycle-error.rs similarity index 100% rename from tests/ui/resolve/suggest-constructor-cycle-error.rs rename to tests/ui/resolve/suggestions/suggest-constructor-cycle-error.rs diff --git a/tests/ui/resolve/suggest-constructor-cycle-error.stderr b/tests/ui/resolve/suggestions/suggest-constructor-cycle-error.stderr similarity index 100% rename from tests/ui/resolve/suggest-constructor-cycle-error.stderr rename to tests/ui/resolve/suggestions/suggest-constructor-cycle-error.stderr diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed b/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.fixed similarity index 100% rename from tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed rename to tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.fixed diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs b/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.rs similarity index 100% rename from tests/ui/resolve/suggest-import-without-clobbering-attrs.rs rename to tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.rs diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr b/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.stderr similarity index 100% rename from tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr rename to tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.stderr diff --git a/tests/ui/resolve/suggest-path-for-tuple-struct.rs b/tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.rs similarity index 100% rename from tests/ui/resolve/suggest-path-for-tuple-struct.rs rename to tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.rs diff --git a/tests/ui/resolve/suggest-path-for-tuple-struct.stderr b/tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.stderr similarity index 100% rename from tests/ui/resolve/suggest-path-for-tuple-struct.stderr rename to tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.stderr diff --git a/tests/ui/resolve/suggest-path-instead-of-mod-dot-item.rs b/tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.rs similarity index 100% rename from tests/ui/resolve/suggest-path-instead-of-mod-dot-item.rs rename to tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.rs diff --git a/tests/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr b/tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.stderr similarity index 100% rename from tests/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr rename to tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.stderr From 000f038e9999ebdbaf3167c42dadc3db872a5d3f Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sun, 29 Jun 2025 15:38:09 +0800 Subject: [PATCH 2/3] Add ui test resolve/false-self-in-macro-issue-143134.rs Signed-off-by: xizheyin --- tests/ui/resolve/false-self-in-macro-issue-143134.rs | 10 ++++++++++ .../resolve/false-self-in-macro-issue-143134.stderr | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/ui/resolve/false-self-in-macro-issue-143134.rs create mode 100644 tests/ui/resolve/false-self-in-macro-issue-143134.stderr diff --git a/tests/ui/resolve/false-self-in-macro-issue-143134.rs b/tests/ui/resolve/false-self-in-macro-issue-143134.rs new file mode 100644 index 0000000000000..0983b8b3dc3c4 --- /dev/null +++ b/tests/ui/resolve/false-self-in-macro-issue-143134.rs @@ -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() {} diff --git a/tests/ui/resolve/false-self-in-macro-issue-143134.stderr b/tests/ui/resolve/false-self-in-macro-issue-143134.stderr new file mode 100644 index 0000000000000..43e77e183ae7a --- /dev/null +++ b/tests/ui/resolve/false-self-in-macro-issue-143134.stderr @@ -0,0 +1,12 @@ +error[E0424]: expected unit struct, unit variant or constant, found local variable `self` + --> $DIR/false-self-in-macro-issue-143134.rs:6:13 + | +LL | / fn f(self) { +LL | | let self = (); + | | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed +LL | | } + | |_____- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0424`. From 236b392904d234d9ec2968c55ae20132405a555d Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sun, 29 Jun 2025 16:10:52 +0800 Subject: [PATCH 3/3] Return early when `self` resolve failure because of `let self = ...` Signed-off-by: xizheyin --- compiler/rustc_resolve/src/late/diagnostics.rs | 16 ++++++++++++---- tests/ui/error-codes/E0424.stderr | 2 -- .../false-self-in-macro-issue-143134.stderr | 7 ++----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index e7b8c988cd4af..0b78835326fc3 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -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; + } + 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 { // 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 @@ -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 diff --git a/tests/ui/error-codes/E0424.stderr b/tests/ui/error-codes/E0424.stderr index d02da3e4ecb8a..831a070bf6cc6 100644 --- a/tests/ui/error-codes/E0424.stderr +++ b/tests/ui/error-codes/E0424.stderr @@ -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 LL | let self = "self"; | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed diff --git a/tests/ui/resolve/false-self-in-macro-issue-143134.stderr b/tests/ui/resolve/false-self-in-macro-issue-143134.stderr index 43e77e183ae7a..48c979575ea8c 100644 --- a/tests/ui/resolve/false-self-in-macro-issue-143134.stderr +++ b/tests/ui/resolve/false-self-in-macro-issue-143134.stderr @@ -1,11 +1,8 @@ error[E0424]: expected unit struct, unit variant or constant, found local variable `self` --> $DIR/false-self-in-macro-issue-143134.rs:6:13 | -LL | / fn f(self) { -LL | | let self = (); - | | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed -LL | | } - | |_____- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters +LL | let self = (); + | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed error: aborting due to 1 previous error