Skip to content

Commit 1df9b8a

Browse files
committed
Create node Item and AdtDef for anonymous adts
1 parent b2da370 commit 1df9b8a

File tree

51 files changed

+195
-328
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+195
-328
lines changed

compiler/rustc_ast/src/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2093,9 +2093,9 @@ pub enum TyKind {
20932093
/// A tuple (`(A, B, C, D,...)`).
20942094
Tup(ThinVec<P<Ty>>),
20952095
/// An anonymous struct type i.e. `struct { foo: Type }`
2096-
AnonStruct(ThinVec<FieldDef>),
2096+
AnonStruct(NodeId, ThinVec<FieldDef>),
20972097
/// An anonymous union type i.e. `union { bar: Type }`
2098-
AnonUnion(ThinVec<FieldDef>),
2098+
AnonUnion(NodeId, ThinVec<FieldDef>),
20992099
/// A path (`module::module::...::Type`), optionally
21002100
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
21012101
///

compiler/rustc_ast/src/mut_visit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
509509
visit_vec(bounds, |bound| vis.visit_param_bound(bound));
510510
}
511511
TyKind::MacCall(mac) => vis.visit_mac_call(mac),
512-
TyKind::AnonStruct(fields) | TyKind::AnonUnion(fields) => {
512+
TyKind::AnonStruct(id, fields) | TyKind::AnonUnion(id, fields) => {
513+
vis.visit_id(id);
513514
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
514515
}
515516
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
438438
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
439439
TyKind::MacCall(mac) => visitor.visit_mac_call(mac),
440440
TyKind::Never | TyKind::CVarArgs => {}
441-
TyKind::AnonStruct(ref fields, ..) | TyKind::AnonUnion(ref fields, ..) => {
441+
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {
442442
walk_list!(visitor, visit_field_def, fields)
443443
}
444444
}

compiler/rustc_ast_lowering/src/item.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
708708
}
709709
}
710710

