Skip to content

Commit e708e55

Browse files
Merge pull request #22326 from MavenRain/fixme-duplicate-field-pat
ide-diagnostics: emit error for duplicate field in record pattern
2 parents 73ca1d4 + af15884 commit e708e55

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

crates/hir-ty/src/infer/pat.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,10 @@ https://doc.rust-lang.org/reference/types.html#trait-objects";
11851185
for (field_idx, field) in fields.iter().enumerate() {
11861186
match used_fields.entry(field.name.clone()) {
11871187
Occupied(_occupied) => {
1188-
// FIXME: Emit an error, field specified twice.
1188+
self.push_diagnostic(InferenceDiagnostic::DuplicateField {
1189+
field: field.pat.into(),
1190+
variant,
1191+
});
11891192
}
11901193
Vacant(vacant) => {
11911194
vacant.insert(field_idx);

crates/ide-diagnostics/src/handlers/duplicate_field.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,45 @@ fn main() {
7878
//^^^^^^ 💡 error: no such field
7979
};
8080
}
81+
"#,
82+
);
83+
}
84+
85+
#[test]
86+
fn duplicate_field_in_struct_pattern() {
87+
check_diagnostics(
88+
r#"
89+
struct S { foo: i32, bar: i32 }
90+
fn f(s: S) {
91+
let S {
92+
foo,
93+
bar,
94+
foo,
95+
//^^^ error: field specified more than once
96+
..
97+
} = s;
98+
let _ = (foo, bar);
99+
}
100+
"#,
101+
);
102+
}
103+
104+
#[test]
105+
fn duplicate_field_in_enum_variant_pattern() {
106+
check_diagnostics(
107+
r#"
108+
enum E { V { foo: i32, bar: i32 } }
109+
fn f(e: E) {
110+
match e {
111+
E::V {
112+
foo,
113+
bar,
114+
foo,
115+
//^^^ error: field specified more than once
116+
..
117+
} => { let _ = (foo, bar); }
118+
}
119+
}
81120
"#,
82121
);
83122
}

0 commit comments

Comments
 (0)