Skip to content

Commit

Permalink
check_expr_struct_fields: taint context with errors if struct definit…
Browse files Browse the repository at this point in the history
…ion is malformed
  • Loading branch information
olafes committed Jun 3, 2024
1 parent 7c52d2d commit e2c12b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
12 changes: 11 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let mut seen_fields = FxHashMap::default();

let mut error_happened = false;
let mut error_happened = if variant.fields.len() > remaining_fields.len() {
// Some field is defined more than once. Make sure we don't try to
// instantiate this struct in static/const context.
let guar = self.dcx().span_delayed_bug(expr.span, "struct fields have non-unique names");
self.set_tainted_by_errors(guar);

true
} else {
false
};


// Type-check each field.
for (idx, field) in hir_fields.iter().enumerate() {
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/static/issue-125842.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Do not try to evaluate static initalizers that reference
// ill-defined types. This used to be an ICE.
// See issues #125842 and #124464.
struct Struct {
field: Option<u8>,
field: u8,
//~^ ERROR field `field` is already declared
}

static STATIC_A: Struct = Struct {
field: 1
};

static STATIC_B: Struct = {
let field = 1;
Struct {
field,
}
};

fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/static/issue-125842.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0124]: field `field` is already declared
--> $DIR/issue-125842.rs:6:5
|
LL | field: Option<u8>,
| ----------------- `field` first declared here
LL | field: u8,
| ^^^^^^^^^ field already declared

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0124`.

0 comments on commit e2c12b4

Please sign in to comment.