711-
pub(super) fn lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'hir> {
711+
pub(super) fn lower_field_def(
712+
&mut self,
713+
(index, f): (usize, &FieldDef),
714+
) -> hir::FieldDef<'hir> {
712715
let ty = if let TyKind::Path(qself, path) = &f.ty.kind {
713716
let t = self.lower_path_ty(
714717
&f.ty,

compiler/rustc_ast_lowering/src/lib.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -1293,15 +1293,39 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12931293
TyKind::Err => {
12941294
hir::TyKind::Err(self.tcx.sess.delay_span_bug(t.span, "TyKind::Err lowered"))
12951295
}
1296-
TyKind::AnonStruct(fields) => {
1297-
hir::TyKind::AnonStruct(
1298-
self.arena.alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_field_def(f))),
1299-
)
1300-
}
1301-
TyKind::AnonUnion(fields) => {
1302-
hir::TyKind::AnonUnion(
1303-
self.arena.alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_field_def(f))),
1304-
)
1296+
TyKind::AnonStruct(def_node_id, fields) | TyKind::AnonUnion(def_node_id, fields) => {
1297+
let def_path_data = match &t.kind {
1298+
TyKind::AnonStruct(..) => DefPathData::AnonStruct,
1299+
_ => DefPathData::AnonUnion,
1300+
};
1301+
let def_id = self.create_def(
1302+
self.current_hir_id_owner.def_id,
1303+
*def_node_id,
1304+
def_path_data,
1305+
t.span,
1306+
);
1307+
debug!(?def_id);
1308+
let owner_id = hir::OwnerId { def_id };
1309+
self.with_hir_id_owner(*def_node_id, |this| {
1310+
let fields = this.arena.alloc_from_iter(
1311+
fields.iter().enumerate().map(|f| this.lower_field_def(f)),
1312+
);
1313+
let span = t.span;
1314+
let variant_data = hir::VariantData::Struct(fields, false);
1315+
let generics = hir::Generics::empty();
1316+
let kind = match &t.kind {
1317+
TyKind::AnonStruct(..) => hir::ItemKind::Struct(variant_data, generics),
1318+
_ => hir::ItemKind::Union(variant_data, generics),
1319+
};
1320+
hir::OwnerNode::Item(this.arena.alloc(hir::Item {
1321+
ident: Ident::empty(),
1322+
owner_id,
1323+
kind,
1324+
span: this.lower_span(span),
1325+
vis_span: this.lower_span(span.shrink_to_lo()),
1326+
}))
1327+
});
1328+
hir::TyKind::AnonAdt(hir::ItemId { owner_id })
13051329
}
13061330
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
13071331
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl<'a> AstValidator<'a> {
219219
}
220220
}
221221
}
222-
TyKind::AnonStruct(ref fields, ..) | TyKind::AnonUnion(ref fields, ..) => {
222+
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {
223223
walk_list!(self, visit_field_def, fields)
224224
}
225225
_ => visit::walk_ty(self, t),

compiler/rustc_ast_pretty/src/pprust/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1053,11 +1053,11 @@ impl<'a> State<'a> {
10531053
}
10541054
self.pclose();
10551055
}
1056-
ast::TyKind::AnonStruct(fields) => {
1056+
ast::TyKind::AnonStruct(_, fields) => {
10571057
self.head("struct");
10581058
self.print_record_struct_body(&fields, ty.span);
10591059
}
1060-
ast::TyKind::AnonUnion(fields) => {
1060+
ast::TyKind::AnonUnion(_, fields) => {
10611061
self.head("union");
10621062
self.print_record_struct_body(&fields, ty.span);
10631063
}

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
338338
enum_type_di_node,
339339
std::iter::once((
340340
variant_index,
341-
Cow::from(enum_adt_def.variant(variant_index).name_ref().as_str()),
341+
Cow::from(enum_adt_def.variant(variant_index).name.as_str()),
342342
)),
343343
);
344344

@@ -399,7 +399,7 @@ fn build_union_fields_for_enum<'ll, 'tcx>(
399399
cx,
400400
enum_type_di_node,
401401
variant_indices.clone().map(|variant_index| {
402-
let variant_name = Cow::from(enum_adt_def.variant(variant_index).name_ref().as_str());
402+
let variant_name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
403403
(variant_index, variant_name)
404404
}),
405405
);

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn build_c_style_enum_di_node<'ll, 'tcx>(
9292
&compute_debuginfo_type_name(cx.tcx, enum_type_and_layout.ty, false),
9393
tag_base_type(cx, enum_type_and_layout),
9494
enum_adt_def.discriminants(cx.tcx).map(|(variant_index, discr)| {
95-
let name = Cow::from(enum_adt_def.variant(variant_index).name_ref().as_str());
95+
let name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
9696
(name, discr.val)
9797
}),
9898
containing_scope,
@@ -263,7 +263,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
263263
enum_type_and_layout.ty,
264264
variant_index,
265265
),
266-
variant_def.name().as_str(),
266+
variant_def.name.as_str(),
267267
// NOTE: We use size and align of enum_type, not from variant_layout:
268268
size_and_align_of(enum_type_and_layout),
269269
Some(enum_type_di_node),

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
8484
.variant_range()
8585
.map(|variant_index| VariantMemberInfo {
8686
variant_index,
87-
variant_name: Cow::from(enum_adt_def.variant(variant_index).name_ref().as_str()),
87+
variant_name: Cow::from(enum_adt_def.variant(variant_index).name.as_str()),
8888
variant_struct_type_di_node: super::build_enum_variant_struct_type_di_node(
8989
cx,
9090
enum_type_and_layout,

compiler/rustc_codegen_llvm/src/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn uncached_llvm_type<'a, 'tcx>(
5151
(layout.ty.kind(), &layout.variants)
5252
{
5353
if def.is_enum() && !def.variants().is_empty() {
54-
write!(&mut name, "::{}", def.variant(index).name()).unwrap();
54+
write!(&mut name, "::{}", def.variant(index).name).unwrap();
5555
}
5656
}
5757
if let (&ty::Generator(_, _, _), &Variants::Single { index }) =

compiler/rustc_const_eval/src/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
686686
new_op: &OpTy<'tcx, M::Provenance>,
687687
) -> InterpResult<'tcx> {
688688
let name = match old_op.layout.ty.kind() {
689-
ty::Adt(adt, _) => PathElem::Variant(adt.variant(variant_id).name()),
689+
ty::Adt(adt, _) => PathElem::Variant(adt.variant(variant_id).name),
690690
// Generators also have variants
691691
ty::Generator(..) => PathElem::GeneratorState(variant_id),
692692
_ => bug!("Unexpected type with variant: {:?}", old_op.layout.ty),

compiler/rustc_hir/src/definitions.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ pub enum DefPathData {
282282
ImplTrait,
283283
/// `impl Trait` generated associated type node.
284284
ImplTraitAssocTy,
285+
/// An anonymous struct type i.e. `struct { foo: Type }`
286+
AnonStruct,
287+
/// An anonymous union type i.e. `union { bar: Type }`
288+
AnonUnion,
285289
}
286290

287291
impl Definitions {
@@ -405,7 +409,7 @@ impl DefPathData {
405409
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
406410

407411
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst
408-
| ImplTrait | ImplTraitAssocTy => None,
412+
| ImplTrait | ImplTraitAssocTy | AnonStruct | AnonUnion => None,
409413
}
410414
}
411415

@@ -424,7 +428,9 @@ impl DefPathData {
424428
ClosureExpr => DefPathDataName::Anon { namespace: sym::closure },
425429
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
426430
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
427-
ImplTrait | ImplTraitAssocTy => DefPathDataName::Anon { namespace: sym::opaque },
431+
ImplTrait | ImplTraitAssocTy | AnonStruct | AnonUnion => {
432+
DefPathDataName::Anon { namespace: sym::opaque }
433+
}
428434
}
429435
}
430436
}

compiler/rustc_hir/src/hir.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2713,10 +2713,8 @@ pub enum TyKind<'hir> {
27132713
Never,
27142714
/// A tuple (`(A, B, C, D, ...)`).
27152715
Tup(&'hir [Ty<'hir>]),
2716-
/// An anonymous struct type i.e. `struct { foo: Type }`
2717-
AnonStruct(&'hir [FieldDef<'hir>]),
2718-
/// An anonymous union type i.e. `union { bar: Type }`
2719-
AnonUnion(&'hir [FieldDef<'hir>]),
2716+
/// An anonymous struct or union type i.e. `struct { foo: Type }` or `union { foo: Type }`
2717+
AnonAdt(ItemId),
27202718
/// A path to a type definition (`module::module::...::Type`), or an
27212719
/// associated type (e.g., `<Vec<T> as Trait>::Type` or `<T>::Target`).
27222720
///

