Skip to content

Commit a8b76fc

Browse files
committed
Lowering anonymous structs or unions to HIR (WIP)
1 parent c3def26 commit a8b76fc

File tree

36 files changed

+278
-173
lines changed

36 files changed

+278
-173
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,9 +2108,9 @@ pub enum TyKind {
21082108
/// A tuple (`(A, B, C, D,...)`).
21092109
Tup(ThinVec<P<Ty>>),
21102110
/// An anonymous struct type i.e. `struct { foo: Type }`
2111-
AnonStruct(ThinVec<FieldDef>),
2111+
AnonStruct(NodeId, ThinVec<FieldDef>),
21122112
/// An anonymous union type i.e. `union { bar: Type }`
2113-
AnonUnion(ThinVec<FieldDef>),
2113+
AnonUnion(NodeId, ThinVec<FieldDef>),
21142114
/// A path (`module::module::...::Type`), optionally
21152115
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
21162116
///

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
514514
visit_vec(bounds, |bound| vis.visit_param_bound(bound));
515515
}
516516
TyKind::MacCall(mac) => vis.visit_mac_call(mac),
517-
TyKind::AnonStruct(fields) | TyKind::AnonUnion(fields) => {
517+
TyKind::AnonStruct(id, fields) | TyKind::AnonUnion(id, fields) => {
518+
vis.visit_id(id);
518519
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
519520
}
520521
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
441441
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
442442
TyKind::MacCall(mac) => visitor.visit_mac_call(mac),
443443
TyKind::Never | TyKind::CVarArgs => {}
444-
TyKind::AnonStruct(ref fields, ..) | TyKind::AnonUnion(ref fields, ..) => {
444+
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {
445445
walk_list!(visitor, visit_field_def, fields)
446446
}
447447
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
680680
}
681681
}
682682

