Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2203,14 +2203,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
}
rustc_hir::StructTailExpr::NoneWithError(ErrorGuaranteed { .. }) => {
rustc_hir::StructTailExpr::NoneWithError(guaranteed) => {
// If parsing the struct recovered from a syntax error, do not report missing
// fields. This prevents spurious errors when a field is intended to be present
// but a preceding syntax error caused it not to be parsed. For example, if a
// struct type `StructName` has fields `foo` and `bar`, then
// StructName { foo(), bar: 2 }
// will not successfully parse a field `foo`, but we will not mention that,
// since the syntax error has already been reported.

// Signal that type checking has failed, even though we haven’t emitted a diagnostic
// about it ourselves.
self.infcx.set_tainted_by_errors(guaranteed);
}
rustc_hir::StructTailExpr::None => {
if adt_kind != AdtKind::Union
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/structs/syntax-error-not-missing-field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ fn enum_expr_missing_separator() {
let e = Bar::Baz { a: make_a() b: 2 }; //~ ERROR found `b`
}

// Should error but not then ICE due to lack of type checking
fn regression_test_for_issue_153388_a() {
struct TheStruct;
struct MyStruct {
value: i32,
s: TheStruct,
}
static A: MyStruct = MyStruct { ,s: TheStruct }; //~ ERROR expected identifier, found `,`
}

fn regression_test_for_issue_153388_b() {
struct MyStruct {
value: i32,
}
static A: MyStruct = MyStruct {,}; //~ ERROR expected identifier, found `,`
}

fn main() {}
24 changes: 23 additions & 1 deletion tests/ui/structs/syntax-error-not-missing-field.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,27 @@ LL | let e = Bar::Baz { a: make_a() b: 2 };
| | help: try adding a comma: `,`
| while parsing this struct

error: aborting due to 9 previous errors
error: expected identifier, found `,`
--> $DIR/syntax-error-not-missing-field.rs:57:37
|
LL | static A: MyStruct = MyStruct { ,s: TheStruct };
| -------- ^ expected identifier
| |
| while parsing this struct
|
help: remove this comma
|
LL - static A: MyStruct = MyStruct { ,s: TheStruct };
LL + static A: MyStruct = MyStruct { s: TheStruct };
|

error: expected identifier, found `,`
--> $DIR/syntax-error-not-missing-field.rs:64:36
|
LL | static A: MyStruct = MyStruct {,};
| -------- ^ expected identifier
| |
| while parsing this struct

error: aborting due to 11 previous errors

Loading