Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c09e12b

Browse files
authoredSep 12, 2021
Rollup merge of #88775 - pnkfelix:revert-anon-union-parsing, r=davidtwco
Revert anon union parsing Revert PR #84571 and #85515, which implemented anonymous union parsing in a manner that broke the context-sensitivity for the `union` keyword and thus broke stable Rust code. Fix #88583.
2 parents 11a4efc + 35370a7 commit c09e12b

File tree

21 files changed

+50
-595
lines changed

21 files changed

+50
-595
lines changed
 

‎compiler/rustc_ast/src/ast.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,10 +1902,6 @@ pub enum TyKind {
19021902
Never,
19031903
/// A tuple (`(A, B, C, D,...)`).
19041904
Tup(Vec<P<Ty>>),
1905-
/// An anonymous struct type i.e. `struct { foo: Type }`
1906-
AnonymousStruct(Vec<FieldDef>, bool),
1907-
/// An anonymous union type i.e. `union { bar: Type }`
1908-
AnonymousUnion(Vec<FieldDef>, bool),
19091905
/// A path (`module::module::...::Type`), optionally
19101906
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
19111907
///

‎compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,6 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
484484
visit_vec(bounds, |bound| vis.visit_param_bound(bound));
485485
}
486486
TyKind::MacCall(mac) => vis.visit_mac_call(mac),
487-
TyKind::AnonymousStruct(fields, ..) | TyKind::AnonymousUnion(fields, ..) => {
488-
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
489-
}
490487
}
491488
vis.visit_span(span);
492489
visit_lazy_tts(tokens, vis);

‎compiler/rustc_ast/src/visit.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,6 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
407407
TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
408408
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
409409
TyKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
410-
TyKind::AnonymousStruct(ref fields, ..) | TyKind::AnonymousUnion(ref fields, ..) => {
411-
walk_list!(visitor, visit_field_def, fields)
412-
}
413410
TyKind::Never | TyKind::CVarArgs => {}
414411
}
415412
}

‎compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
748748
}
749749
}
750750

