Skip to content

Commit

Permalink
refactor: switch to AssocItem
Browse files Browse the repository at this point in the history
TraitItem/ImplItem were combined to `AssocItem`, along with their
respecitve `*Kind`'s, the `Method` variant was also renamed to `Fn` in
the new `AssocItemKind`

rust-lang/rust#67131
  • Loading branch information
calebcartwright committed Jan 15, 2020
1 parent c73348a commit 297f7db
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
26 changes: 11 additions & 15 deletions rustfmt-core/rustfmt-lib/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ impl<'a> FmtVisitor<'a> {
combine_strs_with_missing_comments(&context, &attrs_str, &variant_body, span, shape, false)
}

fn visit_impl_items(&mut self, items: &[ast::ImplItem]) {
fn visit_impl_items(&mut self, items: &[ast::AssocItem]) {
if self.get_context().config.reorder_impl_items() {
// Create visitor for each items, then reorder them.
let mut buffer = vec![];
Expand All @@ -643,26 +643,22 @@ impl<'a> FmtVisitor<'a> {
self.buffer.clear();
}
// type -> opaque -> const -> macro -> method
use crate::ast::ImplItemKind::*;
fn need_empty_line(a: &ast::ImplItemKind, b: &ast::ImplItemKind) -> bool {
use crate::ast::AssocItemKind::*;
fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
match (a, b) {
(TyAlias(..), TyAlias(..))
| (Const(..), Const(..))
| (OpaqueTy(..), OpaqueTy(..)) => false,
| (Const(..), Const(..)) => false,
_ => true,
}
}

buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) {
(TyAlias(..), TyAlias(..))
| (Const(..), Const(..))
| (Macro(..), Macro(..))
| (OpaqueTy(..), OpaqueTy(..)) => a.ident.as_str().cmp(&b.ident.as_str()),
(Method(..), Method(..)) => a.span.lo().cmp(&b.span.lo()),
| (Macro(..), Macro(..)) => a.ident.as_str().cmp(&b.ident.as_str()),
(Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()),
(TyAlias(..), _) => Ordering::Less,
(_, TyAlias(..)) => Ordering::Greater,
(OpaqueTy(..), _) => Ordering::Less,
(_, OpaqueTy(..)) => Ordering::Greater,
(Const(..), _) => Ordering::Less,
(_, Const(..)) => Ordering::Greater,
(Macro(..), _) => Ordering::Less,
Expand Down Expand Up @@ -822,7 +818,7 @@ pub(crate) fn format_impl(

fn is_impl_single_line(
context: &RewriteContext<'_>,
items: &[ast::ImplItem],
items: &[ast::AssocItem],
result: &str,
where_clause_str: &str,
item: &ast::Item,
Expand Down Expand Up @@ -1729,9 +1725,9 @@ impl<'a> StaticParts<'a> {
}
}

pub(crate) fn from_trait_item(ti: &'a ast::TraitItem) -> Self {
pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
let (ty, expr_opt) = match ti.kind {
ast::TraitItemKind::Const(ref ty, ref expr_opt) => (ty, expr_opt),
ast::AssocItemKind::Const(ref ty, ref expr_opt) => (ty, expr_opt),
_ => unreachable!(),
};
StaticParts {
Expand All @@ -1746,9 +1742,9 @@ impl<'a> StaticParts<'a> {
}
}

pub(crate) fn from_impl_item(ii: &'a ast::ImplItem) -> Self {
pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self {
let (ty, expr) = match ii.kind {
ast::ImplItemKind::Const(ref ty, ref expr) => (ty, expr),
ast::AssocItemKind::Const(ref ty, ref expr) => (ty, expr),
_ => unreachable!(),
};
StaticParts {
Expand Down
3 changes: 1 addition & 2 deletions rustfmt-core/rustfmt-lib/src/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ macro_rules! implement_spanned {
}

// Implement `Spanned` for structs with `attrs` field.
implement_spanned!(ast::AssocItem);
implement_spanned!(ast::Expr);
implement_spanned!(ast::Field);
implement_spanned!(ast::ForeignItem);
implement_spanned!(ast::Item);
implement_spanned!(ast::Local);
implement_spanned!(ast::TraitItem);
implement_spanned!(ast::ImplItem);

impl Spanned for ast::Stmt {
fn span(&self) -> Span {
Expand Down
71 changes: 39 additions & 32 deletions rustfmt-core/rustfmt-lib/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,23 +556,23 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.skip_context = skip_context_saved;
}

pub(crate) fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
pub(crate) fn visit_trait_item(&mut self, ti: &ast::AssocItem) {
skip_out_of_file_lines_range_visitor!(self, ti.span);

if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
self.push_skipped_with_span(ti.attrs.as_slice(), ti.span(), ti.span());
self.push_skipped_with_span(ti.attrs.as_slice(), ti.span, ti.span);
return;
}

match ti.kind {
ast::TraitItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
ast::TraitItemKind::Method(ref sig, None) => {
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
ast::AssocItemKind::Fn(ref sig, None) => {
let indent = self.block_indent;
let rewrite =
self.rewrite_required_fn(indent, ti.ident, sig, &ti.generics, ti.span);
self.push_rewrite(ti.span, rewrite);
}
ast::TraitItemKind::Method(ref sig, Some(ref body)) => {
ast::AssocItemKind::Fn(ref sig, Some(ref body)) => {
let inner_attrs = inner_attributes(&ti.attrs);
self.visit_fn(
visit::FnKind::Method(ti.ident, sig, None, body),
Expand All @@ -583,7 +583,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
Some(&inner_attrs),
);
}
ast::TraitItemKind::Type(ref generic_bounds, ref type_default) => {
ast::AssocItemKind::TyAlias(ref generic_bounds, ref type_default) => {
let rewrite = rewrite_associated_type(
ti.ident,
type_default.as_ref(),
Expand All @@ -594,55 +594,62 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
);
self.push_rewrite(ti.span, rewrite);
}
ast::TraitItemKind::Macro(ref mac) => {
ast::AssocItemKind::Macro(ref mac) => {
self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
}
}
}

pub(crate) fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
pub(crate) fn visit_impl_item(&mut self, ii: &ast::AssocItem) {
skip_out_of_file_lines_range_visitor!(self, ii.span);

if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
self.push_skipped_with_span(ii.attrs.as_slice(), ii.span(), ii.span());
self.push_skipped_with_span(ii.attrs.as_slice(), ii.span, ii.span);
return;
}

match ii.kind {
ast::ImplItemKind::Method(ref sig, ref body) => {
ast::AssocItemKind::Fn(ref sig, Some(ref body)) => {
let inner_attrs = inner_attributes(&ii.attrs);
self.visit_fn(
visit::FnKind::Method(ii.ident, sig, Some(&ii.vis), body),
visit::FnKind::Fn(ii.ident, sig, Some(&ii.vis), body),
&ii.generics,
&sig.decl,
ii.span,
ii.defaultness,
Some(&inner_attrs),
);
}
ast::ImplItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
ast::ImplItemKind::TyAlias(ref ty) => {
let rewrite = rewrite_associated_impl_type(
ii.ident,
ii.defaultness,
Some(ty),
&ii.generics,
&self.get_context(),
self.block_indent,
);
self.push_rewrite(ii.span, rewrite);
}
ast::ImplItemKind::OpaqueTy(ref generic_bounds) => {
let rewrite = rewrite_opaque_impl_type(
&self.get_context(),
ii.ident,
&ii.generics,
generic_bounds,
self.block_indent,
);
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
ast::AssocItemKind::TyAlias(ref generic_bounds, ref ty) => {
let rewrite_associated = || {
rewrite_associated_impl_type(
ii.ident,
ii.defaultness,
ty.as_ref(),
&ii.generics,
&self.get_context(),
self.block_indent,
)
};
let rewrite = match ty {
None => rewrite_associated(),
Some(ty) => match ty.kind.opaque_top_hack() {
Some(_) => {
rewrite_opaque_impl_type(
&self.get_context(),
ii.ident,
&ii.generics,
generic_bounds,
self.block_indent,
)
},
None => rewrite_associated()
}
};
self.push_rewrite(ii.span, rewrite);
}
ast::ImplItemKind::Macro(ref mac) => {
ast::AssocItemKind::Macro(ref mac) => {
self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
}
}
Expand Down

0 comments on commit 297f7db

Please sign in to comment.