Skip to content

Commit 0bd67f6

Browse files
committed
auto merge of #7443 : yjh0502/rust/fix_field_dup, r=huonw
Check if there is duplicated field names in struct.
2 parents d324014 + 7bcde87 commit 0bd67f6

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/librustc/middle/resolve.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3846,6 +3846,27 @@ impl Resolver {
38463846
generics: &Generics,
38473847
fields: &[@struct_field],
38483848
visitor: ResolveVisitor) {
3849+
let mut ident_map = HashMap::new::<ast::ident, @struct_field>();
3850+
for fields.iter().advance |&field| {
3851+
match field.node.kind {
3852+
named_field(ident, _) => {
3853+
match ident_map.find(&ident) {
3854+
Some(&prev_field) => {
3855+
let ident_str = self.session.str_of(ident);
3856+
self.session.span_err(field.span,
3857+
fmt!("field `%s` is already declared", ident_str));
3858+
self.session.span_note(prev_field.span,
3859+
"Previously declared here");
3860+
},
3861+
None => {
3862+
ident_map.insert(ident, field);
3863+
}
3864+
}
3865+
}
3866+
_ => ()
3867+
}
3868+
}
3869+
38493870
// If applicable, create a rib for the type parameters.
38503871
do self.with_type_parameter_rib(HasTypeParameters
38513872
(generics, id, 0,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 BuildData {
12+
foo: int,
13+
foo: int, //~ ERROR field `foo` is already declared
14+
}
15+
16+
fn main() {
17+
}

0 commit comments

Comments
 (0)