Skip to content

Commit a7635b8

Browse files
committed
Don't ICE on anonymous struct in enum variant
1 parent 1bb3a9f commit a7635b8

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,13 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
10251025

10261026
let is_anonymous = item.ident.name == kw::Empty;
10271027
let repr = if is_anonymous {
1028-
tcx.adt_def(tcx.local_parent(def_id)).repr()
1028+
let parent = tcx.local_parent(def_id);
1029+
if matches!(tcx.hir_node_by_def_id(parent), Node::Item(_)) {
1030+
tcx.adt_def(tcx.local_parent(def_id)).repr()
1031+
} else {
1032+
tcx.dcx().span_delayed_bug(item.span, "anonymous field inside non struct/union");
1033+
tcx.repr_options_of_def(def_id.to_def_id())
1034+
}
10291035
} else {
10301036
tcx.repr_options_of_def(def_id.to_def_id())
10311037
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![crate_type = "lib"]
2+
#![feature(unnamed_fields)]
3+
#![allow(unused, incomplete_features)]
4+
5+
enum K {
6+
M {
7+
_ : struct { field: u8 },
8+
//~^ error: unnamed fields are not allowed outside of structs or unions
9+
//~| error: anonymous structs are not allowed outside of unnamed struct or union fields
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: unnamed fields are not allowed outside of structs or unions
2+
--> $DIR/anon-struct-in-enum-issue-121446.rs:7:9
3+
|
4+
LL | _ : struct { field: u8 },
5+
| -^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| unnamed field declared here
8+
9+
error: anonymous structs are not allowed outside of unnamed struct or union fields
10+
--> $DIR/anon-struct-in-enum-issue-121446.rs:7:13
11+
|
12+
LL | _ : struct { field: u8 },
13+
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)