From 5f04f98c9aaf04e5f670e04dc12d4665489859fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 7 Jan 2025 22:06:58 +0000 Subject: [PATCH] Ensure that we don't try to access fields on a non-struct pattern type in diagnostic Fix #135209. --- .../rustc_resolve/src/late/diagnostics.rs | 4 ++- ...uct-pattern-on-non-struct-resolve-error.rs | 7 +++++ ...pattern-on-non-struct-resolve-error.stderr | 28 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/ui/pattern/struct-pattern-on-non-struct-resolve-error.rs create mode 100644 tests/ui/pattern/struct-pattern-on-non-struct-resolve-error.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 6ee02e9f47f15..3957a057fdbce 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1130,7 +1130,9 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { let None = following_seg else { return }; for rib in self.ribs[ValueNS].iter().rev() { for (def_id, spans) in &rib.patterns_with_skipped_bindings { - if let Some(fields) = self.r.field_idents(*def_id) { + if let DefKind::Struct = self.r.tcx.def_kind(*def_id) + && let Some(fields) = self.r.field_idents(*def_id) + { for field in fields { if field.name == segment.ident.name { if spans.iter().all(|(_, had_error)| had_error.is_err()) { diff --git a/tests/ui/pattern/struct-pattern-on-non-struct-resolve-error.rs b/tests/ui/pattern/struct-pattern-on-non-struct-resolve-error.rs new file mode 100644 index 0000000000000..c74c77fea60f4 --- /dev/null +++ b/tests/ui/pattern/struct-pattern-on-non-struct-resolve-error.rs @@ -0,0 +1,7 @@ +// Regression test for #135209. +// We ensure that we don't try to access fields on a non-struct pattern type. +fn main() { + if let Iterator::Item { .. } = 1 { //~ ERROR E0223 + x //~ ERROR E0425 + } +} diff --git a/tests/ui/pattern/struct-pattern-on-non-struct-resolve-error.stderr b/tests/ui/pattern/struct-pattern-on-non-struct-resolve-error.stderr new file mode 100644 index 0000000000000..e31b19ab6bcbf --- /dev/null +++ b/tests/ui/pattern/struct-pattern-on-non-struct-resolve-error.stderr @@ -0,0 +1,28 @@ +error[E0425]: cannot find value `x` in this scope + --> $DIR/struct-pattern-on-non-struct-resolve-error.rs:5:9 + | +LL | x + | ^ not found in this scope + +error[E0223]: ambiguous associated type + --> $DIR/struct-pattern-on-non-struct-resolve-error.rs:4:12 + | +LL | if let Iterator::Item { .. } = 1 { + | ^^^^^^^^^^^^^^ + | +help: use fully-qualified syntax + | +LL | if let as Iterator>::Item { .. } = 1 { + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +LL | if let ::Item { .. } = 1 { + | ~~~~~~~~~~~~~~~~~~~~~~~~ +LL | if let ::Item { .. } = 1 { + | ~~~~~~~~~~~~~~~~~~~~~~~~~~ +LL | if let as Iterator>::Item { .. } = 1 { + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + and 71 other candidates + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0223, E0425. +For more information about an error, try `rustc --explain E0223`.