Skip to content

Commit 743c417

Browse files
committed
check_expr_struct_fields: taint context with errors if struct definition is malformed
1 parent c1dba09 commit 743c417

6 files changed

+68
-13
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16711671

16721672
let mut error_happened = false;
16731673

1674+
if variant.fields.len() != remaining_fields.len() {
1675+
// Some field is defined more than once. Make sure we don't try to
1676+
// instantiate this struct in static/const context.
1677+
let guar =
1678+
self.dcx().span_delayed_bug(expr.span, "struct fields have non-unique names");
1679+
self.set_tainted_by_errors(guar);
1680+
error_happened = true;
1681+
}
1682+
16741683
// Type-check each field.
16751684
for (idx, field) in hir_fields.iter().enumerate() {
16761685
let ident = tcx.adjust_ident(field.ident, variant.def_id);

tests/crashes/124552.rs

-12
This file was deleted.

tests/crashes/124464.rs tests/ui/static/duplicated-fields-issue-124464.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
//@ known-bug: rust-lang/rust #124464
1+
// Don't const eval fields with ambiguous layout.
2+
// See issues #125842 and #124464.
3+
24
enum TestOption<T> {
35
TestSome(T),
46
TestSome(T),
7+
//~^ ERROR the name `TestSome` is defined multiple times
58
}
69

710
pub struct Request {
811
bar: TestOption<u64>,
912
bar: u8,
13+
//~^ ERROR field `bar` is already declared
1014
}
1115

1216
fn default_instance() -> &'static Request {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0428]: the name `TestSome` is defined multiple times
2+
--> $DIR/duplicated-fields-issue-124464.rs:6:5
3+
|
4+
LL | TestSome(T),
5+
| ----------- previous definition of the type `TestSome` here
6+
LL | TestSome(T),
7+
| ^^^^^^^^^^^ `TestSome` redefined here
8+
|
9+
= note: `TestSome` must be defined only once in the type namespace of this enum
10+
11+
error[E0124]: field `bar` is already declared
12+
--> $DIR/duplicated-fields-issue-124464.rs:12:5
13+
|
14+
LL | bar: TestOption<u64>,
15+
| -------------------- `bar` first declared here
16+
LL | bar: u8,
17+
| ^^^^^^^ field already declared
18+
19+
error: aborting due to 2 previous errors
20+
21+
Some errors have detailed explanations: E0124, E0428.
22+
For more information about an error, try `rustc --explain E0124`.
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() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0124]: field `field` is already declared
2+
--> $DIR/duplicated-fields-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)