Skip to content

Commit 80055de

Browse files
committed
Disallow duplicate bindings of struct fields
Closes #9725
1 parent fc4540d commit 80055de

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/librustc/middle/typeck/check/_match.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,24 @@ pub fn check_struct_pat_fields(pcx: &pat_ctxt,
294294
etc: bool) {
295295
let tcx = pcx.fcx.ccx.tcx;
296296
297-
// Index the class fields.
297+
// Index the class fields. The second argument in the tuple is whether the
298+
// field has been bound yet or not.
298299
let mut field_map = HashMap::new();
299300
for (i, class_field) in class_fields.iter().enumerate() {
300-
field_map.insert(class_field.name, i);
301+
field_map.insert(class_field.name, (i, false));
301302
}
302303
303304
// Typecheck each field.
304305
let mut found_fields = HashSet::new();
305306
for field in fields.iter() {
306-
match field_map.find(&field.ident.name) {
307-
Some(&index) => {
307+
match field_map.find_mut(&field.ident.name) {
308+
Some(&(_, true)) => {
309+
tcx.sess.span_err(span,
310+
format!("field `{}` bound twice in pattern",
311+
tcx.sess.str_of(field.ident)));
312+
}
313+
Some(&(index, ref mut used)) => {
314+
*used = true;
308315
let class_field = class_fields[index];
309316
let field_type = ty::lookup_field_type(tcx,
310317
class_id,

src/test/compile-fail/issue-9725.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct A { foo: int }
12+
13+
fn main() {
14+
let A { foo, foo } = A { foo: 3 }; //~ ERROR: field `foo` bound twice
15+
}

0 commit comments

Comments
 (0)