Skip to content

Commit c667c2f

Browse files
committed
Remove methods is_struct/is_tuple/is_unit from VariantData
1 parent 5c3d1e5 commit c667c2f

File tree

8 files changed

+130
-181
lines changed

8 files changed

+130
-181
lines changed

src/librustc/hir/mod.rs

-27
Original file line numberDiff line numberDiff line change
@@ -2208,33 +2208,6 @@ impl VariantData {
22082208
VariantData::Tuple(_, hir_id) | VariantData::Unit(hir_id) => Some(hir_id),
22092209
}
22102210
}
2211-
2212-
/// Does this `VariantData` represent a `Struct`-struct/variant?
2213-
pub fn is_struct(&self) -> bool {
2214-
if let VariantData::Struct(..) = *self {
2215-
true
2216-
} else {
2217-
false
2218-
}
2219-
}
2220-
2221-
/// Does this `VariantData` represent a tuple struct/variant?
2222-
pub fn is_tuple(&self) -> bool {
2223-
if let VariantData::Tuple(..) = *self {
2224-
true
2225-
} else {
2226-
false
2227-
}
2228-
}
2229-
2230-
/// Does this `VariantData` represent a unit struct/variant?
2231-
pub fn is_unit(&self) -> bool {
2232-
if let VariantData::Unit(..) = *self {
2233-
true
2234-
} else {
2235-
false
2236-
}
2237-
}
22382211
}
22392212

22402213
// The bodies for items are stored "out of line", in a separate

src/librustc/hir/print.rs

+35-32
Original file line numberDiff line numberDiff line change
@@ -860,41 +860,44 @@ impl<'a> State<'a> {
860860
-> io::Result<()> {
861861
self.print_name(name)?;
862862
self.print_generic_params(&generics.params)?;
863-
if !struct_def.is_struct() {
864-
if struct_def.is_tuple() {
865-
self.popen()?;
866-
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
867-
s.maybe_print_comment(field.span.lo())?;
868-
s.print_outer_attributes(&field.attrs)?;
869-
s.print_visibility(&field.vis)?;
870-
s.print_type(&field.ty)
871-
})?;
872-
self.pclose()?;
873-
}
874-
self.print_where_clause(&generics.where_clause)?;
875-
if print_finalizer {
876-
self.s.word(";")?;
863+
match struct_def {
864+
hir::VariantData::Tuple(..) | hir::VariantData::Unit(..) => {
865+
if let hir::VariantData::Tuple(..) = struct_def {
866+
self.popen()?;
867+
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
868+
s.maybe_print_comment(field.span.lo())?;
869+
s.print_outer_attributes(&field.attrs)?;
870+
s.print_visibility(&field.vis)?;
871+
s.print_type(&field.ty)
872+
})?;
873+
self.pclose()?;
874+
}
875+
self.print_where_clause(&generics.where_clause)?;
876+
if print_finalizer {
877+
self.s.word(";")?;
878+
}
879+
self.end()?;
880+
self.end() // close the outer-box
877881
}
878-
self.end()?;
879-
self.end() // close the outer-box
880-
} else {
881-
self.print_where_clause(&generics.where_clause)?;
882-
self.nbsp()?;
883-
self.bopen()?;
884-
self.hardbreak_if_not_bol()?;
885-
886-
for field in struct_def.fields() {
882+
hir::VariantData::Struct(..) => {
883+
self.print_where_clause(&generics.where_clause)?;
884+
self.nbsp()?;
885+
self.bopen()?;
887886
self.hardbreak_if_not_bol()?;
888-
self.maybe_print_comment(field.span.lo())?;
889-
self.print_outer_attributes(&field.attrs)?;
890-
self.print_visibility(&field.vis)?;
891-
self.print_ident(field.ident)?;
892-
self.word_nbsp(":")?;
893-
self.print_type(&field.ty)?;
894-
self.s.word(",")?;
895-
}
896887

897-
self.bclose(span)
888+
for field in struct_def.fields() {
889+
self.hardbreak_if_not_bol()?;
890+
self.maybe_print_comment(field.span.lo())?;
891+
self.print_outer_attributes(&field.attrs)?;
892+
self.print_visibility(&field.vis)?;
893+
self.print_ident(field.ident)?;
894+
self.word_nbsp(":")?;
895+
self.print_type(&field.ty)?;
896+
self.s.word(",")?;
897+
}
898+
899+
self.bclose(span)
900+
}
898901
}
899902
}
900903

