Skip to content

Commit bf3c52f

Browse files
committed
ast: Fix naming conventions in AST structures
TraitKind -> Trait TyAliasKind -> TyAlias ImplKind -> Impl FnKind -> Fn All `*Kind`s in AST are supposed to be enums. Tuple structs are converted to braced structs for the types above, and fields are reordered in syntactic order. Also, mutable AST visitor now correctly visit spans in defaultness, unsafety, impl polarity and constness.
1 parent 051e2b4 commit bf3c52f

File tree

2 files changed

+86
-56
lines changed

2 files changed

+86
-56
lines changed

Diff for: src/items.rs

+39-25
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ impl<'a> FmtVisitor<'a> {
622622
fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
623623
match (a, b) {
624624
(TyAlias(lty), TyAlias(rty))
625-
if both_type(&lty.3, &rty.3) || both_opaque(&lty.3, &rty.3) =>
625+
if both_type(&lty.ty, &rty.ty) || both_opaque(&lty.ty, &rty.ty) =>
626626
{
627627
false
628628
}
@@ -633,16 +633,16 @@ impl<'a> FmtVisitor<'a> {
633633

634634
buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) {
635635
(TyAlias(lty), TyAlias(rty))
636-
if both_type(&lty.3, &rty.3) || both_opaque(&lty.3, &rty.3) =>
636+
if both_type(&lty.ty, &rty.ty) || both_opaque(&lty.ty, &rty.ty) =>
637637
{
638638
a.ident.as_str().cmp(&b.ident.as_str())
639639
}
640640
(Const(..), Const(..)) | (MacCall(..), MacCall(..)) => {
641641
a.ident.as_str().cmp(&b.ident.as_str())
642642
}
643643
(Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()),
644-
(TyAlias(ty), _) if is_type(&ty.3) => Ordering::Less,
645-
(_, TyAlias(ty)) if is_type(&ty.3) => Ordering::Greater,
644+
(TyAlias(ty), _) if is_type(&ty.ty) => Ordering::Less,
645+
(_, TyAlias(ty)) if is_type(&ty.ty) => Ordering::Greater,
646646
(TyAlias(..), _) => Ordering::Less,
647647
(_, TyAlias(..)) => Ordering::Greater,
648648
(Const(..), _) => Ordering::Less,
@@ -679,7 +679,7 @@ pub(crate) fn format_impl(
679679
offset: Indent,
680680
) -> Option<String> {
681681
if let ast::ItemKind::Impl(impl_kind) = &item.kind {
682-
let ast::ImplKind {
682+
let ast::Impl {
683683
ref generics,
684684
ref self_ty,
685685
ref items,
@@ -833,7 +833,7 @@ fn format_impl_ref_and_type(
833833
offset: Indent,
834834
) -> Option<String> {
835835
if let ast::ItemKind::Impl(impl_kind) = &item.kind {
836-
let ast::ImplKind {
836+
let ast::Impl {
837837
unsafety,
838838
polarity,
839839
defaultness,
@@ -1029,8 +1029,13 @@ pub(crate) fn format_trait(
10291029
offset: Indent,
10301030
) -> Option<String> {
10311031
if let ast::ItemKind::Trait(trait_kind) = &item.kind {
1032-
let ast::TraitKind(is_auto, unsafety, ref generics, ref generic_bounds, ref trait_items) =
1033-
**trait_kind;
1032+
let ast::Trait {
1033+
is_auto,
1034+
unsafety,
1035+
ref generics,
1036+
ref bounds,
1037+
ref items,
1038+
} = **trait_kind;
10341039
let mut result = String::with_capacity(128);
10351040
let header = format!(
10361041
"{}{}{}trait ",
@@ -1048,11 +1053,11 @@ pub(crate) fn format_trait(
10481053
result.push_str(&generics_str);
10491054

10501055
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1051-
if !generic_bounds.is_empty() {
1056+
if !bounds.is_empty() {
10521057
let ident_hi = context
10531058
.snippet_provider
10541059
.span_after(item.span, &item.ident.as_str());
1055-
let bound_hi = generic_bounds.last().unwrap().span().hi();
1060+
let bound_hi = bounds.last().unwrap().span().hi();
10561061
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
10571062
if contains_comment(snippet) {
10581063
return None;
@@ -1061,7 +1066,7 @@ pub(crate) fn format_trait(
10611066
result = rewrite_assign_rhs_with(
10621067
context,
10631068
result + ":",
1064-
generic_bounds,
1069+
bounds,
10651070
shape,
10661071
RhsTactics::ForceNextLineWithoutIndent,
10671072
)?;
@@ -1072,10 +1077,10 @@ pub(crate) fn format_trait(
10721077
let where_on_new_line = context.config.indent_style() != IndentStyle::Block;
10731078

10741079
let where_budget = context.budget(last_line_width(&result));
1075-
let pos_before_where = if generic_bounds.is_empty() {
1080+
let pos_before_where = if bounds.is_empty() {
10761081
generics.where_clause.span.lo()
10771082
} else {
1078-
generic_bounds[generic_bounds.len() - 1].span().hi()
1083+
bounds[bounds.len() - 1].span().hi()
10791084
};
10801085
let option = WhereClauseOption::snuggled(&generics_str);
10811086
let where_clause_str = rewrite_where_clause(
@@ -1134,7 +1139,7 @@ pub(crate) fn format_trait(
11341139
BraceStyle::PreferSameLine => result.push(' '),
11351140
BraceStyle::SameLineWhere => {
11361141
if result.contains('\n')
1137-
|| (!generics.where_clause.predicates.is_empty() && !trait_items.is_empty())
1142+
|| (!generics.where_clause.predicates.is_empty() && !items.is_empty())
11381143
{
11391144
result.push_str(&offset.to_string_with_newline(context.config));
11401145
} else {
@@ -1149,12 +1154,12 @@ pub(crate) fn format_trait(
11491154
let open_pos = snippet.find_uncommented("{")? + 1;
11501155
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
11511156

1152-
if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
1157+
if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
11531158
let mut visitor = FmtVisitor::from_context(context);
11541159
visitor.block_indent = offset.block_only().block_indent(context.config);
11551160
visitor.last_pos = block_span.lo() + BytePos(open_pos as u32);
11561161

1157-
for item in trait_items {
1162+
for item in items {
11581163
visitor.visit_trait_item(item);
11591164
}
11601165

@@ -3125,17 +3130,22 @@ impl Rewrite for ast::ForeignItem {
31253130

31263131
let item_str = match self.kind {
31273132
ast::ForeignItemKind::Fn(ref fn_kind) => {
3128-
let ast::FnKind(defaultness, ref fn_sig, ref generics, ref block) = **fn_kind;
3129-
if let Some(ref body) = block {
3133+
let ast::Fn {
3134+
defaultness,
3135+
ref sig,
3136+
ref generics,
3137+
ref body,
3138+
} = **fn_kind;
3139+
if let Some(ref body) = body {
31303140
let mut visitor = FmtVisitor::from_context(context);
31313141
visitor.block_indent = shape.indent;
31323142
visitor.last_pos = self.span.lo();
31333143
let inner_attrs = inner_attributes(&self.attrs);
31343144
let fn_ctxt = visit::FnCtxt::Foreign;
31353145
visitor.visit_fn(
3136-
visit::FnKind::Fn(fn_ctxt, self.ident, &fn_sig, &self.vis, Some(body)),
3146+
visit::FnKind::Fn(fn_ctxt, self.ident, &sig, &self.vis, Some(body)),
31373147
generics,
3138-
&fn_sig.decl,
3148+
&sig.decl,
31393149
self.span,
31403150
defaultness,
31413151
Some(&inner_attrs),
@@ -3146,7 +3156,7 @@ impl Rewrite for ast::ForeignItem {
31463156
context,
31473157
shape.indent,
31483158
self.ident,
3149-
&FnSig::from_method_sig(&fn_sig, generics, &self.vis),
3159+
&FnSig::from_method_sig(&sig, generics, &self.vis),
31503160
span,
31513161
FnBraceStyle::None,
31523162
)
@@ -3168,16 +3178,20 @@ impl Rewrite for ast::ForeignItem {
31683178
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
31693179
}
31703180
ast::ForeignItemKind::TyAlias(ref ty_alias_kind) => {
3171-
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
3172-
**ty_alias_kind;
3181+
let ast::TyAlias {
3182+
ref generics,
3183+
ref bounds,
3184+
ref ty,
3185+
..
3186+
} = **ty_alias_kind;
31733187
rewrite_type(
31743188
&context,
31753189
shape.indent,
31763190
self.ident,
31773191
&self.vis,
31783192
generics,
3179-
Some(generic_bounds),
3180-
type_default.as_ref(),
3193+
Some(bounds),
3194+
ty.as_ref(),
31813195
self.span,
31823196
)
31833197
}

Diff for: src/visitor.rs

+47-31
Original file line numberDiff line numberDiff line change
@@ -540,44 +540,41 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
540540
self.visit_static(&StaticParts::from_item(item));
541541
}
542542
ast::ItemKind::Fn(ref fn_kind) => {
543-
let ast::FnKind(defaultness, ref fn_signature, ref generics, ref block) =
544-
**fn_kind;
545-
if let Some(ref body) = block {
543+
let ast::Fn {
544+
defaultness,
545+
ref sig,
546+
ref generics,
547+
ref body,
548+
} = **fn_kind;
549+
if let Some(ref body) = body {
546550
let inner_attrs = inner_attributes(&item.attrs);
547-
let fn_ctxt = match fn_signature.header.ext {
551+
let fn_ctxt = match sig.header.ext {
548552
ast::Extern::None => visit::FnCtxt::Free,
549553
_ => visit::FnCtxt::Foreign,
550554
};
551555
self.visit_fn(
552-
visit::FnKind::Fn(
553-
fn_ctxt,
554-
item.ident,
555-
&fn_signature,
556-
&item.vis,
557-
Some(body),
558-
),
556+
visit::FnKind::Fn(fn_ctxt, item.ident, &sig, &item.vis, Some(body)),
559557
generics,
560-
&fn_signature.decl,
558+
&sig.decl,
561559
item.span,
562560
defaultness,
563561
Some(&inner_attrs),
564562
)
565563
} else {
566564
let indent = self.block_indent;
567565
let rewrite = self.rewrite_required_fn(
568-
indent,
569-
item.ident,
570-
&fn_signature,
571-
&item.vis,
572-
generics,
573-
item.span,
566+
indent, item.ident, &sig, &item.vis, generics, item.span,
574567
);
575568
self.push_rewrite(item.span, rewrite);
576569
}
577570
}
578571
ast::ItemKind::TyAlias(ref alias_kind) => {
579-
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref ty) =
580-
**alias_kind;
572+
let ast::TyAlias {
573+
ref generics,
574+
ref bounds,
575+
ref ty,
576+
..
577+
} = **alias_kind;
581578
match ty {
582579
Some(ty) => {
583580
let rewrite = rewrite_type(
@@ -586,7 +583,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
586583
item.ident,
587584
&item.vis,
588585
generics,
589-
Some(generic_bounds),
586+
Some(bounds),
590587
Some(&*ty),
591588
item.span,
592589
);
@@ -597,7 +594,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
597594
&self.get_context(),
598595
self.block_indent,
599596
item.ident,
600-
generic_bounds,
597+
bounds,
601598
generics,
602599
&item.vis,
603600
item.span,
@@ -639,8 +636,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
639636
match ti.kind {
640637
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
641638
ast::AssocItemKind::Fn(ref fn_kind) => {
642-
let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind;
643-
if let Some(ref body) = block {
639+
let ast::Fn {
640+
defaultness,
641+
ref sig,
642+
ref generics,
643+
ref body,
644+
} = **fn_kind;
645+
if let Some(ref body) = body {
644646
let inner_attrs = inner_attributes(&ti.attrs);
645647
let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Trait);
646648
self.visit_fn(
@@ -659,16 +661,20 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
659661
}
660662
}
661663
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
662-
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
663-
**ty_alias_kind;
664+
let ast::TyAlias {
665+
ref generics,
666+
ref bounds,
667+
ref ty,
668+
..
669+
} = **ty_alias_kind;
664670
let rewrite = rewrite_type(
665671
&self.get_context(),
666672
self.block_indent,
667673
ti.ident,
668674
&ti.vis,
669675
generics,
670-
Some(generic_bounds),
671-
type_default.as_ref(),
676+
Some(bounds),
677+
ty.as_ref(),
672678
ti.span,
673679
);
674680
self.push_rewrite(ti.span, rewrite);
@@ -689,8 +695,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
689695

690696
match ii.kind {
691697
ast::AssocItemKind::Fn(ref fn_kind) => {
692-
let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind;
693-
if let Some(ref body) = block {
698+
let ast::Fn {
699+
defaultness,
700+
ref sig,
701+
ref generics,
702+
ref body,
703+
} = **fn_kind;
704+
if let Some(ref body) = body {
694705
let inner_attrs = inner_attributes(&ii.attrs);
695706
let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Impl);
696707
self.visit_fn(
@@ -710,7 +721,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
710721
}
711722
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
712723
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
713-
let ast::TyAliasKind(defaultness, ref generics, _, ref ty) = **ty_alias_kind;
724+
let ast::TyAlias {
725+
defaultness,
726+
ref generics,
727+
ref ty,
728+
..
729+
} = **ty_alias_kind;
714730
self.push_rewrite(
715731
ii.span,
716732
rewrite_impl_type(

0 commit comments

Comments
 (0)