compiler/rustc_hir/src/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,8 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
862862
}
863863
TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
864864
TyKind::Infer | TyKind::Err(_) => {}
865-
TyKind::AnonStruct(fields) | TyKind::AnonUnion(fields) => {
866-
walk_list!(visitor, visit_field_def, fields);
865+
TyKind::AnonAdt(item_id) => {
866+
visitor.visit_nested_item(item_id);
867867
}
868868
}
869869
}

compiler/rustc_hir_analysis/src/astconv/mod.rs

+5-57
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14271427
&adt_def
14281428
.variants()
14291429
.iter()
1430-
.map(|variant| variant.name())
1430+
.map(|variant| variant.name)
14311431
.collect::<Vec<Symbol>>(),
14321432
assoc_ident.name,
14331433
None,
@@ -2422,6 +2422,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24222422
hir::TyKind::Tup(fields) => {
24232423
Ty::new_tup_from_iter(tcx, fields.iter().map(|t| self.ast_ty_to_ty(t)))
24242424
}
2425+
hir::TyKind::AnonAdt(item_id) => {
2426+
let adt_def = crate::collect::adt_def(tcx, item_id.owner_id.def_id);
2427+
Ty::new_adt(tcx, adt_def, tcx.mk_args(&[]))
2428+
}
24252429
hir::TyKind::BareFn(bf) => {
24262430
require_c_abi_if_c_variadic(tcx, bf.decl, bf.abi, ast_ty.span);
24272431

@@ -2524,27 +2528,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
25242528
self.ty_infer(None, ast_ty.span)
25252529
}
25262530
hir::TyKind::Err(guar) => Ty::new_error(tcx, *guar),
2527-
hir::TyKind::AnonStruct(fields,) | hir::TyKind::AnonUnion(fields) => {
2528-
let repr = tcx.repr_options_of_def(ast_ty.hir_id.owner.to_def_id());
2529-
if !repr.c() {
2530-
// tcx.sess.emit_err(todo!());
2531-
}
2532-
let adt_kind = match ast_ty.kind {
2533-
hir::TyKind::AnonStruct(..) => ty::AdtKind::Struct,
2534-
_ => ty::AdtKind::Union,
2535-
};
2536-
let field_def = tcx.hir().expect_field(tcx.hir().parent_id(ast_ty.hir_id));
2537-
let did = field_def.def_id;
2538-
let variants = std::iter::once(convert_variant(
2539-
tcx,
2540-
did,
2541-
fields,
2542-
adt_kind,
2543-
))
2544-
.collect();
2545-
let adt_def = tcx.mk_adt_def(did.to_def_id(), adt_kind, variants, repr);
2546-
Ty::new_adt(tcx, adt_def, tcx.mk_args(&[]))
2547-
}
25482531
};
25492532