683-
fn lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'hir> {
683+
pub(super) fn lower_field_def(
684+
&mut self,
685+
(index, f): (usize, &FieldDef),
686+
) -> hir::FieldDef<'hir> {
684687
let ty = if let TyKind::Path(qself, path) = &f.ty.kind {
685688
let t = self.lower_path_ty(
686689
&f.ty,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,18 +1326,42 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13261326
TyKind::Err => {
13271327
hir::TyKind::Err(self.tcx.sess.span_delayed_bug(t.span, "TyKind::Err lowered"))
13281328
}
1329-
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
1330-
#[allow(rustc::untranslatable_diagnostic)]
1331-
#[allow(rustc::diagnostic_outside_of_impl)]
1332-
TyKind::AnonStruct(ref _fields) => hir::TyKind::Err(
1333-
self.tcx.sess.span_err(t.span, "anonymous structs are unimplemented"),
1334-
),
1335-
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
1336-
#[allow(rustc::untranslatable_diagnostic)]
1337-
#[allow(rustc::diagnostic_outside_of_impl)]
1338-
TyKind::AnonUnion(ref _fields) => hir::TyKind::Err(
1339-
self.tcx.sess.span_err(t.span, "anonymous unions are unimplemented"),
1340-
),
1329+
TyKind::AnonStruct(def_node_id, fields) | TyKind::AnonUnion(def_node_id, fields) => {
1330+
let def_kind = match t.kind {
1331+
TyKind::AnonStruct(..) => DefKind::Struct,
1332+
TyKind::AnonUnion(..) => DefKind::Union,
1333+
_ => unreachable!(),
1334+
};
1335+
let def_id = self.create_def(
1336+
self.current_hir_id_owner.def_id,
1337+
*def_node_id,
1338+
sym::anon,
1339+
def_kind,
1340+
t.span,
1341+
);
1342+
debug!(?def_id);
1343+
let owner_id = hir::OwnerId { def_id };
1344+
self.with_hir_id_owner(*def_node_id, |this| {
1345+
let fields = this.arena.alloc_from_iter(
1346+
fields.iter().enumerate().map(|f| this.lower_field_def(f)),
1347+
);
1348+
let span = t.span;
1349+
let variant_data = hir::VariantData::Struct(fields, false);
1350+
let generics = hir::Generics::empty();
1351+
let kind = match &t.kind {
1352+
TyKind::AnonStruct(..) => hir::ItemKind::Struct(variant_data, generics),
1353+
_ => hir::ItemKind::Union(variant_data, generics),
1354+
};
1355+
hir::OwnerNode::Item(this.arena.alloc(hir::Item {
1356+
ident: Ident::with_dummy_span(sym::anon),
1357+
owner_id,
1358+
kind,
1359+
span: this.lower_span(span),
1360+
vis_span: this.lower_span(span.shrink_to_lo()),
1361+
}))
1362+
});
1363+
hir::TyKind::AnonAdt(hir::ItemId { owner_id })
1364+
}
13411365
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
13421366
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
13431367
TyKind::Ref(region, mt) => {

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a> AstValidator<'a> {
198198
}
199199
}
200200
}
201-
TyKind::AnonStruct(ref fields, ..) | TyKind::AnonUnion(ref fields, ..) => {
201+
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {
202202
walk_list!(self, visit_field_def, fields)
203203
}
204204
_ => visit::walk_ty(self, t),

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,11 +987,11 @@ impl<'a> State<'a> {
987987
}
988988
self.pclose();
989989
}
990-
ast::TyKind::AnonStruct(fields) => {
990+
ast::TyKind::AnonStruct(_, fields) => {
991991
self.head("struct");
992992
self.print_record_struct_body(fields, ty.span);
993993
}
994-
ast::TyKind::AnonUnion(fields) => {
994+
ast::TyKind::AnonUnion(_, fields) => {
995995
self.head("union");
996996
self.print_record_struct_body(fields, ty.span);
997997
}

compiler/rustc_hir/src/def.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_data_structures::stable_hasher::ToStableHashKey;
88
use rustc_macros::HashStable_Generic;
99
use rustc_span::def_id::{DefId, LocalDefId};
1010
use rustc_span::hygiene::MacroKind;
11+
use rustc_span::symbol::sym;
1112
use rustc_span::Symbol;
1213

1314
use std::array::IntoIter;
@@ -225,6 +226,7 @@ impl DefKind {
225226

226227
pub fn def_path_data(self, name: Symbol) -> DefPathData {
227228
match self {
229+
DefKind::Struct | DefKind::Union if name == sym::anon => DefPathData::AnonAdt,
228230
DefKind::Mod
229231
| DefKind::Struct
230232
| DefKind::Union

compiler/rustc_hir/src/definitions.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ pub enum DefPathData {
282282
/// An existential `impl Trait` type node.
283283
/// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
284284
OpaqueTy,
285+
/// An anonymous struct or union type i.e. `struct { foo: Type }` or `union { bar: Type }`
286+
AnonAdt,
285287
}
286288

287289
impl Definitions {
@@ -404,8 +406,9 @@ impl DefPathData {
404406
match *self {
405407
TypeNs(name) if name == kw::Empty => None,
406408
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
409+
407410
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | Closure | Ctor | AnonConst
408-
| OpaqueTy => None,
411+
| OpaqueTy | AnonAdt => None,
409412
}
410413
}
411414

@@ -426,6 +429,7 @@ impl DefPathData {
426429
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
427430
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
428431
OpaqueTy => DefPathDataName::Anon { namespace: sym::opaque },
432+
AnonAdt => DefPathDataName::Anon { namespace: sym::anon },
429433
}
430434
}
431435
}

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,8 @@ pub enum TyKind<'hir> {
25432543
Never,
25442544
/// A tuple (`(A, B, C, D, ...)`).
25452545
Tup(&'hir [Ty<'hir>]),
2546+
/// An anonymous struct or union type i.e. `struct { foo: Type }` or `union { foo: Type }`
2547+
AnonAdt(ItemId),
25462548
/// A path to a type definition (`module::module::...::Type`), or an
25472549
/// associated type (e.g., `<Vec<T> as Trait>::Type` or `<T>::Target`).
25482550
///

0 commit comments

Comments
 (0)