Skip to content

Commit

Permalink
Fix rust-lang#126344, skip report nonexisting_field error for recover…
Browse files Browse the repository at this point in the history
…ed structs
  • Loading branch information
chenyukang committed Jul 9, 2024
1 parent 2976c59 commit f198542
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
12 changes: 12 additions & 0 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"ban_nonexisting_field: field={:?}, base={:?}, expr={:?}, base_ty={:?}",
ident, base, expr, base_ty
);

for (ty, _) in self.autoderef(expr.span, base_ty) {
if let ty::Adt(def, _) = ty.kind()
&& !def.is_enum()
{
let variant = def.non_enum_variant();
if let Err(reported) = variant.fields_checked() {
return reported;
}
}
}

let mut err = self.no_such_field_err(ident, base_ty, base.hir_id);

match *base_ty.peel_refs().kind() {
Expand Down
42 changes: 42 additions & 0 deletions tests/ui/pattern/struct-parser-recovery-issue-126344.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
struct Wrong {
x: i32; //~ ERROR struct fields are separated by `,`
y: i32,
z: i32,
h: i32,
}

fn oops(w: &Wrong) {
w.x;
}

fn foo(w: &Wrong) {
w.y;
}

fn haha(w: &Wrong) {
w.z;
}

struct WrongWithType {
x: 1, //~ ERROR expected type, found `1`
y: i32,
z: i32,
h: i32,
}

fn oops_type(w: &WrongWithType) {
w.x;
}

fn foo_type(w: &WrongWithType) {
w.y;
}

fn haha_type(w: &WrongWithType) {
w.z;
}

fn main() {
let v = Wrong { x: 1, y: 2, z: 3, h: 4 };
let x = WrongWithType { x: 1, y: 2, z: 3, h: 4 };
}
18 changes: 18 additions & 0 deletions tests/ui/pattern/struct-parser-recovery-issue-126344.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: struct fields are separated by `,`
--> $DIR/struct-parser-recovery-issue-126344.rs:2:11
|
LL | struct Wrong {
| ----- while parsing this struct
LL | x: i32;
| ^ help: replace `;` with `,`

error: expected type, found `1`
--> $DIR/struct-parser-recovery-issue-126344.rs:21:8
|
LL | struct WrongWithType {
| ------------- while parsing this struct
LL | x: 1,
| ^ expected type

error: aborting due to 2 previous errors

0 comments on commit f198542

Please sign in to comment.