Remove methods is_struct/is_tuple/is_unit from VariantData

This commit is contained in:
Vadim Petrochenkov
2019-03-24 00:06:58 +03:00
parent 5c3d1e5d76
commit c667c2fc08
8 changed files with 127 additions and 178 deletions

View File

@@ -2208,33 +2208,6 @@ impl VariantData {
VariantData::Tuple(_, hir_id) | VariantData::Unit(hir_id) => Some(hir_id), VariantData::Tuple(_, hir_id) | VariantData::Unit(hir_id) => Some(hir_id),
} }
} }
/// Does this `VariantData` represent a `Struct`-struct/variant?
pub fn is_struct(&self) -> bool {
if let VariantData::Struct(..) = *self {
true
} else {
false
}
}
/// Does this `VariantData` represent a tuple struct/variant?
pub fn is_tuple(&self) -> bool {
if let VariantData::Tuple(..) = *self {
true
} else {
false
}
}
/// Does this `VariantData` represent a unit struct/variant?
pub fn is_unit(&self) -> bool {
if let VariantData::Unit(..) = *self {
true
} else {
false
}
}
} }
// The bodies for items are stored "out of line", in a separate // The bodies for items are stored "out of line", in a separate

View File

@@ -860,41 +860,44 @@ impl<'a> State<'a> {
-> io::Result<()> { -> io::Result<()> {
self.print_name(name)?; self.print_name(name)?;
self.print_generic_params(&generics.params)?; self.print_generic_params(&generics.params)?;
if !struct_def.is_struct() { match struct_def {
if struct_def.is_tuple() { hir::VariantData::Tuple(..) | hir::VariantData::Unit(..) => {
self.popen()?; if let hir::VariantData::Tuple(..) = struct_def {
self.commasep(Inconsistent, struct_def.fields(), |s, field| { self.popen()?;
s.maybe_print_comment(field.span.lo())?; self.commasep(Inconsistent, struct_def.fields(), |s, field| {
s.print_outer_attributes(&field.attrs)?; s.maybe_print_comment(field.span.lo())?;
s.print_visibility(&field.vis)?; s.print_outer_attributes(&field.attrs)?;
s.print_type(&field.ty) s.print_visibility(&field.vis)?;
})?; s.print_type(&field.ty)
self.pclose()?; })?;
self.pclose()?;
}
self.print_where_clause(&generics.where_clause)?;
if print_finalizer {
self.s.word(";")?;
}
self.end()?;
self.end() // close the outer-box
} }
self.print_where_clause(&generics.where_clause)?; hir::VariantData::Struct(..) => {
if print_finalizer { self.print_where_clause(&generics.where_clause)?;
self.s.word(";")?; self.nbsp()?;
} self.bopen()?;
self.end()?;
self.end() // close the outer-box
} else {
self.print_where_clause(&generics.where_clause)?;
self.nbsp()?;
self.bopen()?;
self.hardbreak_if_not_bol()?;
for field in struct_def.fields() {
self.hardbreak_if_not_bol()?; self.hardbreak_if_not_bol()?;
self.maybe_print_comment(field.span.lo())?;
self.print_outer_attributes(&field.attrs)?;
self.print_visibility(&field.vis)?;
self.print_ident(field.ident)?;
self.word_nbsp(":")?;
self.print_type(&field.ty)?;
self.s.word(",")?;
}
self.bclose(span) for field in struct_def.fields() {
self.hardbreak_if_not_bol()?;
self.maybe_print_comment(field.span.lo())?;
self.print_outer_attributes(&field.attrs)?;
self.print_visibility(&field.vis)?;
self.print_ident(field.ident)?;
self.word_nbsp(":")?;
self.print_type(&field.ty)?;
self.s.word(",")?;
}
self.bclose(span)
}
} }
} }

View File

@@ -661,7 +661,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
} }
} }
ItemKind::Union(ref vdata, _) => { ItemKind::Union(ref vdata, _) => {
if !vdata.is_struct() { if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {
self.err_handler().span_err(item.span, self.err_handler().span_err(item.span,
"tuple and unit unions are not permitted"); "tuple and unit unions are not permitted");
} }

View File

@@ -3192,12 +3192,11 @@ pub enum VariantKind {
impl Clean<VariantKind> for hir::VariantData { impl Clean<VariantKind> for hir::VariantData {
fn clean(&self, cx: &DocContext<'_>) -> VariantKind { fn clean(&self, cx: &DocContext<'_>) -> VariantKind {
if self.is_struct() { match self {
VariantKind::Struct(self.clean(cx)) hir::VariantData::Struct(..) => VariantKind::Struct(self.clean(cx)),
} else if self.is_unit() { hir::VariantData::Tuple(..) =>
VariantKind::CLike VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect()),
} else { hir::VariantData::Unit(..) => VariantKind::CLike,
VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
} }
} }
} }

View File

@@ -2155,33 +2155,6 @@ impl VariantData {
VariantData::Tuple(_, id) | VariantData::Unit(id) => Some(id), VariantData::Tuple(_, id) | VariantData::Unit(id) => Some(id),
} }
} }
/// Does this `VariantData` represent a `Struct`-struct/variant?
pub fn is_struct(&self) -> bool {
if let VariantData::Struct(..) = *self {
true
} else {
false
}
}
/// Does this `VariantData` represent a tuple struct/variant?
pub fn is_tuple(&self) -> bool {
if let VariantData::Tuple(..) = *self {
true
} else {
false
}
}
/// Does this `VariantData` represent a unit struct/variant?
pub fn is_unit(&self) -> bool {
if let VariantData::Unit(..) = *self {
true
} else {
false
}
}
} }
/// An item. /// An item.