src/librustc_passes/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
661661
}
662662
}
663663
ItemKind::Union(ref vdata, _) => {
664-
if !vdata.is_struct() {
664+
if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {
665665
self.err_handler().span_err(item.span,
666666
"tuple and unit unions are not permitted");
667667
}

src/librustdoc/clean/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3192,12 +3192,11 @@ pub enum VariantKind {
31923192

31933193
impl Clean<VariantKind> for hir::VariantData {
31943194
fn clean(&self, cx: &DocContext<'_>) -> VariantKind {
3195-
if self.is_struct() {
3196-
VariantKind::Struct(self.clean(cx))
3197-
} else if self.is_unit() {
3198-
VariantKind::CLike
3199-
} else {
3200-
VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
3195+
match self {
3196+
hir::VariantData::Struct(..) => VariantKind::Struct(self.clean(cx)),
3197+
hir::VariantData::Tuple(..) =>
3198+
VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect()),
3199+
hir::VariantData::Unit(..) => VariantKind::CLike,
32013200
}
32023201
}
32033202
}

src/libsyntax/ast.rs

-27
Original file line numberDiff line numberDiff line change
@@ -2155,33 +2155,6 @@ impl VariantData {
21552155
VariantData::Tuple(_, id) | VariantData::Unit(id) => Some(id),
21562156
}
21572157
}
2158-
2159-
/// Does this `VariantData` represent a `Struct`-struct/variant?
2160-
pub fn is_struct(&self) -> bool {
2161-
if let VariantData::Struct(..) = *self {
2162-
true
2163-
} else {
2164-
false
2165-
}
2166-
}
2167-
2168-
/// Does this `VariantData` represent a tuple struct/variant?
2169-
pub fn is_tuple(&self) -> bool {
2170-
if let VariantData::Tuple(..) = *self {
2171-
true
2172-
} else {
2173-
false
2174-
}
2175-
}
2176-
2177-
/// Does this `VariantData` represent a unit struct/variant?
2178-
pub fn is_unit(&self) -> bool {
2179-
if let VariantData::Unit(..) = *self {
2180-
true
2181-
} else {
2182-
false
2183-
}
2184-
}
21852158
}
21862159

21872160
/// An item.

src/libsyntax/print/pprust.rs

+38-35
Original file line numberDiff line numberDiff line change
@@ -1550,44 +1550,47 @@ impl<'a> State<'a> {
15501550
print_finalizer: bool) -> io::Result<()> {
15511551
self.print_ident(ident)?;
15521552
self.print_generic_params(&generics.params)?;
1553-
if !struct_def.is_struct() {
1554-
if struct_def.is_tuple() {
1555-
self.popen()?;
1556-
self.commasep(
1557-
Inconsistent, struct_def.fields(),
1558-
|s, field| {
1559-
s.maybe_print_comment(field.span.lo())?;
1560-
s.print_outer_attributes(&field.attrs)?;
1561-
s.print_visibility(&field.vis)?;
1562-
s.print_type(&field.ty)
1563-
}
1564-
)?;
1565-
self.pclose()?;
1566-
}
1567-
self.print_where_clause(&generics.where_clause)?;
1568-
if print_finalizer {
1569-
self.s.word(";")?;
1553+
match struct_def {
1554+
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
1555+
if let ast::VariantData::Tuple(..) = struct_def {
1556+
self.popen()?;
1557+
self.commasep(
1558+
Inconsistent, struct_def.fields(),
1559+
|s, field| {
1560+
s.maybe_print_comment(field.span.lo())?;
1561+
s.print_outer_attributes(&field.attrs)?;
1562+
s.print_visibility(&field.vis)?;
1563+
s.print_type(&field.ty)
1564+
}
1565+
)?;
1566+
self.pclose()?;
1567+
}
1568+
self.print_where_clause(&generics.where_clause)?;
1569+
if print_finalizer {
1570+
self.s.word(";")?;
1571+
}
1572+
self.end()?;
1573+
self.end() // close the outer-box
15701574
}
1571-
self.end()?;
1572-
self.end() // close the outer-box
1573-
} else {
1574-
self.print_where_clause(&generics.where_clause)?;
1575-
self.nbsp()?;
1576-
self.bopen()?;
1577-
self.hardbreak_if_not_bol()?;
1578-
1579-
for field in struct_def.fields() {
1575+
ast::VariantData::Struct(..) => {
1576+
self.print_where_clause(&generics.where_clause)?;
1577+
self.nbsp()?;
1578+
self.bopen()?;
15801579
self.hardbreak_if_not_bol()?;
1581-
self.maybe_print_comment(field.span.lo())?;
1582-
self.print_outer_attributes(&field.attrs)?;
1583-
self.print_visibility(&field.vis)?;
1584-
self.print_ident(field.ident.unwrap())?;
1585-
self.word_nbsp(":")?;
1586-
self.print_type(&field.ty)?;
1587-
self.s.word(",")?;
1588-
}
15891580

1590-
self.bclose(span)
1581+
for field in struct_def.fields() {
1582+
self.hardbreak_if_not_bol()?;
1583+
self.maybe_print_comment(field.span.lo())?;
1584+
self.print_outer_attributes(&field.attrs)?;
1585+
self.print_visibility(&field.vis)?;
1586+
self.print_ident(field.ident.unwrap())?;
1587+
self.word_nbsp(":")?;
1588+
self.print_type(&field.ty)?;
1589+
self.s.word(",")?;
1590+
}
1591+
1592+
self.bclose(span)
1593+
}
15911594
}
15921595
}
15931596

src/libsyntax_ext/deriving/debug.rs

+46-50
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
5151
// build fmt.debug_struct(<name>).field(<fieldname>, &<fieldval>)....build()
5252
// or fmt.debug_tuple(<name>).field(&<fieldval>)....build()
5353
// based on the "shape".
54-
let (ident, is_struct) = match *substr.fields {
55-
Struct(vdata, _) => (substr.type_ident, vdata.is_struct()),
56-
EnumMatching(_, _, v, _) => (v.node.ident, v.node.data.is_struct()),
54+
let (ident, vdata, fields) = match substr.fields {
55+
Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
56+
EnumMatching(_, _, v, fields) => (v.node.ident, &v.node.data, fields),
5757
EnumNonMatchingCollapsed(..) |
5858
StaticStruct(..) |
5959
StaticEnum(..) => cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`"),
@@ -67,55 +67,51 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
6767

6868
let fmt = substr.nonself_args[0].clone();
6969

70-
let mut stmts = match *substr.fields {
71-
Struct(_, ref fields) |
72-
EnumMatching(.., ref fields) => {
73-
let mut stmts = vec![];
74-
if !is_struct {
75-
// tuple struct/"normal" variant
76-
let expr =
77-
cx.expr_method_call(span, fmt, Ident::from_str("debug_tuple"), vec![name]);
78-
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
79-
80-
for field in fields {
81-
// Use double indirection to make sure this works for unsized types
82-
let field = cx.expr_addr_of(field.span, field.self_.clone());
83-
let field = cx.expr_addr_of(field.span, field);
84-
85-
let expr = cx.expr_method_call(span,
86-
builder_expr.clone(),
87-
Ident::from_str("field"),
88-
vec![field]);
89-
90-
// Use `let _ = expr;` to avoid triggering the
91-
// unused_results lint.
92-
stmts.push(stmt_let_undescore(cx, span, expr));
93-
}
94-
} else {
95-
// normal struct/struct variant
96-
let expr =
97-
cx.expr_method_call(span, fmt, Ident::from_str("debug_struct"), vec![name]);
98-
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
99-
100-
for field in fields {
101-
let name = cx.expr_lit(field.span,
102-
ast::LitKind::Str(field.name.unwrap().name,
103-
ast::StrStyle::Cooked));
104-
105-
// Use double indirection to make sure this works for unsized types
106-
let field = cx.expr_addr_of(field.span, field.self_.clone());
107-
let field = cx.expr_addr_of(field.span, field);
108-
let expr = cx.expr_method_call(span,
109-
builder_expr.clone(),
110-
Ident::from_str("field"),
111-
vec![name, field]);
112-
stmts.push(stmt_let_undescore(cx, span, expr));
113-
}
70+
let mut stmts = vec![];
71+
match vdata {
72+
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
73+
// tuple struct/"normal" variant
74+
let expr =
75+
cx.expr_method_call(span, fmt, Ident::from_str("debug_tuple"), vec![name]);
76+
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
77+
78+
for field in fields {
79+
// Use double indirection to make sure this works for unsized types
80+
let field = cx.expr_addr_of(field.span, field.self_.clone());
81+
let field = cx.expr_addr_of(field.span, field);
82+
83+
let expr = cx.expr_method_call(span,
84+
builder_expr.clone(),
85+
Ident::from_str("field"),
86+
vec![field]);
87+
88+
// Use `let _ = expr;` to avoid triggering the
89+
// unused_results lint.
90+
stmts.push(stmt_let_undescore(cx, span, expr));
11491
}
115-
stmts
11692
}
117-
_ => unreachable!(),
118-
};
93+
ast::VariantData::Struct(..) => {
94+
// normal struct/struct variant
95+
let expr =
96+
cx.expr_method_call(span, fmt, Ident::from_str("debug_struct"), vec![name]);
97+
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
98+
99+
for field in fields {
100+
let name = cx.expr_lit(field.span,
101+
ast::LitKind::Str(field.name.unwrap().name,
102+
ast::StrStyle::Cooked));
103+
104+
// Use double indirection to make sure this works for unsized types
105+
let field = cx.expr_addr_of(field.span, field.self_.clone());
106+
let field = cx.expr_addr_of(field.span, field);
107+
let expr = cx.expr_method_call(span,
108+
builder_expr.clone(),
109+
Ident::from_str("field"),
110+
vec![name, field]);
111+
stmts.push(stmt_let_undescore(cx, span, expr));
112+
}
113+
}
114+
}
119115

120116
let expr = cx.expr_method_call(span, builder_expr, Ident::from_str("finish"), vec![]);
121117

src/libsyntax_ext/deriving/generic/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,7 @@ impl<'a> TraitDef<'a> {
15391539
}
15401540
}
15411541

1542+
let is_tuple = if let ast::VariantData::Tuple(..) = struct_def { true } else { false };
15421543
match (just_spans.is_empty(), named_idents.is_empty()) {
15431544
(false, false) => {
15441545
cx.span_bug(self.span,
@@ -1547,9 +1548,10 @@ impl<'a> TraitDef<'a> {
15471548
}
15481549
// named fields
15491550
(_, false) => Named(named_idents),
1550-
// empty structs
1551-
_ if struct_def.is_struct() => Named(named_idents),
1552-
_ => Unnamed(just_spans, struct_def.is_tuple()),
1551+
// unnamed fields
1552+
(false, _) => Unnamed(just_spans, is_tuple),
1553+
// empty
1554+
_ => Named(Vec::new()),
15531555
}
15541556
}
15551557

0 commit comments

Comments
 (0)