@@ -6,7 +6,7 @@ use std::cmp::{max, min, Ordering};
66use regex:: Regex ;
77use rustc_ast:: visit;
88use rustc_ast:: { ast, ptr} ;
9- use rustc_span:: { symbol, BytePos , Span } ;
9+ use rustc_span:: { symbol:: kw , symbol :: Ident , BytePos , Span } ;
1010
1111use crate :: attr:: filter_inline_attrs;
1212use crate :: comment:: {
@@ -291,7 +291,7 @@ impl<'a> FmtVisitor<'a> {
291291 pub ( crate ) fn rewrite_fn_before_block (
292292 & mut self ,
293293 indent : Indent ,
294- ident : symbol :: Ident ,
294+ ident : Ident ,
295295 fn_sig : & FnSig < ' _ > ,
296296 span : Span ,
297297 ) -> Option < ( String , FnBraceStyle ) > {
@@ -315,7 +315,7 @@ impl<'a> FmtVisitor<'a> {
315315 pub ( crate ) fn rewrite_required_fn (
316316 & mut self ,
317317 indent : Indent ,
318- ident : symbol :: Ident ,
318+ ident : Ident ,
319319 sig : & ast:: FnSig ,
320320 generics : & ast:: Generics ,
321321 span : Span ,
@@ -385,18 +385,14 @@ impl<'a> FmtVisitor<'a> {
385385 }
386386
387387 pub ( crate ) fn visit_struct ( & mut self , struct_parts : & StructParts < ' _ > ) {
388- let is_tuple = match struct_parts. def {
389- ast:: VariantData :: Tuple ( ..) => true ,
390- _ => false ,
391- } ;
392388 let rewrite = format_struct ( & self . get_context ( ) , struct_parts, self . block_indent , None )
393- . map ( |s| if is_tuple { s + ";" } else { s } ) ;
389+ . map ( |s| if struct_parts . is_tuple ( ) { s + ";" } else { s } ) ;
394390 self . push_rewrite ( struct_parts. span , rewrite) ;
395391 }
396392
397393 pub ( crate ) fn visit_enum (
398394 & mut self ,
399- ident : symbol :: Ident ,
395+ ident : Ident ,
400396 vis : & ast:: Visibility ,
401397 enum_def : & ast:: EnumDef ,
402398 generics : & ast:: Generics ,
@@ -957,11 +953,27 @@ fn rewrite_trait_ref(
957953 ) )
958954}
959955
956+ enum StructPartsVariantData < ' a > {
957+ Unit ,
958+ Tuple ( & ' a [ ast:: FieldDef ] ) ,
959+ Struct ( & ' a [ ast:: FieldDef ] ) ,
960+ }
961+
962+ impl < ' a > From < & ' a ast:: VariantData > for StructPartsVariantData < ' a > {
963+ fn from ( variant_data : & ' a ast:: VariantData ) -> Self {
964+ match variant_data {
965+ ast:: VariantData :: Unit ( ..) => StructPartsVariantData :: Unit ,
966+ ast:: VariantData :: Tuple ( ref fields, _) => StructPartsVariantData :: Tuple ( fields) ,
967+ ast:: VariantData :: Struct ( ref fields, _) => StructPartsVariantData :: Struct ( fields) ,
968+ }
969+ }
970+ }
971+
960972pub ( crate ) struct StructParts < ' a > {
961973 prefix : & ' a str ,
962- ident : symbol :: Ident ,
974+ ident : Ident ,
963975 vis : & ' a ast:: Visibility ,
964- def : & ' a ast :: VariantData ,
976+ def : StructPartsVariantData < ' a > ,
965977 generics : Option < & ' a ast:: Generics > ,
966978 span : Span ,
967979}
@@ -971,12 +983,16 @@ impl<'a> StructParts<'a> {
971983 format_header ( context, self . prefix , self . ident , self . vis , offset)
972984 }
973985
986+ pub ( crate ) fn is_tuple ( & self ) -> bool {
987+ matches ! ( self . def, StructPartsVariantData :: Tuple ( ..) )
988+ }
989+
974990 pub ( crate ) fn from_variant ( variant : & ' a ast:: Variant ) -> Self {
975991 StructParts {
976992 prefix : "" ,
977993 ident : variant. ident ,
978994 vis : & DEFAULT_VISIBILITY ,
979- def : & variant. data ,
995+ def : StructPartsVariantData :: from ( & variant. data ) ,
980996 generics : None ,
981997 span : variant. span ,
982998 }
@@ -992,11 +1008,28 @@ impl<'a> StructParts<'a> {
9921008 prefix,
9931009 ident : item. ident ,
9941010 vis : & item. vis ,
995- def,
1011+ def : StructPartsVariantData :: from ( def ) ,
9961012 generics : Some ( generics) ,
9971013 span : item. span ,
9981014 }
9991015 }
1016+
1017+ pub ( crate ) fn from_anonymous_type ( ty : & ' a ast:: Ty ) -> Self {
1018+ let ( fields, kw, kw_length) = match ty. kind {
1019+ ast:: TyKind :: AnonymousStruct ( ref fields, _) => ( fields, kw:: Struct , 6 ) ,
1020+ ast:: TyKind :: AnonymousUnion ( ref fields, _) => ( fields, kw:: Union , 5 ) ,
1021+ _ => unreachable ! ( ) ,
1022+ } ;
1023+
1024+ StructParts {
1025+ prefix : "" ,
1026+ ident : Ident :: new ( kw, mk_sp ( ty. span . lo ( ) , ty. span . lo ( ) + BytePos ( kw_length) ) ) ,
1027+ vis : & DEFAULT_VISIBILITY ,
1028+ def : StructPartsVariantData :: Struct ( fields) ,
1029+ generics : None ,
1030+ span : ty. span ,
1031+ }
1032+ }
10001033}
10011034
10021035fn format_struct (
@@ -1005,12 +1038,12 @@ fn format_struct(
10051038 offset : Indent ,
10061039 one_line_width : Option < usize > ,
10071040) -> Option < String > {
1008- match * struct_parts. def {
1009- ast :: VariantData :: Unit ( .. ) => format_unit_struct ( context, struct_parts, offset) ,
1010- ast :: VariantData :: Tuple ( ref fields, _ ) => {
1041+ match struct_parts. def {
1042+ StructPartsVariantData :: Unit => format_unit_struct ( context, struct_parts, offset) ,
1043+ StructPartsVariantData :: Tuple ( fields) => {
10111044 format_tuple_struct ( context, struct_parts, fields, offset)
10121045 }
1013- ast :: VariantData :: Struct ( ref fields, _ ) => {
1046+ StructPartsVariantData :: Struct ( fields) => {
10141047 format_struct_struct ( context, struct_parts, fields, offset, one_line_width)
10151048 }
10161049 }
@@ -1222,7 +1255,7 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
12221255
12231256pub ( crate ) fn format_trait_alias (
12241257 context : & RewriteContext < ' _ > ,
1225- ident : symbol :: Ident ,
1258+ ident : Ident ,
12261259 vis : & ast:: Visibility ,
12271260 generics : & ast:: Generics ,
12281261 generic_bounds : & ast:: GenericBounds ,
@@ -1500,7 +1533,7 @@ fn format_tuple_struct(
15001533fn rewrite_type < R : Rewrite > (
15011534 context : & RewriteContext < ' _ > ,
15021535 indent : Indent ,
1503- ident : symbol :: Ident ,
1536+ ident : Ident ,
15041537 vis : & ast:: Visibility ,
15051538 generics : & ast:: Generics ,
15061539 generic_bounds_opt : Option < & ast:: GenericBounds > ,
@@ -1598,7 +1631,7 @@ fn rewrite_type<R: Rewrite>(
15981631pub ( crate ) fn rewrite_opaque_type (
15991632 context : & RewriteContext < ' _ > ,
16001633 indent : Indent ,
1601- ident : symbol :: Ident ,
1634+ ident : Ident ,
16021635 generic_bounds : & ast:: GenericBounds ,
16031636 generics : & ast:: Generics ,
16041637 vis : & ast:: Visibility ,
@@ -1714,7 +1747,7 @@ pub(crate) fn rewrite_struct_field(
17141747pub ( crate ) struct StaticParts < ' a > {
17151748 prefix : & ' a str ,
17161749 vis : & ' a ast:: Visibility ,
1717- ident : symbol :: Ident ,
1750+ ident : Ident ,
17181751 ty : & ' a ast:: Ty ,
17191752 mutability : ast:: Mutability ,
17201753 expr_opt : Option < & ' a ptr:: P < ast:: Expr > > ,
@@ -1843,7 +1876,7 @@ fn rewrite_static(
18431876}
18441877
18451878pub ( crate ) fn rewrite_type_alias (
1846- ident : symbol :: Ident ,
1879+ ident : Ident ,
18471880 ty_opt : Option < & ptr:: P < ast:: Ty > > ,
18481881 generics : & ast:: Generics ,
18491882 generic_bounds_opt : Option < & ast:: GenericBounds > ,
@@ -1879,7 +1912,7 @@ impl<'a> Rewrite for OpaqueType<'a> {
18791912
18801913pub ( crate ) fn rewrite_opaque_impl_type (
18811914 context : & RewriteContext < ' _ > ,
1882- ident : symbol :: Ident ,
1915+ ident : Ident ,
18831916 generics : & ast:: Generics ,
18841917 generic_bounds : & ast:: GenericBounds ,
18851918 indent : Indent ,
@@ -1903,7 +1936,7 @@ pub(crate) fn rewrite_opaque_impl_type(
19031936}
19041937
19051938pub ( crate ) fn rewrite_associated_impl_type (
1906- ident : symbol :: Ident ,
1939+ ident : Ident ,
19071940 vis : & ast:: Visibility ,
19081941 defaultness : ast:: Defaultness ,
19091942 ty_opt : Option < & ptr:: P < ast:: Ty > > ,
@@ -2125,7 +2158,7 @@ pub(crate) fn span_hi_for_param(context: &RewriteContext<'_>, param: &ast::Param
21252158
21262159pub ( crate ) fn is_named_param ( param : & ast:: Param ) -> bool {
21272160 if let ast:: PatKind :: Ident ( _, ident, _) = param. pat . kind {
2128- ident. name != symbol :: kw:: Empty
2161+ ident. name != kw:: Empty
21292162 } else {
21302163 true
21312164 }
@@ -2142,7 +2175,7 @@ pub(crate) enum FnBraceStyle {
21422175fn rewrite_fn_base (
21432176 context : & RewriteContext < ' _ > ,
21442177 indent : Indent ,
2145- ident : symbol :: Ident ,
2178+ ident : Ident ,
21462179 fn_sig : & FnSig < ' _ > ,
21472180 span : Span ,
21482181 fn_brace_style : FnBraceStyle ,
@@ -2971,7 +3004,7 @@ fn rewrite_comments_before_after_where(
29713004fn format_header (
29723005 context : & RewriteContext < ' _ > ,
29733006 item_name : & str ,
2974- ident : symbol :: Ident ,
3007+ ident : Ident ,
29753008 vis : & ast:: Visibility ,
29763009 offset : Indent ,
29773010) -> String {
0 commit comments