View File

@@ -1550,44 +1550,47 @@ impl<'a> State<'a> {
print_finalizer: bool) -> io::Result<()> { print_finalizer: bool) -> io::Result<()> {
self.print_ident(ident)?; self.print_ident(ident)?;
self.print_generic_params(&generics.params)?; self.print_generic_params(&generics.params)?;
if !struct_def.is_struct() { match struct_def {
if struct_def.is_tuple() { ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
self.popen()?; if let ast::VariantData::Tuple(..) = struct_def {
self.commasep( self.popen()?;
Inconsistent, struct_def.fields(), self.commasep(
|s, field| { Inconsistent, struct_def.fields(),
s.maybe_print_comment(field.span.lo())?; |s, field| {
s.print_outer_attributes(&field.attrs)?; s.maybe_print_comment(field.span.lo())?;
s.print_visibility(&field.vis)?; s.print_outer_attributes(&field.attrs)?;
s.print_type(&field.ty) s.print_visibility(&field.vis)?;
} s.print_type(&field.ty)
)?; }
self.pclose()?; )?;
self.pclose()?;
}
self.print_where_clause(&generics.where_clause)?;
if print_finalizer {
self.s.word(";")?;
}
self.end()?;
self.end() // close the outer-box
} }
self.print_where_clause(&generics.where_clause)?; ast::VariantData::Struct(..) => {
if print_finalizer { self.print_where_clause(&generics.where_clause)?;
self.s.word(";")?; self.nbsp()?;
} self.bopen()?;
self.end()?;
self.end() // close the outer-box
} else {
self.print_where_clause(&generics.where_clause)?;
self.nbsp()?;
self.bopen()?;
self.hardbreak_if_not_bol()?;
for field in struct_def.fields() {
self.hardbreak_if_not_bol()?; self.hardbreak_if_not_bol()?;
self.maybe_print_comment(field.span.lo())?;
self.print_outer_attributes(&field.attrs)?;
self.print_visibility(&field.vis)?;
self.print_ident(field.ident.unwrap())?;
self.word_nbsp(":")?;
self.print_type(&field.ty)?;
self.s.word(",")?;
}
self.bclose(span) for field in struct_def.fields() {
self.hardbreak_if_not_bol()?;
self.maybe_print_comment(field.span.lo())?;
self.print_outer_attributes(&field.attrs)?;
self.print_visibility(&field.vis)?;
self.print_ident(field.ident.unwrap())?;
self.word_nbsp(":")?;
self.print_type(&field.ty)?;
self.s.word(",")?;
}
self.bclose(span)
}
} }
} }

View File

