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

@@ -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));
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);
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]);
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));
}
// 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![]);