Skip to content

Commit 4e03c51

Browse files
committed
hir_analysis: enums return None in find_field
Unnamed union fields with enums are checked for, but if `find_field` causes an ICE then the compiler won't get to that point. Signed-off-by: David Wood <[email protected]>
1 parent 7606c13 commit 4e03c51

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,12 @@ fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
791791
}
792792

793793
fn find_field(tcx: TyCtxt<'_>, (def_id, ident): (DefId, Ident)) -> Option<FieldIdx> {
794-
tcx.adt_def(def_id).non_enum_variant().fields.iter_enumerated().find_map(|(idx, field)| {
794+
let adt = tcx.adt_def(def_id);
795+
if adt.is_enum() {
796+
return None;
797+
}
798+
799+
adt.non_enum_variant().fields.iter_enumerated().find_map(|(idx, field)| {
795800
if field.is_unnamed() {
796801
let field_ty = tcx.type_of(field.did).instantiate_identity();
797802
let adt_def = field_ty.ty_adt_def().expect("expect Adt for unnamed field");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type NodeId = u32;
2+
struct Type<'a>(std::marker::PhantomData::<&'a ()>);
3+
4+
type Ast<'ast> = &'ast AstStructure<'ast>;
5+
6+
struct AstStructure<'ast> {
7+
//~^ ERROR struct with unnamed fields must have `#[repr(C)]` representation
8+
id: NodeId,
9+
_: AstKind<'ast>
10+
//~^ ERROR unnamed fields are not yet fully implemented [E0658]
11+
//~^^ ERROR unnamed fields can only have struct or union types
12+
}
13+
14+
enum AstKind<'ast> {
15+
ExprInt,
16+
ExprLambda(Ast<'ast>),
17+
}
18+
19+
fn compute_types<'tcx,'ast>(ast: Ast<'ast>) -> Type<'tcx>
20+
{
21+
match ast.kind {}
22+
//~^ ERROR no field `kind` on type `&'ast AstStructure<'ast>` [E0609]
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0658]: unnamed fields are not yet fully implemented
2+
--> $DIR/unnamed-enum-field-issue-121757.rs:9:5
3+
|
4+
LL | _: AstKind<'ast>
5+
| ^
6+
|
7+
= note: see issue #49804 <https://github.com/rust-lang/rust/issues/49804> for more information
8+
= help: add `#![feature(unnamed_fields)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error: struct with unnamed fields must have `#[repr(C)]` representation
12+
--> $DIR/unnamed-enum-field-issue-121757.rs:6:1
13+
|
14+
LL | struct AstStructure<'ast> {
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ struct `AstStructure` defined here
16+
|
17+
note: unnamed field defined here
18+
--> $DIR/unnamed-enum-field-issue-121757.rs:9:5
19+
|
20+
LL | _: AstKind<'ast>
21+
| ^^^^^^^^^^^^^^^^
22+
help: add `#[repr(C)]` to this struct
23+
|
24+
LL + #[repr(C)]
25+
LL | struct AstStructure<'ast> {
26+
|
27+
28+
error: unnamed fields can only have struct or union types
29+
--> $DIR/unnamed-enum-field-issue-121757.rs:9:5
30+
|
31+
LL | _: AstKind<'ast>
32+
| ^^^^^^^^^^^^^^^^
33+
34+
error[E0609]: no field `kind` on type `&'ast AstStructure<'ast>`
35+
--> $DIR/unnamed-enum-field-issue-121757.rs:21:15
36+
|
37+
LL | match ast.kind {}
38+
| ^^^^ unknown field
39+
|
40+
= note: available fields are: `id`, `_`
41+
42+
error: aborting due to 4 previous errors
43+
44+
Some errors have detailed explanations: E0609, E0658.
45+
For more information about an error, try `rustc --explain E0609`.

0 commit comments

Comments
 (0)