Skip to content

Commit cb4de73

Browse files
committed
Fix checking of duplicate and missing struct field initializers. Closes #3486. Closes #3892
1 parent b2462aa commit cb4de73

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/rustc/middle/typeck/check.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,8 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
14571457
let tcx = fcx.ccx.tcx;
14581458
let mut bot = false;
14591459
1460+
error!("%? %?", ast_fields.len(), field_types.len());
1461+
14601462
let class_field_map = HashMap();
14611463
let mut fields_found = 0;
14621464
for field_types.each |field| {
@@ -1470,7 +1472,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
14701472
None => {
14711473
tcx.sess.span_err(
14721474
field.span,
1473-
fmt!("structure has no field named field named `%s`",
1475+
fmt!("structure has no field named `%s`",
14741476
tcx.sess.str_of(field.node.ident)));
14751477
}
14761478
Some((_, true)) => {
@@ -1486,18 +1488,20 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
14861488
bot |= check_expr(fcx,
14871489
field.node.expr,
14881490
Some(expected_field_type));
1491+
class_field_map.insert(
1492+
field.node.ident, (field_id, true));
14891493
fields_found += 1;
14901494
}
14911495
}
14921496
}
14931497

14941498
if check_completeness {
14951499
// Make sure the programmer specified all the fields.
1496-
assert fields_found <= ast_fields.len();
1497-
if fields_found < ast_fields.len() {
1500+
assert fields_found <= field_types.len();
1501+
if fields_found < field_types.len() {
14981502
let mut missing_fields = ~[];
1499-
for ast_fields.each |class_field| {
1500-
let name = class_field.node.ident;
1503+
for field_types.each |class_field| {
1504+
let name = class_field.ident;
15011505
let (_, seen) = class_field_map.get(name);
15021506
if !seen {
15031507
missing_fields.push(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct BuildData {
2+
foo: int,
3+
}
4+
5+
fn main() {
6+
let foo = BuildData {
7+
foo: 0,
8+
foo: 0 //~ ERROR field `foo` specified more than once
9+
};
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct BuildData {
2+
foo: int,
3+
bar: ~int
4+
}
5+
6+
fn main() {
7+
let foo = BuildData { //~ ERROR missing field: `bar`
8+
foo: 0
9+
};
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct BuildData {
2+
foo: int,
3+
}
4+
5+
fn main() {
6+
let foo = BuildData {
7+
foo: 0,
8+
bar: 0 //~ ERROR structure has no field named `bar`
9+
};
10+
}

0 commit comments

Comments
 (0)