Skip to content
Permalink
Browse files

Remove methods is_struct/is_tuple/is_unit from VariantData

  • Loading branch information...
petrochenkov committed Mar 23, 2019
1 parent 5c3d1e5 commit c667c2fc0896b2a58a363d2c535881e5b983857a
@@ -2208,33 +2208,6 @@ impl VariantData {
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
@@ -860,41 +860,44 @@ impl<'a> State<'a> {
-> io::Result<()> {
self.print_name(name)?;
self.print_generic_params(&generics.params)?;
if !struct_def.is_struct() {
if struct_def.is_tuple() {
self.popen()?;
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
s.maybe_print_comment(field.span.lo())?;
s.print_outer_attributes(&field.attrs)?;
s.print_visibility(&field.vis)?;
s.print_type(&field.ty)
})?;
self.pclose()?;
}
self.print_where_clause(&generics.where_clause)?;
if print_finalizer {
self.s.word(";")?;
match struct_def {
hir::VariantData::Tuple(..) | hir::VariantData::Unit(..) => {
if let hir::VariantData::Tuple(..) = struct_def {
self.popen()?;
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
s.maybe_print_comment(field.span.lo())?;
s.print_outer_attributes(&field.attrs)?;
s.print_visibility(&field.vis)?;
s.print_type(&field.ty)
})?;
self.pclose()?;
}
self.print_where_clause(&generics.where_clause)?;
if print_finalizer {
self.s.word(";")?;
}
self.end()?;
self.end() // close the outer-box
}
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() {
hir::VariantData::Struct(..) => {
self.print_where_clause(&generics.where_clause)?;
self.nbsp()?;
self.bopen()?;
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)
}
}
}

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

impl Clean<VariantKind> for hir::VariantData {
fn clean(&self, cx: &DocContext<'_>) -> VariantKind {
if self.is_struct() {
VariantKind::Struct(self.clean(cx))
} else if self.is_unit() {
VariantKind::CLike
} else {
VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
match self {
hir::VariantData::Struct(..) => VariantKind::Struct(self.clean(cx)),
hir::VariantData::Tuple(..) =>
VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect()),
hir::VariantData::Unit(..) => VariantKind::CLike,
}
}
}
@@ -2155,33 +2155,6 @@ impl VariantData {
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.
@@ -1550,44 +1550,47 @@ impl<'a> State<'a> {
print_finalizer: bool) -> io::Result<()> {
self.print_ident(ident)?;
self.print_generic_params(&generics.params)?;
if !struct_def.is_struct() {
if struct_def.is_tuple() {
self.popen()?;
self.commasep(
Inconsistent, struct_def.fields(),
|s, field| {
s.maybe_print_comment(field.span.lo())?;
s.print_outer_attributes(&field.attrs)?;
s.print_visibility(&field.vis)?;
s.print_type(&field.ty)
}
)?;
self.pclose()?;
}
self.print_where_clause(&generics.where_clause)?;
if print_finalizer {
self.s.word(";")?;
match struct_def {
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
if let ast::VariantData::Tuple(..) = struct_def {
self.popen()?;
self.commasep(
Inconsistent, struct_def.fields(),
|s, field| {
s.maybe_print_comment(field.span.lo())?;
s.print_outer_attributes(&field.attrs)?;
s.print_visibility(&field.vis)?;
s.print_type(&field.ty)
}
)?;
self.pclose()?;
}
self.print_where_clause(&generics.where_clause)?;
if print_finalizer {
self.s.word(";")?;
}
self.end()?;
self.end() // close the outer-box
}
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() {
ast::VariantData::Struct(..) => {
self.print_where_clause(&generics.where_clause)?;
self.nbsp()?;
self.bopen()?;
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)
}
}
}

@@ -51,9 +51,9 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
// build fmt.debug_struct(<name>).field(<fieldname>, &<fieldval>)....build()
// or fmt.debug_tuple(<name>).field(&<fieldval>)....build()
// based on the "shape".
let (ident, is_struct) = match *substr.fields {
Struct(vdata, _) => (substr.type_ident, vdata.is_struct()),
EnumMatching(_, _, v, _) => (v.node.ident, v.node.data.is_struct()),
let (ident, vdata, fields) = match substr.fields {
Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
EnumMatching(_, _, v, fields) => (v.node.ident, &v.node.data, fields),
EnumNonMatchingCollapsed(..) |
StaticStruct(..) |
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 mut stmts = match *substr.fields {
Struct(_, ref fields) |
EnumMatching(.., ref fields) => {
let mut stmts = vec![];
if !is_struct {
// tuple struct/"normal" variant
let 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 {
// 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![field]);

// Use `let _ = expr;` to avoid triggering the
// unused_results lint.
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));
}
let mut stmts = vec![];
match vdata {
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
// tuple struct/"normal" variant
let 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 {
// 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![field]);

// Use `let _ = expr;` to avoid triggering the
// unused_results lint.
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![]);

@@ -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()) {
(false, false) => {
cx.span_bug(self.span,
@@ -1547,9 +1548,10 @@ impl<'a> TraitDef<'a> {
}
// named fields
(_, false) => Named(named_idents),
// empty structs
_ if struct_def.is_struct() => Named(named_idents),
_ => Unnamed(just_spans, struct_def.is_tuple()),
// unnamed fields
(false, _) => Unnamed(just_spans, is_tuple),
// empty
_ => Named(Vec::new()),
}
}

0 comments on commit c667c2f

Please sign in to comment.
You can’t perform that action at this time.