1
Fork 0

Lower anonymous structs or unions to HIR

This commit is contained in:
Frank King 2024-01-04 21:53:06 +08:00
parent 084ce5bdb5
commit 879a1e5713
38 changed files with 288 additions and 174 deletions

View file

@ -720,7 +720,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}
fn lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'hir> {
pub(super) fn lower_field_def(
&mut self,
(index, f): (usize, &FieldDef),
) -> hir::FieldDef<'hir> {
let ty = if let TyKind::Path(qself, path) = &f.ty.kind {
let t = self.lower_path_ty(
&f.ty,

View file

@ -1288,17 +1288,49 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
TyKind::Err => {
hir::TyKind::Err(self.dcx().span_delayed_bug(t.span, "TyKind::Err lowered"))
}
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
TyKind::AnonStruct(ref _fields) => {
hir::TyKind::Err(self.dcx().span_err(t.span, "anonymous structs are unimplemented"))
}
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
TyKind::AnonUnion(ref _fields) => {
hir::TyKind::Err(self.dcx().span_err(t.span, "anonymous unions are unimplemented"))
// Lower the anonymous structs or unions in a nested lowering context.
//
// ```
// struct Foo {
// _: union {
// // ^__________________ <-- within the nested lowering context,
// /* fields */ // | we lower all fields defined into an
// } // | owner node of struct or union item
// // ^_____________________|
// }
// ```
TyKind::AnonStruct(def_node_id, fields) | TyKind::AnonUnion(def_node_id, fields) => {
let (def_kind, item_kind): (DefKind, fn(_, _) -> _) = match t.kind {
TyKind::AnonStruct(..) => (DefKind::Struct, hir::ItemKind::Struct),
TyKind::AnonUnion(..) => (DefKind::Union, hir::ItemKind::Union),
_ => unreachable!(),
};
let def_id = self.create_def(
self.current_hir_id_owner.def_id,
*def_node_id,
kw::Empty,
def_kind,
t.span,
);
debug!(?def_id);
let owner_id = hir::OwnerId { def_id };
self.with_hir_id_owner(*def_node_id, |this| {
let fields = this.arena.alloc_from_iter(
fields.iter().enumerate().map(|f| this.lower_field_def(f)),
);
let span = t.span;
let variant_data = hir::VariantData::Struct(fields, false);
// FIXME: capture the generics from the outer adt.
let generics = hir::Generics::empty();
hir::OwnerNode::Item(this.arena.alloc(hir::Item {
ident: Ident::new(kw::Empty, span),
owner_id,
kind: item_kind(variant_data, generics),
span: this.lower_span(span),
vis_span: this.lower_span(span.shrink_to_lo()),
}))
});
hir::TyKind::AnonAdt(hir::ItemId { owner_id })
}
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),