751-
pub(super) fn lower_field_def(
752-
&mut self,
753-
(index, f): (usize, &FieldDef),
754-
) -> hir::FieldDef<'hir> {
751+
fn lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'hir> {
755752
let ty = if let TyKind::Path(ref qself, ref path) = f.ty.kind {
756753
let t = self.lower_path_ty(
757754
&f.ty,

‎compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,15 +1301,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13011301
let kind = match t.kind {
13021302
TyKind::Infer => hir::TyKind::Infer,
13031303
TyKind::Err => hir::TyKind::Err,
1304-
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
1305-
TyKind::AnonymousStruct(ref _fields, _recovered) => {
1306-
self.sess.struct_span_err(t.span, "anonymous structs are unimplemented").emit();
1307-
hir::TyKind::Err
1308-
}
1309-
TyKind::AnonymousUnion(ref _fields, _recovered) => {
1310-
self.sess.struct_span_err(t.span, "anonymous unions are unimplemented").emit();
1311-
hir::TyKind::Err
1312-
}
13131304
TyKind::Slice(ref ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
13141305
TyKind::Ptr(ref mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
13151306
TyKind::Rptr(ref region, ref mt) => {

‎compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,13 @@ impl<'a> AstValidator<'a> {
193193
}
194194
}
195195
}
196-
TyKind::AnonymousStruct(ref fields, ..) | TyKind::AnonymousUnion(ref fields, ..) => {
197-
self.with_banned_assoc_ty_bound(|this| {
198-
walk_list!(this, visit_struct_field_def, fields)
199-
});
200-
}
201196
_ => visit::walk_ty(self, t),
202197
}
203198
}
204199

205200
fn visit_struct_field_def(&mut self, field: &'a FieldDef) {
206201
if let Some(ident) = field.ident {
207202
if ident.name == kw::Underscore {
208-
self.check_anonymous_field(field);
209203
self.visit_vis(&field.vis);
210204
self.visit_ident(ident);
211205
self.visit_ty_common(&field.ty);
@@ -251,66 +245,6 @@ impl<'a> AstValidator<'a> {
251245
err.emit();
252246
}
253247

254-
fn check_anonymous_field(&self, field: &FieldDef) {
255-
let FieldDef { ty, .. } = field;
256-
match &ty.kind {
257-
TyKind::AnonymousStruct(..) | TyKind::AnonymousUnion(..) => {
258-
// We already checked for `kw::Underscore` before calling this function,
259-
// so skip the check
260-
}
261-
TyKind::Path(..) => {
262-
// If the anonymous field contains a Path as type, we can't determine
263-
// if the path is a valid struct or union, so skip the check
264-
}
265-
_ => {
266-
let msg = "unnamed fields can only have struct or union types";
267-
let label = "not a struct or union";
268-
self.err_handler()
269-
.struct_span_err(field.span, msg)
270-
.span_label(ty.span, label)
271-
.emit();
272-
}
273-
}
274-
}
275-
276-
fn deny_anonymous_struct(&self, ty: &Ty) {
277-
match &ty.kind {
278-
TyKind::AnonymousStruct(..) => {
279-
self.err_handler()
280-
.struct_span_err(
281-
ty.span,
282-
"anonymous structs are not allowed outside of unnamed struct or union fields",
283-
)
284-
.span_label(ty.span, "anonymous struct declared here")
285-
.emit();
286-
}
287-
TyKind::AnonymousUnion(..) => {
288-
self.err_handler()
289-
.struct_span_err(
290-
ty.span,
291-
"anonymous unions are not allowed outside of unnamed struct or union fields",
292-
)
293-
.span_label(ty.span, "anonymous union declared here")
294-
.emit();
295-
}
296-
_ => {}
297-
}
298-
}
299-
300-
fn deny_anonymous_field(&self, field: &FieldDef) {
301-
if let Some(ident) = field.ident {
302-
if ident.name == kw::Underscore {
303-
self.err_handler()
304-
.struct_span_err(
305-
field.span,
306-
"anonymous fields are not allowed outside of structs or unions",
307-
)
308-
.span_label(ident.span, "anonymous field declared here")
309-
.emit()
310-
}
311-
}
312-
}
313-
314248
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
315249
for Param { pat, .. } in &decl.inputs {
316250
match pat.kind {
@@ -1081,7 +1015,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10811015

10821016
fn visit_ty(&mut self, ty: &'a Ty) {
10831017
self.visit_ty_common(ty);
1084-
self.deny_anonymous_struct(ty);
10851018
self.walk_ty(ty)
10861019
}
10871020

@@ -1096,7 +1029,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10961029
}
10971030

10981031
fn visit_field_def(&mut self, s: &'a FieldDef) {
1099-
self.deny_anonymous_field(s);
11001032
visit::walk_field_def(self, s)
11011033
}
11021034

‎compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
668668
// involved, so we only emit errors where there are no other parsing errors.
669669
gate_all!(destructuring_assignment, "destructuring assignments are unstable");
670670
}
671-
gate_all!(unnamed_fields, "unnamed fields are not yet fully implemented");
672671

673672
// All uses of `gate_all!` below this point were added in #65742,
674673
// and subsequently disabled (with the non-early gating readded).

‎compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -985,14 +985,6 @@ impl<'a> State<'a> {
985985
}
986986
self.pclose();
987987
}
988-
ast::TyKind::AnonymousStruct(ref fields, ..) => {
989-
self.head("struct");
990-
self.print_record_struct_body(&fields, ty.span);
991-
}
992-
ast::TyKind::AnonymousUnion(ref fields, ..) => {
993-
self.head("union");
994-
self.print_record_struct_body(&fields, ty.span);
995-
}
996988
ast::TyKind::Paren(ref typ) => {
997989
self.popen();
998990
self.print_type(typ);
@@ -1413,7 +1405,12 @@ impl<'a> State<'a> {
14131405
}
14141406
}
14151407

1416-
crate fn print_record_struct_body(&mut self, fields: &[ast::FieldDef], span: rustc_span::Span) {
1408+
crate fn print_record_struct_body(
1409+
&mut self,
1410+
fields: &Vec<ast::FieldDef>,
1411+
span: rustc_span::Span,
1412+
) {
1413+
self.nbsp();
14171414
self.bopen();
14181415
self.hardbreak_if_not_bol();
14191416

@@ -1462,7 +1459,6 @@ impl<'a> State<'a> {
14621459
}
14631460
ast::VariantData::Struct(ref fields, ..) => {
14641461
self.print_where_clause(&generics.where_clause);
1465-
self.nbsp();
14661462
self.print_record_struct_body(fields, span);
14671463
}
14681464
}

‎compiler/rustc_feature/src/active.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,6 @@ declare_features! (
638638
/// Allows specifying the as-needed link modifier
639639
(active, native_link_modifiers_as_needed, "1.53.0", Some(81490), None),
640640

641-
/// Allows unnamed fields of struct and union type
642-
(incomplete, unnamed_fields, "1.53.0", Some(49804), None),
643-
644641
/// Allows qualified paths in struct expressions, struct patterns and tuple struct patterns.
645642
(active, more_qualified_paths, "1.54.0", Some(86935), None),
646643

‎compiler/rustc_parse/src/parser/item.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ impl<'a> Parser<'a> {
12341234
Ok((class_name, ItemKind::Union(vdata, generics)))
12351235
}
12361236

1237-
pub(super) fn parse_record_struct_body(
1237+
fn parse_record_struct_body(
12381238
&mut self,
12391239
adt_ty: &str,
12401240
) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
@@ -1468,28 +1468,22 @@ impl<'a> Parser<'a> {
14681468
fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
14691469
let (ident, is_raw) = self.ident_or_err()?;
14701470
if !is_raw && ident.is_reserved() {
1471-
if ident.name == kw::Underscore {
1472-
self.sess.gated_spans.gate(sym::unnamed_fields, lo);
1471+
let err = if self.check_fn_front_matter(false) {
1472+
// We use `parse_fn` to get a span for the function
1473+
if let Err(mut db) = self.parse_fn(&mut Vec::new(), |_| true, lo) {
1474+
db.delay_as_bug();
1475+
}
1476+
let mut err = self.struct_span_err(
1477+
lo.to(self.prev_token.span),
1478+
&format!("functions are not allowed in {} definitions", adt_ty),
1479+
);
1480+
err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks");
1481+
err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
1482+
err
14731483
} else {
1474-
let err = if self.check_fn_front_matter(false) {
1475-
// We use `parse_fn` to get a span for the function
1476-
if let Err(mut db) = self.parse_fn(&mut Vec::new(), |_| true, lo) {
1477-
db.delay_as_bug();
1478-
}
1479-
let mut err = self.struct_span_err(
1480-
lo.to(self.prev_token.span),
1481-
&format!("functions are not allowed in {} definitions", adt_ty),
1482-
);
1483-
err.help(
1484-
"unlike in C++, Java, and C#, functions are declared in `impl` blocks",
1485-
);
1486-
err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
1487-
err
1488-
} else {
1489-
self.expected_ident_found()
1490-
};
1491-
return Err(err);
1492-
}
1484+
self.expected_ident_found()
1485+
};
1486+
return Err(err);
14931487
}
14941488
self.bump();
14951489
Ok(ident)

‎compiler/rustc_parse/src/parser/ty.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,6 @@ impl<'a> Parser<'a> {
226226
}
227227
} else if self.eat_keyword(kw::Impl) {
228228
self.parse_impl_ty(&mut impl_dyn_multi)?
229-
} else if self.token.is_keyword(kw::Union)
230-
&& self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace))
231-
{
232-
self.bump();
233-
let (fields, recovered) = self.parse_record_struct_body("union")?;
234-
let span = lo.to(self.prev_token.span);
235-
self.sess.gated_spans.gate(sym::unnamed_fields, span);
236-
TyKind::AnonymousUnion(fields, recovered)
237-
} else if self.eat_keyword(kw::Struct) {
238-
let (fields, recovered) = self.parse_record_struct_body("struct")?;
239-
let span = lo.to(self.prev_token.span);
240-
self.sess.gated_spans.gate(sym::unnamed_fields, span);
241-
TyKind::AnonymousStruct(fields, recovered)
242229
} else if self.is_explicit_dyn_type() {
243230
self.parse_dyn_ty(&mut impl_dyn_multi)?
244231
} else if self.eat_lt() {

‎compiler/rustc_span/src/symbol.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,6 @@ symbols! {
13591359
unix,
13601360
unlikely,
13611361
unmarked_api,
1362-
unnamed_fields,
13631362
unpin,
13641363
unreachable,
13651364
unreachable_code,

‎src/test/pretty/anonymous-types.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

‎src/test/ui/feature-gates/feature-gate-unnamed_fields.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎src/test/ui/feature-gates/feature-gate-unnamed_fields.stderr

Lines changed: 0 additions & 111 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
3+
#![allow(non_camel_case_types)]
4+
5+
struct union;
6+
7+
impl union {
8+
pub fn new() -> Self {
9+
union { }
10+
}
11+
}
12+
13+
fn main() {
14+
let _u = union::new();
15+
}

‎src/test/ui/unnamed_fields/restrict_anonymous.rs

Lines changed: 0 additions & 52 deletions
This file was deleted.

‎src/test/ui/unnamed_fields/restrict_anonymous.stderr

Lines changed: 0 additions & 175 deletions
This file was deleted.

‎src/tools/rustfmt/src/items.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::cmp::{max, min, Ordering};
66
use regex::Regex;
77
use rustc_ast::visit;
88
use rustc_ast::{ast, ptr};
9-
use rustc_span::{symbol, BytePos, Span};
9+
use rustc_span::{symbol, BytePos, Span, DUMMY_SP};
1010

1111
use crate::attr::filter_inline_attrs;
1212
use crate::comment::{
@@ -31,7 +31,12 @@ use crate::stmt::Stmt;
3131
use crate::utils::*;
3232
use crate::vertical::rewrite_with_alignment;
3333
use crate::visitor::FmtVisitor;
34-
use crate::DEFAULT_VISIBILITY;
34+
35+
const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
36+
kind: ast::VisibilityKind::Inherited,
37+
span: DUMMY_SP,
38+
tokens: None,
39+
};
3540

3641
fn type_annotation_separator(config: &Config) -> &str {
3742
colon_spaces(config)
@@ -972,7 +977,7 @@ impl<'a> StructParts<'a> {
972977
format_header(context, self.prefix, self.ident, self.vis, offset)
973978
}
974979

975-
pub(crate) fn from_variant(variant: &'a ast::Variant) -> Self {
980+
fn from_variant(variant: &'a ast::Variant) -> Self {
976981
StructParts {
977982
prefix: "",
978983
ident: variant.ident,

‎src/tools/rustfmt/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::path::PathBuf;
3232
use std::rc::Rc;
3333

3434
use rustc_ast::ast;
35-
use rustc_span::{symbol, DUMMY_SP};
35+
use rustc_span::symbol;
3636
use thiserror::Error;
3737

3838
use crate::comment::LineClasses;
@@ -96,11 +96,6 @@ mod types;
9696
mod vertical;
9797
pub(crate) mod visitor;
9898

99-
const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
100-
kind: ast::VisibilityKind::Inherited,
101-
span: DUMMY_SP,
102-
tokens: None,
103-
};
10499
/// The various errors that can occur during formatting. Note that not all of
105100
/// these can currently be propagated to clients.
106101
#[derive(Error, Debug)]

‎src/tools/rustfmt/src/types.rs

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::iter::ExactSizeIterator;
22
use std::ops::Deref;
33

4-
use rustc_ast::ast::{self, AttrVec, FnRetTy, Mutability};
5-
use rustc_span::{symbol::kw, symbol::Ident, BytePos, Pos, Span};
4+
use rustc_ast::ast::{self, FnRetTy, Mutability};
5+
use rustc_span::{symbol::kw, BytePos, Pos, Span};
66

7+
use crate::comment::{combine_strs_with_missing_comments, contains_comment};
78
use crate::config::lists::*;
89
use crate::config::{IndentStyle, TypeDensity, Version};
910
use crate::expr::{
1011
format_expr, rewrite_assign_rhs, rewrite_call, rewrite_tuple, rewrite_unary_prefix, ExprType,
1112
};
12-
use crate::items::StructParts;
1313
use crate::lists::{
1414
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
1515
};
@@ -24,11 +24,6 @@ use crate::utils::{
2424
colon_spaces, extra_offset, first_line_width, format_extern, format_mutability,
2525
last_line_extendable, last_line_width, mk_sp, rewrite_ident,
2626
};
27-
use crate::DEFAULT_VISIBILITY;
28-
use crate::{
29-
comment::{combine_strs_with_missing_comments, contains_comment},
30-
items::format_struct_struct,
31-
};
3227

3328
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3429
pub(crate) enum PathContext {
@@ -769,54 +764,6 @@ impl Rewrite for ast::Ty {
769764
ast::TyKind::Tup(ref items) => {
770765
rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
771766
}
772-
ast::TyKind::AnonymousStruct(ref fields, recovered) => {
773-
let ident = Ident::new(
774-
kw::Struct,
775-
mk_sp(self.span.lo(), self.span.lo() + BytePos(6)),
776-
);
777-
let data = ast::VariantData::Struct(fields.clone(), recovered);
778-
let variant = ast::Variant {
779-
attrs: AttrVec::new(),
780-
id: self.id,
781-
span: self.span,
782-
vis: DEFAULT_VISIBILITY,
783-
ident,
784-
data,
785-
disr_expr: None,
786-
is_placeholder: false,
787-
};
788-
format_struct_struct(
789-
&context,
790-
&StructParts::from_variant(&variant),
791-
fields,
792-
shape.indent,
793-
None,
794-
)
795-
}
796-
ast::TyKind::AnonymousUnion(ref fields, recovered) => {
797-
let ident = Ident::new(
798-
kw::Union,
799-
mk_sp(self.span.lo(), self.span.lo() + BytePos(5)),
800-
);
801-
let data = ast::VariantData::Struct(fields.clone(), recovered);
802-
let variant = ast::Variant {
803-
attrs: AttrVec::new(),
804-
id: self.id,
805-
span: self.span,
806-
vis: DEFAULT_VISIBILITY,
807-
ident,
808-
data,
809-
disr_expr: None,
810-
is_placeholder: false,
811-
};
812-
format_struct_struct(
813-
&context,
814-
&StructParts::from_variant(&variant),
815-
fields,
816-
shape.indent,
817-
None,
818-
)
819-
}
820767
ast::TyKind::Path(ref q_self, ref path) => {
821768
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
822769
}

0 commit comments

Comments
 (0)
Please sign in to comment.