Skip to content

Commit 5f04f98

Browse files
committed
Ensure that we don't try to access fields on a non-struct pattern type in diagnostic
Fix #135209.
1 parent ad211ce commit 5f04f98

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,9 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11301130
let None = following_seg else { return };
11311131
for rib in self.ribs[ValueNS].iter().rev() {
11321132
for (def_id, spans) in &rib.patterns_with_skipped_bindings {
1133-
if let Some(fields) = self.r.field_idents(*def_id) {
1133+
if let DefKind::Struct = self.r.tcx.def_kind(*def_id)
1134+
&& let Some(fields) = self.r.field_idents(*def_id)
1135+
{
11341136
for field in fields {
11351137
if field.name == segment.ident.name {
11361138
if spans.iter().all(|(_, had_error)| had_error.is_err()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Regression test for #135209.
2+
// We ensure that we don't try to access fields on a non-struct pattern type.
3+
fn main() {
4+
if let Iterator::Item { .. } = 1 { //~ ERROR E0223
5+
x //~ ERROR E0425
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0425]: cannot find value `x` in this scope
2+
--> $DIR/struct-pattern-on-non-struct-resolve-error.rs:5:9
3+
|
4+
LL | x
5+
| ^ not found in this scope
6+
7+
error[E0223]: ambiguous associated type
8+
--> $DIR/struct-pattern-on-non-struct-resolve-error.rs:4:12
9+
|
10+
LL | if let Iterator::Item { .. } = 1 {
11+
| ^^^^^^^^^^^^^^
12+
|
13+
help: use fully-qualified syntax
14+
|
15+
LL | if let <Ancestors<'_> as Iterator>::Item { .. } = 1 {
16+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17+
LL | if let <Args as Iterator>::Item { .. } = 1 {
18+
| ~~~~~~~~~~~~~~~~~~~~~~~~
19+
LL | if let <ArgsOs as Iterator>::Item { .. } = 1 {
20+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
21+
LL | if let <CharIndices<'_> as Iterator>::Item { .. } = 1 {
22+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23+
and 71 other candidates
24+
25+
error: aborting due to 2 previous errors
26+
27+
Some errors have detailed explanations: E0223, E0425.
28+
For more information about an error, try `rustc --explain E0223`.

0 commit comments

Comments
 (0)