Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 40465d2

Browse files
Remove anon struct and union types
1 parent e3a0da1 commit 40465d2

File tree

19 files changed

+3
-227
lines changed

19 files changed

+3
-227
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,10 +2167,6 @@ pub enum TyKind {
21672167
Never,
21682168
/// A tuple (`(A, B, C, D,...)`).
21692169
Tup(ThinVec<P<Ty>>),
2170-
/// An anonymous struct type i.e. `struct { foo: Type }`.
2171-
AnonStruct(NodeId, ThinVec<FieldDef>),
2172-
/// An anonymous union type i.e. `union { bar: Type }`.
2173-
AnonUnion(NodeId, ThinVec<FieldDef>),
21742170
/// A path (`module::module::...::Type`), optionally
21752171
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
21762172
///
@@ -2227,10 +2223,6 @@ impl TyKind {
22272223
None
22282224
}
22292225
}
2230-
2231-
pub fn is_anon_adt(&self) -> bool {
2232-
matches!(self, TyKind::AnonStruct(..) | TyKind::AnonUnion(..))
2233-
}
22342226
}
22352227

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

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,6 @@ pub fn walk_ty<T: MutVisitor>(vis: &mut T, ty: &mut P<Ty>) {
519519
visit_vec(bounds, |bound| vis.visit_param_bound(bound, BoundKind::Impl));
520520
}
521521
TyKind::MacCall(mac) => vis.visit_mac_call(mac),
522-
TyKind::AnonStruct(id, fields) | TyKind::AnonUnion(id, fields) => {
523-
vis.visit_id(id);
524-
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
525-
}
526522
}
527523
visit_lazy_tts(vis, tokens);
528524
vis.visit_span(span);

compiler/rustc_ast/src/util/classify.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,6 @@ fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
287287
| ast::TyKind::Pat(..)
288288
| ast::TyKind::Dummy
289289
| ast::TyKind::Err(..) => break None,
290-
291-
// These end in brace, but cannot occur in a let-else statement.
292-
// They are only parsed as fields of a data structure. For the
293-
// purpose of denying trailing braces in the expression of a
294-
// let-else, we can disregard these.
295-
ast::TyKind::AnonStruct(..) | ast::TyKind::AnonUnion(..) => break None,
296290
}
297291
}
298292
}

compiler/rustc_ast/src/visit.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,6 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) -> V::Result {
535535
TyKind::Err(_guar) => {}
536536
TyKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
537537
TyKind::Never | TyKind::CVarArgs => {}
538-
TyKind::AnonStruct(_id, ref fields) | TyKind::AnonUnion(_id, ref fields) => {
539-
walk_list!(visitor, visit_field_def, fields);
540-
}
541538
}
542539
V::Result::output()
543540
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,46 +1264,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12641264
let kind = match &t.kind {
12651265
TyKind::Infer => hir::TyKind::Infer,
12661266
TyKind::Err(guar) => hir::TyKind::Err(*guar),
1267-
// Lower the anonymous structs or unions in a nested lowering context.
1268-
//
1269-
// ```
1270-
// struct Foo {
1271-
// _: union {
1272-
// // ^__________________ <-- within the nested lowering context,
1273-
// /* fields */ // | we lower all fields defined into an
1274-
// } // | owner node of struct or union item
1275-
// // ^_____________________|
1276-
// }
1277-
// ```
1278-
TyKind::AnonStruct(node_id, fields) | TyKind::AnonUnion(node_id, fields) => {
1279-
// Here its `def_id` is created in `build_reduced_graph`.
1280-
let def_id = self.local_def_id(*node_id);
1281-
debug!(?def_id);
1282-
let owner_id = hir::OwnerId { def_id };
1283-
self.with_hir_id_owner(*node_id, |this| {
1284-
let fields = this.arena.alloc_from_iter(
1285-
fields.iter().enumerate().map(|f| this.lower_field_def(f)),
1286-
);
1287-
let span = t.span;
1288-
let variant_data =
1289-
hir::VariantData::Struct { fields, recovered: ast::Recovered::No };
1290-
// FIXME: capture the generics from the outer adt.
1291-
let generics = hir::Generics::empty();
1292-
let kind = match t.kind {
1293-
TyKind::AnonStruct(..) => hir::ItemKind::Struct(variant_data, generics),
1294-
TyKind::AnonUnion(..) => hir::ItemKind::Union(variant_data, generics),
1295-
_ => unreachable!(),
1296-
};
1297-
hir::OwnerNode::Item(this.arena.alloc(hir::Item {
1298-
ident: Ident::new(kw::Empty, span),
1299-
owner_id,
1300-
kind,
1301-
span: this.lower_span(span),
1302-
vis_span: this.lower_span(span.shrink_to_lo()),
1303-
}))
1304-
});
1305-
hir::TyKind::AnonAdt(hir::ItemId { owner_id })
1306-
}
13071267
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
13081268
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
13091269
TyKind::Ref(region, mt) => {

compiler/rustc_ast_passes/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
ast_passes_anon_struct_or_union_not_allowed =
2-
anonymous {$struct_or_union}s are not allowed outside of unnamed struct or union fields
3-
.label = anonymous {$struct_or_union} declared here
4-
51
ast_passes_assoc_const_without_body =
62
associated constant in `impl` without body
73
.suggestion = provide a definition for the constant

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,6 @@ impl<'a> AstValidator<'a> {
244244
}
245245
}
246246
}
247-
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {
248-
walk_list!(self, visit_struct_field_def, fields)
249-
}
250247
_ => visit::walk_ty(self, t),
251248
}
252249
}
@@ -293,15 +290,6 @@ impl<'a> AstValidator<'a> {
293290
}
294291
}
295292

