Skip to content

Commit e2c12b4

Browse files
committed
check_expr_struct_fields: taint context with errors if struct definition is malformed
1 parent 7c52d2d commit e2c12b4

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16721672

16731673
let mut seen_fields = FxHashMap::default();
16741674

1675-
let mut error_happened = false;
1675+
let mut error_happened = if variant.fields.len() > remaining_fields.len() {
1676+
// Some field is defined more than once. Make sure we don't try to
1677+
// instantiate this struct in static/const context.
1678+
let guar = self.dcx().span_delayed_bug(expr.span, "struct fields have non-unique names");
1679+
self.set_tainted_by_errors(guar);
1680+
1681+
true
1682+
} else {
1683+
false
1684+
};
1685+
16761686

16771687
// Type-check each field.
16781688
for (idx, field) in hir_fields.iter().enumerate() {

tests/ui/static/issue-125842.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Do not try to evaluate static initalizers that reference
2+
// ill-defined types. This used to be an ICE.
3+
// See issues #125842 and #124464.
4+
struct Struct {
5+
field: Option<u8>,
6+
field: u8,
7+
//~^ ERROR field `field` is already declared
8+
}
9+
10+
static STATIC_A: Struct = Struct {
11+
field: 1
12+
};
13+
14+
static STATIC_B: Struct = {
15+
let field = 1;
16+
Struct {
17+
field,
18+
}
19+
};
20+
21+
fn main() {}

tests/ui/static/issue-125842.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0124]: field `field` is already declared
2+
--> $DIR/issue-125842.rs:6:5
3+
|
4+
LL | field: Option<u8>,
5+
| ----------------- `field` first declared here
6+
LL | field: u8,
7+
| ^^^^^^^^^ field already declared
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0124`.

0 commit comments

Comments
 (0)