Rollup merge of #119145 - aDotInTheVoid:variantdata-struct-struct, r=compiler-errors

Give `VariantData::Struct`  named fields, to clairfy `recovered`.

Implements https://github.com/rust-lang/rust/pull/119121#discussion_r1431467066. Supersedes #119121

This way, it's clear what the bool fields means, instead of having to find where it's generated. Changes both ast and hir.

r? `@compiler-errors`
This commit is contained in:
Matthias Krüger 2023-12-20 21:18:59 +01:00 committed by GitHub
commit d0d814ff48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 51 additions and 40 deletions

View File

@ -2788,7 +2788,11 @@ pub enum VariantData {
/// Struct variant.
///
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
Struct(ThinVec<FieldDef>, bool),
Struct {
fields: ThinVec<FieldDef>,
// FIXME: investigate making this a `Option<ErrorGuaranteed>`
recovered: bool,
},
/// Tuple variant.
///
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
@ -2803,7 +2807,7 @@ impl VariantData {
/// Return the fields of this variant.
pub fn fields(&self) -> &[FieldDef] {
match self {
VariantData::Struct(fields, ..) | VariantData::Tuple(fields, _) => fields,
VariantData::Struct { fields, .. } | VariantData::Tuple(fields, _) => fields,
_ => &[],
}
}
@ -2811,7 +2815,7 @@ pub fn fields(&self) -> &[FieldDef] {
/// Return the `NodeId` of this variant's constructor, if it has one.
pub fn ctor_node_id(&self) -> Option<NodeId> {
match *self {
VariantData::Struct(..) => None,
VariantData::Struct { .. } => None,
VariantData::Tuple(_, id) | VariantData::Unit(id) => Some(id),
}
}

View File

@ -976,7 +976,7 @@ pub fn noop_visit_where_predicate<T: MutVisitor>(pred: &mut WherePredicate, vis:
pub fn noop_visit_variant_data<T: MutVisitor>(vdata: &mut VariantData, vis: &mut T) {
match vdata {
VariantData::Struct(fields, ..) => {
VariantData::Struct { fields, .. } => {
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
}
VariantData::Tuple(fields, id) => {

View File

@ -661,11 +661,12 @@ fn lower_variant_data(
vdata: &VariantData,
) -> hir::VariantData<'hir> {
match vdata {
VariantData::Struct(fields, recovered) => hir::VariantData::Struct(
self.arena
VariantData::Struct { fields, recovered } => hir::VariantData::Struct {
fields: self
.arena
.alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_field_def(f))),
*recovered,
),
recovered: *recovered,
},
VariantData::Tuple(fields, id) => {
let ctor_id = self.lower_node_id(*id);
self.alias_attrs(ctor_id, parent_id);

View File

@ -1000,7 +1000,7 @@ fn visit_item(&mut self, item: &'a Item) {
}
}
ItemKind::Struct(vdata, generics) => match vdata {
VariantData::Struct(fields, ..) => {
VariantData::Struct { fields, .. } => {
self.visit_vis(&item.vis);
self.visit_ident(item.ident);
self.visit_generics(generics);
@ -1016,7 +1016,7 @@ fn visit_item(&mut self, item: &'a Item) {
self.dcx().emit_err(errors::FieldlessUnion { span: item.span });
}
match vdata {
VariantData::Struct(fields, ..) => {
VariantData::Struct { fields, .. } => {
self.visit_vis(&item.vis);
self.visit_ident(item.ident);
self.visit_generics(generics);

View File

@ -500,7 +500,7 @@ fn print_struct(
self.end();
self.end(); // Close the outer-box.
}
ast::VariantData::Struct(fields, ..) => {
ast::VariantData::Struct { fields, .. } => {
self.print_where_clause(&generics.where_clause);
self.print_record_struct_body(fields, span);
}

View File

@ -188,7 +188,7 @@ fn cs_clone(
}
let expr = match *vdata {
VariantData::Struct(..) => {
VariantData::Struct { .. } => {
let fields = all_fields
.iter()
.map(|field| {

View File

@ -71,7 +71,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
(false, 0)
}
ast::VariantData::Tuple(..) => (false, 1),
ast::VariantData::Struct(..) => (true, 2),
ast::VariantData::Struct { .. } => (true, 2),
};
// The number of fields that can be handled without an array.
@ -226,7 +226,7 @@ fn show_fieldless_enum(
debug_assert!(fields.is_empty());
cx.pat_tuple_struct(span, variant_path, ThinVec::new())
}
ast::VariantData::Struct(fields, _) => {
ast::VariantData::Struct { fields, .. } => {
debug_assert!(fields.is_empty());
cx.pat_struct(span, variant_path, ThinVec::new())
}

View File

@ -1485,7 +1485,7 @@ fn create_struct_patterns(
let struct_path = struct_path.clone();
match *struct_def {
VariantData::Struct(..) => {
VariantData::Struct { .. } => {
let field_pats = pieces_iter
.map(|(sp, ident, pat)| {
if ident.is_none() {

View File

@ -174,7 +174,7 @@ fn mac_placeholder() -> P<ast::MacCall> {
}]),
AstFragmentKind::Variants => AstFragment::Variants(smallvec![ast::Variant {
attrs: Default::default(),
data: ast::VariantData::Struct(Default::default(), false),
data: ast::VariantData::Struct { fields: Default::default(), recovered: false },
disr_expr: None,
id,
ident,

View File

@ -603,7 +603,7 @@ pub fn from_ast(vdata: &ast::VariantData) -> Option<(CtorKind, NodeId)> {
match *vdata {
ast::VariantData::Tuple(_, node_id) => Some((CtorKind::Fn, node_id)),
ast::VariantData::Unit(node_id) => Some((CtorKind::Const, node_id)),
ast::VariantData::Struct(..) => None,
ast::VariantData::Struct { .. } => None,
}
}
}

View File

@ -2848,7 +2848,11 @@ pub enum VariantData<'hir> {
/// A struct variant.
///
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
Struct(&'hir [FieldDef<'hir>], /* recovered */ bool),
Struct {
fields: &'hir [FieldDef<'hir>],
// FIXME: investigate making this a `Option<ErrorGuaranteed>`
recovered: bool,
},
/// A tuple variant.
///
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
@ -2863,7 +2867,7 @@ impl<'hir> VariantData<'hir> {
/// Return the fields of this variant.
pub fn fields(&self) -> &'hir [FieldDef<'hir>] {
match *self {
VariantData::Struct(ref fields, ..) | VariantData::Tuple(ref fields, ..) => fields,
VariantData::Struct { fields, .. } | VariantData::Tuple(fields, ..) => fields,
_ => &[],
}
}
@ -2872,7 +2876,7 @@ pub fn ctor(&self) -> Option<(CtorKind, HirId, LocalDefId)> {
match *self {
VariantData::Tuple(_, hir_id, def_id) => Some((CtorKind::Fn, hir_id, def_id)),
VariantData::Unit(hir_id, def_id) => Some((CtorKind::Const, hir_id, def_id)),
VariantData::Struct(..) => None,
VariantData::Struct { .. } => None,
}
}

View File

@ -814,7 +814,7 @@ fn convert_variant(
})
.collect();
let recovered = match def {
hir::VariantData::Struct(_, r) => *r,
hir::VariantData::Struct { recovered, .. } => *recovered,
_ => false,
};
ty::VariantDef::new(

View File

@ -481,7 +481,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
},
Node::Ctor(def) | Node::Variant(Variant { data: def, .. }) => match def {
VariantData::Unit(..) | VariantData::Struct(..) => {
VariantData::Unit(..) | VariantData::Struct { .. } => {
tcx.type_of(tcx.hir().get_parent_item(hir_id)).instantiate_identity()
}
VariantData::Tuple(..) => {

View File

@ -741,7 +741,7 @@ fn print_struct(
self.end();
self.end() // close the outer-box
}
hir::VariantData::Struct(..) => {
hir::VariantData::Struct { .. } => {
self.print_where_clause(generics);
self.nbsp();
self.bopen();

View File

@ -1484,7 +1484,7 @@ fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> {
(thin_vec![], true)
}
};
VariantData::Struct(fields, recovered)
VariantData::Struct { fields, recovered }
} else if this.check(&token::OpenDelim(Delimiter::Parenthesis)) {
let body = match this.parse_tuple_struct_body() {
Ok(body) => body,
@ -1569,7 +1569,7 @@ fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
class_name.span,
generics.where_clause.has_where_token,
)?;
VariantData::Struct(fields, recovered)
VariantData::Struct { fields, recovered }
}
// No `where` so: `struct Foo<T>;`
} else if self.eat(&token::Semi) {
@ -1581,7 +1581,7 @@ fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
class_name.span,
generics.where_clause.has_where_token,
)?;
VariantData::Struct(fields, recovered)
VariantData::Struct { fields, recovered }
// Tuple-style struct definition with optional where-clause.
} else if self.token == token::OpenDelim(Delimiter::Parenthesis) {
let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
@ -1610,14 +1610,14 @@ fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
class_name.span,
generics.where_clause.has_where_token,
)?;
VariantData::Struct(fields, recovered)
VariantData::Struct { fields, recovered }
} else if self.token == token::OpenDelim(Delimiter::Brace) {
let (fields, recovered) = self.parse_record_struct_body(
"union",
class_name.span,
generics.where_clause.has_where_token,
)?;
VariantData::Struct(fields, recovered)
VariantData::Struct { fields, recovered }
} else {
let token_str = super::token_descr(&self.token);
let msg = format!("expected `where` or `{{` after union name, found {token_str}");

View File

@ -2479,8 +2479,8 @@ fn clean_variant_data<'tcx>(
.map(|disr| Discriminant { expr: Some(disr.body), value: disr.def_id.to_def_id() });
let kind = match variant {
hir::VariantData::Struct(..) => VariantKind::Struct(VariantStruct {
fields: variant.fields().iter().map(|x| clean_field(x, cx)).collect(),
hir::VariantData::Struct { fields, .. } => VariantKind::Struct(VariantStruct {
fields: fields.iter().map(|x| clean_field(x, cx)).collect(),
}),
hir::VariantData::Tuple(..) => {
VariantKind::Tuple(variant.fields().iter().map(|x| clean_field(x, cx)).collect())

View File

@ -436,7 +436,7 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
{
match item.kind {
ItemKind::Enum(def, _) => check_variant(cx, self.enum_threshold, &def, item_name, item.span),
ItemKind::Struct(VariantData::Struct(fields, _), _) => {
ItemKind::Struct(VariantData::Struct { fields, .. }, _) => {
check_fields(cx, self.struct_threshold, item, fields);
},
_ => (),

View File

@ -103,7 +103,7 @@ fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
if let ast::ItemKind::Struct(variant_data, _) = &item.kind {
let (fields, delimiter) = match variant_data {
ast::VariantData::Struct(fields, _) => (&**fields, '{'),
ast::VariantData::Struct { fields, .. } => (&**fields, '{'),
ast::VariantData::Tuple(fields, _) => (&**fields, '('),
ast::VariantData::Unit(_) => return,
};

View File

@ -546,7 +546,9 @@ pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
use VariantData::*;
match (l, r) {
(Unit(_), Unit(_)) => true,
(Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, eq_struct_field),
(Struct { fields: l, .. }, Struct { fields: r, .. }) | (Tuple(l, _), Tuple(r, _)) => {
over(l, r, eq_struct_field)
},
_ => false,
}
}

View File

@ -200,7 +200,7 @@ fn item_search_pat(item: &Item<'_>) -> (Pat, Pat) {
ItemKind::ForeignMod { .. } => (Pat::Str("extern"), Pat::Str("}")),
ItemKind::TyAlias(..) | ItemKind::OpaqueTy(_) => (Pat::Str("type"), Pat::Str(";")),
ItemKind::Enum(..) => (Pat::Str("enum"), Pat::Str("}")),
ItemKind::Struct(VariantData::Struct(..), _) => (Pat::Str("struct"), Pat::Str("}")),
ItemKind::Struct(VariantData::Struct { .. }, _) => (Pat::Str("struct"), Pat::Str("}")),
ItemKind::Struct(..) => (Pat::Str("struct"), Pat::Str(";")),
ItemKind::Union(..) => (Pat::Str("union"), Pat::Str("}")),
ItemKind::Trait(_, Unsafety::Unsafe, ..)
@ -255,7 +255,7 @@ fn field_def_search_pat(def: &FieldDef<'_>) -> (Pat, Pat) {
fn variant_search_pat(v: &Variant<'_>) -> (Pat, Pat) {
match v.data {
VariantData::Struct(..) => (Pat::Sym(v.ident.name), Pat::Str("}")),
VariantData::Struct { .. } => (Pat::Sym(v.ident.name), Pat::Str("}")),
VariantData::Tuple(..) => (Pat::Sym(v.ident.name), Pat::Str("")),
VariantData::Unit(..) => (Pat::Sym(v.ident.name), Pat::Sym(v.ident.name)),
}

View File

@ -666,7 +666,7 @@ fn format_variant(
let span = mk_sp(lo, field.span.lo());
let variant_body = match field.data {
ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => format_struct(
ast::VariantData::Tuple(..) | ast::VariantData::Struct { .. } => format_struct(
&context,
&StructParts::from_variant(field, &context),
self.block_indent,
@ -1092,7 +1092,7 @@ fn enum_variant_span(variant: &ast::Variant, context: &RewriteContext<'_>) -> Sp
if let Some(ref anon_const) = variant.disr_expr {
let span_before_consts = variant.span.until(anon_const.value.span);
let hi = match &variant.data {
Struct(..) => context
Struct { .. } => context
.snippet_provider
.span_after_last(span_before_consts, "}"),
Tuple(..) => context
@ -1112,12 +1112,12 @@ fn format_struct(
offset: Indent,
one_line_width: Option<usize>,
) -> Option<String> {
match *struct_parts.def {
match struct_parts.def {
ast::VariantData::Unit(..) => format_unit_struct(context, struct_parts, offset),
ast::VariantData::Tuple(ref fields, _) => {
ast::VariantData::Tuple(fields, _) => {
format_tuple_struct(context, struct_parts, fields, offset)
}
ast::VariantData::Struct(ref fields, _) => {
ast::VariantData::Struct { fields, .. } => {
format_struct_struct(context, struct_parts, fields, offset, one_line_width)
}
}