@@ -51,9 +51,9 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
// build fmt.debug_struct(<name>).field(<fieldname>, &<fieldval>)....build() // build fmt.debug_struct(<name>).field(<fieldname>, &<fieldval>)....build()
// or fmt.debug_tuple(<name>).field(&<fieldval>)....build() // or fmt.debug_tuple(<name>).field(&<fieldval>)....build()
// based on the "shape". // based on the "shape".
let (ident, is_struct) = match *substr.fields { let (ident, vdata, fields) = match substr.fields {
Struct(vdata, _) => (substr.type_ident, vdata.is_struct()), Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
EnumMatching(_, _, v, _) => (v.node.ident, v.node.data.is_struct()), EnumMatching(_, _, v, fields) => (v.node.ident, &v.node.data, fields),
EnumNonMatchingCollapsed(..) | EnumNonMatchingCollapsed(..) |
StaticStruct(..) | StaticStruct(..) |
StaticEnum(..) => cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`"), StaticEnum(..) => cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`"),
@@ -67,55 +67,51 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
let fmt = substr.nonself_args[0].clone(); let fmt = substr.nonself_args[0].clone();
let mut stmts = match *substr.fields { let mut stmts = vec![];
Struct(_, ref fields) | match vdata {
EnumMatching(.., ref fields) => { ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
let mut stmts = vec![]; // tuple struct/"normal" variant
if !is_struct { let expr =
// tuple struct/"normal" variant cx.expr_method_call(span, fmt, Ident::from_str("debug_tuple"), vec![name]);
let expr = stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
cx.expr_method_call(span, fmt, Ident::from_str("debug_tuple"), vec![name]);
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
for field in fields { for field in fields {
// Use double indirection to make sure this works for unsized types // Use double indirection to make sure this works for unsized types
let field = cx.expr_addr_of(field.span, field.self_.clone()); let field = cx.expr_addr_of(field.span, field.self_.clone());
let field = cx.expr_addr_of(field.span, field); let field = cx.expr_addr_of(field.span, field);
let expr = cx.expr_method_call(span, let expr = cx.expr_method_call(span,
builder_expr.clone(), builder_expr.clone(),
Ident::from_str("field"), Ident::from_str("field"),
vec![field]); vec![field]);
// Use `let _ = expr;` to avoid triggering the // Use `let _ = expr;` to avoid triggering the
// unused_results lint. // unused_results lint.
stmts.push(stmt_let_undescore(cx, span, expr)); stmts.push(stmt_let_undescore(cx, span, expr));
}
} else {
// normal struct/struct variant
let expr =
cx.expr_method_call(span, fmt, Ident::from_str("debug_struct"), vec![name]);
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
for field in fields {
let name = cx.expr_lit(field.span,
ast::LitKind::Str(field.name.unwrap().name,
ast::StrStyle::Cooked));
// Use double indirection to make sure this works for unsized types
let field = cx.expr_addr_of(field.span, field.self_.clone());
let field = cx.expr_addr_of(field.span, field);
let expr = cx.expr_method_call(span,
builder_expr.clone(),
Ident::from_str("field"),
vec![name, field]);
stmts.push(stmt_let_undescore(cx, span, expr));
}
} }
stmts
} }
_ => unreachable!(), ast::VariantData::Struct(..) => {
}; // normal struct/struct variant
let expr =
cx.expr_method_call(span, fmt, Ident::from_str("debug_struct"), vec![name]);
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
for field in fields {
let name = cx.expr_lit(field.span,
ast::LitKind::Str(field.name.unwrap().name,
ast::StrStyle::Cooked));
// Use double indirection to make sure this works for unsized types
let field = cx.expr_addr_of(field.span, field.self_.clone());
let field = cx.expr_addr_of(field.span, field);
let expr = cx.expr_method_call(span,
builder_expr.clone(),
Ident::from_str("field"),
vec![name, field]);
stmts.push(stmt_let_undescore(cx, span, expr));
}
}
}
let expr = cx.expr_method_call(span, builder_expr, Ident::from_str("finish"), vec![]); let expr = cx.expr_method_call(span, builder_expr, Ident::from_str("finish"), vec![]);

View File

@@ -1539,6 +1539,7 @@ impl<'a> TraitDef<'a> {
} }
} }
let is_tuple = if let ast::VariantData::Tuple(..) = struct_def { true } else { false };
match (just_spans.is_empty(), named_idents.is_empty()) { match (just_spans.is_empty(), named_idents.is_empty()) {
(false, false) => { (false, false) => {
cx.span_bug(self.span, cx.span_bug(self.span,
@@ -1547,9 +1548,10 @@ impl<'a> TraitDef<'a> {
} }
// named fields // named fields
(_, false) => Named(named_idents), (_, false) => Named(named_idents),
// empty structs // unnamed fields
_ if struct_def.is_struct() => Named(named_idents), (false, _) => Unnamed(just_spans, is_tuple),
_ => Unnamed(just_spans, struct_def.is_tuple()), // empty
_ => Named(Vec::new()),
} }
} }