296-
fn deny_anon_struct_or_union(&self, ty: &Ty) {
297-
let struct_or_union = match &ty.kind {
298-
TyKind::AnonStruct(..) => "struct",
299-
TyKind::AnonUnion(..) => "union",
300-
_ => return,
301-
};
302-
self.dcx().emit_err(errors::AnonStructOrUnionNotAllowed { struct_or_union, span: ty.span });
303-
}
304-
305293
fn check_trait_fn_not_const(&self, constness: Const, parent: &TraitOrTraitImpl) {
306294
let Const::Yes(span) = constness else {
307295
return;
@@ -865,14 +853,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
865853

866854
fn visit_ty(&mut self, ty: &'a Ty) {
867855
self.visit_ty_common(ty);
868-
self.deny_anon_struct_or_union(ty);
869856
self.walk_ty(ty)
870857
}
871858

872-
fn visit_field_def(&mut self, field: &'a FieldDef) {
873-
visit::walk_field_def(self, field)
874-
}
875-
876859
fn visit_item(&mut self, item: &'a Item) {
877860
if item.attrs.iter().any(|attr| attr.is_proc_macro_attr()) {
878861
self.has_proc_macro_decls = true;

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -814,15 +814,6 @@ pub(crate) struct NegativeBoundWithParentheticalNotation {
814814
pub span: Span,
815815
}
816816

817-
#[derive(Diagnostic)]
818-
#[diag(ast_passes_anon_struct_or_union_not_allowed)]
819-
pub(crate) struct AnonStructOrUnionNotAllowed {
820-
#[primary_span]
821-
#[label]
822-
pub span: Span,
823-
pub struct_or_union: &'static str,
824-
}
825-
826817
#[derive(Diagnostic)]
827818
#[diag(ast_passes_match_arm_with_no_body)]
828819
pub(crate) struct MatchArmWithNoBody {

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,14 +1174,6 @@ impl<'a> State<'a> {
11741174
}
11751175
self.pclose();
11761176
}
1177-
ast::TyKind::AnonStruct(_, fields) => {
1178-
self.head("struct");
1179-
self.print_record_struct_body(fields, ty.span);
1180-
}
1181-
ast::TyKind::AnonUnion(_, fields) => {
1182-
self.head("union");
1183-
self.print_record_struct_body(fields, ty.span);
1184-
}
11851177
ast::TyKind::Paren(typ) => {
11861178
self.popen();
11871179
self.print_type(typ);

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn cs_clone_simple(
113113
// Already produced an assertion for this type.
114114
// Anonymous structs or unions must be eliminated as they cannot be
115115
// type parameters.
116-
} else if !field.ty.kind.is_anon_adt() {
116+
} else {
117117
// let _: AssertParamIsClone<FieldTy>;
118118
super::assert_ty_bounds(cx, &mut stmts, field.ty.clone(), field.span, &[
119119
sym::clone,

0 commit comments

Comments
 (0)