diff options
| author | Trevor Gross <t.gross35@gmail.com> | 2025-07-08 22:50:26 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-08 22:50:26 -0500 |
| commit | a71dbcd6790aa447e7c446def25ad2aa8b848c62 (patch) | |
| tree | e67dc912609f2663ddb89237405f4e02642806cb /compiler/rustc_resolve/src/late/diagnostics.rs | |
| parent | d069a7cf1443a5095d947ad49642c750fad03a5e (diff) | |
| parent | 236b392904d234d9ec2968c55ae20132405a555d (diff) | |
| download | rust-a71dbcd6790aa447e7c446def25ad2aa8b848c62.tar.gz rust-a71dbcd6790aa447e7c446def25ad2aa8b848c62.zip | |
Rollup merge of #143177 - xizheyin:143134, r=lcnr
Remove false label when `self` resolve failure does not relate to macro Fixes rust-lang/rust#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
Diffstat (limited to 'compiler/rustc_resolve/src/late/diagnostics.rs')
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index fa04c8bc604..e015eb7a636 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 |
