Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d44f021

Browse files
committedJan 8, 2025
Add check for missing fields in enum variant pattern
1 parent 5f04f98 commit d44f021

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed
 

‎compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ 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 DefKind::Struct = self.r.tcx.def_kind(*def_id)
1133+
if let DefKind::Struct | DefKind::Variant = self.r.tcx.def_kind(*def_id)
11341134
&& let Some(fields) = self.r.field_idents(*def_id)
11351135
{
11361136
for field in fields {

‎tests/ui/pattern/struct-pattern-with-missing-fields-resolve-error.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ struct Website {
33
title: Option<String>,
44
}
55

6+
enum Foo {
7+
Bar { a: i32 },
8+
}
9+
610
fn main() {
711
let website = Website {
812
url: "http://www.example.com".into(),
@@ -18,4 +22,9 @@ fn main() {
1822
println!("[{}]({})", title, url); //~ ERROR cannot find value `title` in this scope
1923
//~^ NOTE not found in this scope
2024
}
25+
26+
let x = Foo::Bar { a: 1 };
27+
if let Foo::Bar { .. } = x { //~ NOTE this pattern
28+
println!("{a}"); //~ ERROR cannot find value `a` in this scope
29+
}
2130
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
error: expected `,`
2-
--> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:12:31
2+
--> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:16:31
33
|
44
LL | if let Website { url, Some(title) } = website {
55
| ------- ^
66
| |
77
| while parsing the fields for this pattern
88

99
error[E0425]: cannot find value `title` in this scope
10-
--> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:18:30
10+
--> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:22:30
1111
|
1212
LL | if let Website { url, .. } = website {
1313
| ------------------- this pattern doesn't include `title`, which is available in `Website`
1414
LL | println!("[{}]({})", title, url);
1515
| ^^^^^ not found in this scope
1616

17-
error: aborting due to 2 previous errors
17+
error[E0425]: cannot find value `a` in this scope
18+
--> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:28:20
19+
|
20+
LL | if let Foo::Bar { .. } = x {
21+
| --------------- this pattern doesn't include `a`, which is available in `Bar`
22+
LL | println!("{a}");
23+
| ^ help: a local variable with a similar name exists: `x`
24+
25+
error: aborting due to 3 previous errors
1826

1927
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)
Failed to load comments.