25502533
self.record_ty(ast_ty.hir_id, result_ty, ast_ty.span);
@@ -2834,38 +2817,3 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
28342817
Some(r)
28352818
}
28362819
}
2837-
2838-
fn convert_variant(
2839-
tcx: TyCtxt<'_>,
2840-
did: LocalDefId,
2841-
fields: &[hir::FieldDef<'_>],
2842-
adt_kind: ty::AdtKind,
2843-
) -> ty::VariantDef {
2844-
let mut seen_fields: FxHashMap<Ident, Span> = Default::default();
2845-
let fields = fields
2846-
.iter()
2847-
.map(|f| {
2848-
let dup_span = seen_fields.get(&f.ident.normalize_to_macros_2_0()).cloned();
2849-
if let Some(prev_span) = dup_span {
2850-
tcx.sess.emit_err(crate::errors::FieldAlreadyDeclared {
2851-
field_name: f.ident,
2852-
span: f.span,
2853-
prev_span,
2854-
});
2855-
} else {
2856-
seen_fields.insert(f.ident.normalize_to_macros_2_0(), f.span);
2857-
}
2858-
2859-
ty::FieldDef {
2860-
did: f.def_id.to_def_id(),
2861-
name: f.ident.name,
2862-
vis: tcx.visibility(f.def_id),
2863-
}
2864-
})
2865-
.collect();
2866-
ty::VariantDef::new_anon(
2867-
did.to_def_id(),
2868-
fields,
2869-
adt_kind,
2870-
)
2871-
}

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1323,8 +1323,8 @@ fn detect_discriminant_duplicate<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
13231323
idx.as_u32().checked_sub(distance_to_explicit).map(VariantIdx::from_u32)
13241324
{
13251325
let explicit_variant = adt.variant(explicit_idx);
1326-
let ve_ident = var.name();
1327-
let ex_ident = explicit_variant.name();
1326+
let ve_ident = var.name;
1327+
let ex_ident = explicit_variant.name;
13281328
let sp = if distance_to_explicit > 1 { "variants" } else { "variant" };
13291329

13301330
err.span_label(

compiler/rustc_hir_analysis/src/collect.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
776776
}
777777
}
778778

779-
pub(crate) fn convert_variant(
779+
fn convert_variant(
780780
tcx: TyCtxt<'_>,
781781
variant_did: Option<LocalDefId>,
782782
ident: Ident,
@@ -827,7 +827,7 @@ pub(crate) fn convert_variant(
827827
)
828828
}
829829

830-
fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
830+
pub(crate) fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
831831
use rustc_hir::*;
832832

833833
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
@@ -885,7 +885,11 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
885885
}
886886
_ => bug!(),
887887
};
888-
tcx.mk_adt_def(def_id.to_def_id(), kind, variants, repr)
888+
if item.ident.name == kw::Empty {
889+
tcx.mk_anon_adt_def(def_id.to_def_id(), kind, variants, repr)
890+
} else {
891+
tcx.mk_adt_def(def_id.to_def_id(), kind, variants, repr)
892+
}
889893
}
890894

891895
fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {

compiler/rustc_hir_pretty/src/lib.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,7 @@ impl<'a> State<'a> {
367367
hir::TyKind::Infer => {
368368
self.word("_");
369369
}
370-
hir::TyKind::AnonStruct(fields) => {
371-
self.word("struct");
372-
self.print_variant_struct(ty.span, fields);
373-
}
374-
hir::TyKind::AnonUnion(fields) => {
375-
self.word("union");
376-
self.print_variant_struct(ty.span, fields);
377-
}
370+
hir::TyKind::AnonAdt(..) => self.word("/* anonymous adt */"),
378371
}
379372
self.end()
380373
}

0 commit comments

Comments
 (0)