Skip to content

Commit

Permalink
Disallow duplicate bindings of struct fields
Browse files Browse the repository at this point in the history
Closes #9725
  • Loading branch information
alexcrichton committed Dec 2, 2013
1 parent fc4540d commit 80055de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/librustc/middle/typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,24 @@ pub fn check_struct_pat_fields(pcx: &pat_ctxt,
etc: bool) {
let tcx = pcx.fcx.ccx.tcx;
// Index the class fields.
// Index the class fields. The second argument in the tuple is whether the
// field has been bound yet or not.
let mut field_map = HashMap::new();
for (i, class_field) in class_fields.iter().enumerate() {
field_map.insert(class_field.name, i);
field_map.insert(class_field.name, (i, false));
}
// Typecheck each field.
let mut found_fields = HashSet::new();
for field in fields.iter() {
match field_map.find(&field.ident.name) {
Some(&index) => {
match field_map.find_mut(&field.ident.name) {
Some(&(_, true)) => {
tcx.sess.span_err(span,
format!("field `{}` bound twice in pattern",
tcx.sess.str_of(field.ident)));
}
Some(&(index, ref mut used)) => {
*used = true;
let class_field = class_fields[index];
let field_type = ty::lookup_field_type(tcx,
class_id,
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/issue-9725.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

struct A { foo: int }

fn main() {
let A { foo, foo } = A { foo: 3 }; //~ ERROR: field `foo` bound twice
}

5 comments on commit 80055de

@bors
Copy link
Contributor

@bors bors commented on 80055de Dec 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at alexcrichton@80055de

@bors
Copy link
Contributor

@bors bors commented on 80055de Dec 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/issue-9725 = 80055de into auto

@bors
Copy link
Contributor

@bors bors commented on 80055de Dec 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/issue-9725 = 80055de merged ok, testing candidate = 63c9522

@bors
Copy link
Contributor

@bors bors commented on 80055de Dec 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 80055de Dec 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 63c9522

Please sign in to comment.