@@ -6,7 +6,7 @@ use std::cmp::{max, min, Ordering};
6
6
use regex:: Regex ;
7
7
use rustc_ast:: visit;
8
8
use rustc_ast:: { ast, ptr} ;
9
- use rustc_span:: { symbol, BytePos , Span } ;
9
+ use rustc_span:: { symbol:: kw , symbol :: Ident , BytePos , Span } ;
10
10
11
11
use crate :: attr:: filter_inline_attrs;
12
12
use crate :: comment:: {
@@ -291,7 +291,7 @@ impl<'a> FmtVisitor<'a> {
291
291
pub ( crate ) fn rewrite_fn_before_block (
292
292
& mut self ,
293
293
indent : Indent ,
294
- ident : symbol :: Ident ,
294
+ ident : Ident ,
295
295
fn_sig : & FnSig < ' _ > ,
296
296
span : Span ,
297
297
) -> Option < ( String , FnBraceStyle ) > {
@@ -315,7 +315,7 @@ impl<'a> FmtVisitor<'a> {
315
315
pub ( crate ) fn rewrite_required_fn (
316
316
& mut self ,
317
317
indent : Indent ,
318
- ident : symbol :: Ident ,
318
+ ident : Ident ,
319
319
sig : & ast:: FnSig ,
320
320
generics : & ast:: Generics ,
321
321
span : Span ,
@@ -385,18 +385,14 @@ impl<'a> FmtVisitor<'a> {
385
385
}
386
386
387
387
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
- } ;
392
388
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 } ) ;
394
390
self . push_rewrite ( struct_parts. span , rewrite) ;
395
391
}
396
392
397
393
pub ( crate ) fn visit_enum (
398
394
& mut self ,
399
- ident : symbol :: Ident ,
395
+ ident : Ident ,
400
396
vis : & ast:: Visibility ,
401
397
enum_def : & ast:: EnumDef ,
402
398
generics : & ast:: Generics ,
@@ -957,11 +953,27 @@ fn rewrite_trait_ref(
957
953
) )
958
954
}
959
955
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
+
960
972
pub ( crate ) struct StructParts < ' a > {
961
973
prefix : & ' a str ,
962
- ident : symbol :: Ident ,
974
+ ident : Ident ,
963
975
vis : & ' a ast:: Visibility ,
964
- def : & ' a ast :: VariantData ,
976
+ def : StructPartsVariantData < ' a > ,
965
977
generics : Option < & ' a ast:: Generics > ,
966
978
span : Span ,
967
979
}
@@ -971,12 +983,16 @@ impl<'a> StructParts<'a> {
971
983
format_header ( context, self . prefix , self . ident , self . vis , offset)
972
984
}
973
985
986
+ pub ( crate ) fn is_tuple ( & self ) -> bool {
987
+ matches ! ( self . def, StructPartsVariantData :: Tuple ( ..) )
988
+ }
989
+
974
990
pub ( crate ) fn from_variant ( variant : & ' a ast:: Variant ) -> Self {
975
991
StructParts {
976
992
prefix : "" ,
977
993
ident : variant. ident ,
978
994
vis : & DEFAULT_VISIBILITY ,
979
- def : & variant. data ,
995
+ def : StructPartsVariantData :: from ( & variant. data ) ,
980
996
generics : None ,
981
997
span : variant. span ,
982
998
}
@@ -992,11 +1008,28 @@ impl<'a> StructParts<'a> {
992
1008
prefix,
993
1009
ident : item. ident ,
994
1010
vis : & item. vis ,
995
- def,
1011
+ def : StructPartsVariantData :: from ( def ) ,
996
1012
generics : Some ( generics) ,
997
1013
span : item. span ,
998
1014
}
999
1015
}
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
+ }
1000
1033
}
1001
1034
1002
1035
fn format_struct (
@@ -1005,12 +1038,12 @@ fn format_struct(
1005
1038
offset : Indent ,
1006
1039
one_line_width : Option < usize > ,
1007
1040
) -> 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) => {
1011
1044
format_tuple_struct ( context, struct_parts, fields, offset)
1012
1045
}
1013
- ast :: VariantData :: Struct ( ref fields, _ ) => {
1046
+ StructPartsVariantData :: Struct ( fields) => {
1014
1047
format_struct_struct ( context, struct_parts, fields, offset, one_line_width)
1015
1048
}
1016
1049
}
@@ -1222,7 +1255,7 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
1222
1255
1223
1256
pub ( crate ) fn format_trait_alias (
1224
1257
context : & RewriteContext < ' _ > ,
1225
- ident : symbol :: Ident ,
1258
+ ident : Ident ,
1226
1259
vis : & ast:: Visibility ,
1227
1260
generics : & ast:: Generics ,
1228
1261
generic_bounds : & ast:: GenericBounds ,
@@ -1500,7 +1533,7 @@ fn format_tuple_struct(
1500
1533
fn rewrite_type < R : Rewrite > (
1501
1534
context : & RewriteContext < ' _ > ,
1502
1535
indent : Indent ,
1503
- ident : symbol :: Ident ,
1536
+ ident : Ident ,
1504
1537
vis : & ast:: Visibility ,
1505
1538
generics : & ast:: Generics ,
1506
1539
generic_bounds_opt : Option < & ast:: GenericBounds > ,
@@ -1598,7 +1631,7 @@ fn rewrite_type<R: Rewrite>(
1598
1631
pub ( crate ) fn rewrite_opaque_type (
1599
1632
context : & RewriteContext < ' _ > ,
1600
1633
indent : Indent ,
1601
- ident : symbol :: Ident ,
1634
+ ident : Ident ,
1602
1635
generic_bounds : & ast:: GenericBounds ,
1603
1636
generics : & ast:: Generics ,
1604
1637
vis : & ast:: Visibility ,
@@ -1714,7 +1747,7 @@ pub(crate) fn rewrite_struct_field(
1714
1747
pub ( crate ) struct StaticParts < ' a > {
1715
1748
prefix : & ' a str ,
1716
1749
vis : & ' a ast:: Visibility ,
1717
- ident : symbol :: Ident ,
1750
+ ident : Ident ,
1718
1751
ty : & ' a ast:: Ty ,
1719
1752
mutability : ast:: Mutability ,
1720
1753
expr_opt : Option < & ' a ptr:: P < ast:: Expr > > ,
@@ -1843,7 +1876,7 @@ fn rewrite_static(
1843
1876
}
1844
1877
1845
1878
pub ( crate ) fn rewrite_type_alias (
1846
- ident : symbol :: Ident ,
1879
+ ident : Ident ,
1847
1880
ty_opt : Option < & ptr:: P < ast:: Ty > > ,
1848
1881
generics : & ast:: Generics ,
1849
1882
generic_bounds_opt : Option < & ast:: GenericBounds > ,
@@ -1879,7 +1912,7 @@ impl<'a> Rewrite for OpaqueType<'a> {
1879
1912
1880
1913
pub ( crate ) fn rewrite_opaque_impl_type (
1881
1914
context : & RewriteContext < ' _ > ,
1882
- ident : symbol :: Ident ,
1915
+ ident : Ident ,
1883
1916
generics : & ast:: Generics ,
1884
1917
generic_bounds : & ast:: GenericBounds ,
1885
1918
indent : Indent ,
@@ -1903,7 +1936,7 @@ pub(crate) fn rewrite_opaque_impl_type(
1903
1936
}
1904
1937
1905
1938
pub ( crate ) fn rewrite_associated_impl_type (
1906
- ident : symbol :: Ident ,
1939
+ ident : Ident ,
1907
1940
vis : & ast:: Visibility ,
1908
1941
defaultness : ast:: Defaultness ,
1909
1942
ty_opt : Option < & ptr:: P < ast:: Ty > > ,
@@ -2125,7 +2158,7 @@ pub(crate) fn span_hi_for_param(context: &RewriteContext<'_>, param: &ast::Param
2125
2158
2126
2159
pub ( crate ) fn is_named_param ( param : & ast:: Param ) -> bool {
2127
2160
if let ast:: PatKind :: Ident ( _, ident, _) = param. pat . kind {
2128
- ident. name != symbol :: kw:: Empty
2161
+ ident. name != kw:: Empty
2129
2162
} else {
2130
2163
true
2131
2164
}
@@ -2142,7 +2175,7 @@ pub(crate) enum FnBraceStyle {
2142
2175
fn rewrite_fn_base (
2143
2176
context : & RewriteContext < ' _ > ,
2144
2177
indent : Indent ,
2145
- ident : symbol :: Ident ,
2178
+ ident : Ident ,
2146
2179
fn_sig : & FnSig < ' _ > ,
2147
2180
span : Span ,
2148
2181
fn_brace_style : FnBraceStyle ,
@@ -2971,7 +3004,7 @@ fn rewrite_comments_before_after_where(
2971
3004
fn format_header (
2972
3005
context : & RewriteContext < ' _ > ,
2973
3006
item_name : & str ,
2974
- ident : symbol :: Ident ,
3007
+ ident : Ident ,
2975
3008
vis : & ast:: Visibility ,
2976
3009
offset : Indent ,
2977
3010
) -> String {
0 commit comments