Skip to content

Commit bdc1592

Browse files
committed
Auto merge of rust-lang#115367 - frank-king:feature/unnamed-fields-hir, r=davidtwco
Lowering unnamed fields and anonymous adt This implements rust-lang#49804. Goals: - [x] lowering anonymous ADTs from AST to HIR - [x] generating definitions of anonymous ADTs - [x] uniqueness check of the unnamed fields - [x] field projection of anonymous ADTs - [x] `#[repr(C)]` check of the anonymous ADTs Non-Goals (will be in the next PRs) - capturing generic params for the anonymous ADTs from the parent ADT - pattern matching of anonymous ADTs - structural expressions of anonymous ADTs - rustdoc support of anonymous ADTs
2 parents ed19532 + 0dbd6e9 commit bdc1592

File tree

60 files changed

+3273
-259
lines changed

Some content is hidden

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

60 files changed

+3273
-259
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,9 +2107,9 @@ pub enum TyKind {
21072107
/// A tuple (`(A, B, C, D,...)`).
21082108
Tup(ThinVec<P<Ty>>),
21092109
/// An anonymous struct type i.e. `struct { foo: Type }`
2110-
AnonStruct(ThinVec<FieldDef>),
2110+
AnonStruct(NodeId, ThinVec<FieldDef>),
21112111
/// An anonymous union type i.e. `union { bar: Type }`
2112-
AnonUnion(ThinVec<FieldDef>),
2112+
AnonUnion(NodeId, ThinVec<FieldDef>),
21132113
/// A path (`module::module::...::Type`), optionally
21142114
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
21152115
///
@@ -2161,6 +2161,10 @@ impl TyKind {
21612161
None
21622162
}
21632163
}
2164+
2165+
pub fn is_anon_adt(&self) -> bool {
2166+
matches!(self, TyKind::AnonStruct(..) | TyKind::AnonUnion(..))
2167+
}
21642168
}
21652169

21662170
/// Syntax used to declare a trait object.

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
@@ -450,7 +450,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
450450
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
451451
TyKind::MacCall(mac) => visitor.visit_mac_call(mac),
452452
TyKind::Never | TyKind::CVarArgs => {}
453-
TyKind::AnonStruct(ref fields, ..) | TyKind::AnonUnion(ref fields, ..) => {
453+
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {
454454
walk_list!(visitor, visit_field_def, fields)
455455
}
456456
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
720720
}
721721
}
722722

723-
fn lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'hir> {
723+
pub(super) fn lower_field_def(
724+
&mut self,
725+
(index, f): (usize, &FieldDef),
726+
) -> hir::FieldDef<'hir> {
724727
let ty = if let TyKind::Path(qself, path) = &f.ty.kind {
725728
let t = self.lower_path_ty(
726729
&f.ty,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,17 +1288,44 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12881288
TyKind::Err => {
12891289
hir::TyKind::Err(self.dcx().span_delayed_bug(t.span, "TyKind::Err lowered"))
12901290
}
1291-
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
1292-
#[allow(rustc::untranslatable_diagnostic)]
1293-
#[allow(rustc::diagnostic_outside_of_impl)]
1294-
TyKind::AnonStruct(ref _fields) => {
1295-
hir::TyKind::Err(self.dcx().span_err(t.span, "anonymous structs are unimplemented"))
1296-
}
1297-
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
1298-
#[allow(rustc::untranslatable_diagnostic)]
1299-
#[allow(rustc::diagnostic_outside_of_impl)]
1300-
TyKind::AnonUnion(ref _fields) => {
1301-
hir::TyKind::Err(self.dcx().span_err(t.span, "anonymous unions are unimplemented"))
1291+
// Lower the anonymous structs or unions in a nested lowering context.
1292+
//
1293+
// ```
1294+
// struct Foo {
1295+
// _: union {
1296+
// // ^__________________ <-- within the nested lowering context,
1297+
// /* fields */ // | we lower all fields defined into an
1298+
// } // | owner node of struct or union item
1299+
// // ^_____________________|
1300+
// }
1301+
// ```
1302+
TyKind::AnonStruct(node_id, fields) | TyKind::AnonUnion(node_id, fields) => {
1303+
// Here its `def_id` is created in `build_reduced_graph`.
1304+
let def_id = self.local_def_id(*node_id);
1305+
debug!(?def_id);
1306+
let owner_id = hir::OwnerId { def_id };
1307+
self.with_hir_id_owner(*node_id, |this| {
1308+
let fields = this.arena.alloc_from_iter(
1309+
fields.iter().enumerate().map(|f| this.lower_field_def(f)),
1310+
);
1311+
let span = t.span;
1312+
let variant_data = hir::VariantData::Struct { fields, recovered: false };
1313+
// FIXME: capture the generics from the outer adt.
1314+
let generics = hir::Generics::empty();
1315+
let kind = match t.kind {
1316+
TyKind::AnonStruct(..) => hir::ItemKind::Struct(variant_data, generics),
1317+
TyKind::AnonUnion(..) => hir::ItemKind::Union(variant_data, generics),
1318+
_ => unreachable!(),
1319+
};
1320+
hir::OwnerNode::Item(this.arena.alloc(hir::Item {
1321+
ident: Ident::new(kw::Empty, span),
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 })
13021329
}
13031330
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
13041331
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ impl<'a> AstValidator<'a> {
219219
}
220220
}
221221
}
222-
TyKind::AnonStruct(ref fields, ..) | TyKind::AnonUnion(ref fields, ..) => {
223-
walk_list!(self, visit_field_def, fields)
222+
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {
223+
walk_list!(self, visit_struct_field_def, fields)
224224
}
225225
_ => visit::walk_ty(self, t),
226226
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,11 +1003,11 @@ impl<'a> State<'a> {
10031003
}
10041004
self.pclose();
10051005
}
1006-
ast::TyKind::AnonStruct(fields) => {
1006+
ast::TyKind::AnonStruct(_, fields) => {
10071007
self.head("struct");
10081008
self.print_record_struct_body(fields, ty.span);
10091009
}
1010-
ast::TyKind::AnonUnion(fields) => {
1010+
ast::TyKind::AnonUnion(_, fields) => {
10111011
self.head("union");
10121012
self.print_record_struct_body(fields, ty.span);
10131013
}

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ fn cs_clone_simple(
110110
&& !seen_type_names.insert(name)
111111
{
112112
// Already produced an assertion for this type.
113-
} else {
113+
// Anonymous structs or unions must be eliminated as they cannot be
114+
// type parameters.
115+
} else if !field.ty.kind.is_anon_adt() {
114116
// let _: AssertParamIsClone<FieldTy>;
115117
super::assert_ty_bounds(
116118
cx,

compiler/rustc_builtin_macros/src/deriving/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ fn assert_ty_bounds(
123123
span: Span,
124124
assert_path: &[Symbol],
125125
) {
126+
// Deny anonymous structs or unions to avoid wierd errors.
127+
assert!(!ty.kind.is_anon_adt(), "Anonymous structs or unions cannot be type parameters");
126128
// Generate statement `let _: assert_path<ty>;`.
127129
let span = cx.with_def_site_ctxt(span);
128130
let assert_path = cx.path_all(span, true, cx.std_path(assert_path), vec![GenericArg::Type(ty)]);

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::unord::UnordMap;
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::kw;
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 == kw::Empty => DefPathData::AnonAdt,
228230
DefKind::Mod
229231
| DefKind::Struct
230232
| DefKind::Union

0 commit comments

Comments
 (0)