From 297f7db2a9df4e46d2b48714dd00e734dc2cbca6 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Tue, 14 Jan 2020 19:00:44 -0600 Subject: [PATCH] refactor: switch to AssocItem TraitItem/ImplItem were combined to `AssocItem`, along with their respecitve `*Kind`'s, the `Method` variant was also renamed to `Fn` in the new `AssocItemKind` https://github.com/rust-lang/rust/pull/67131 --- rustfmt-core/rustfmt-lib/src/items.rs | 26 ++++----- rustfmt-core/rustfmt-lib/src/spanned.rs | 3 +- rustfmt-core/rustfmt-lib/src/visitor.rs | 71 ++++++++++++++----------- 3 files changed, 51 insertions(+), 49 deletions(-) diff --git a/rustfmt-core/rustfmt-lib/src/items.rs b/rustfmt-core/rustfmt-lib/src/items.rs index 9bab07cdcea..b62a8ead212 100644 --- a/rustfmt-core/rustfmt-lib/src/items.rs +++ b/rustfmt-core/rustfmt-lib/src/items.rs @@ -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![]; @@ -643,12 +643,11 @@ 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, } } @@ -656,13 +655,10 @@ impl<'a> FmtVisitor<'a> { 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, @@ -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, @@ -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 { @@ -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 { diff --git a/rustfmt-core/rustfmt-lib/src/spanned.rs b/rustfmt-core/rustfmt-lib/src/spanned.rs index d49f59440ff..be294efaac2 100644 --- a/rustfmt-core/rustfmt-lib/src/spanned.rs +++ b/rustfmt-core/rustfmt-lib/src/spanned.rs @@ -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 { diff --git a/rustfmt-core/rustfmt-lib/src/visitor.rs b/rustfmt-core/rustfmt-lib/src/visitor.rs index 174320961e5..0455d075bea 100644 --- a/rustfmt-core/rustfmt-lib/src/visitor.rs +++ b/rustfmt-core/rustfmt-lib/src/visitor.rs @@ -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), @@ -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(), @@ -594,25 +594,25 @@ 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, @@ -620,29 +620,36 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { 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); } }