From 939a6c58202f775a63e46fbde4e0d6b3b04cf8aa Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 24 Dec 2017 00:29:59 +0900 Subject: [PATCH] Get rid of GenericsArg wrapper type `GenericsArg` is basically identical to `ast::GenericParam`. Just use the latter. --- src/items.rs | 57 +++++++++++++------------------------------------- src/spanned.rs | 9 ++++++++ src/utils.rs | 13 ------------ 3 files changed, 23 insertions(+), 56 deletions(-) diff --git a/src/items.rs b/src/items.rs index 35dd5c39d38..d09d95699b7 100644 --- a/src/items.rs +++ b/src/items.rs @@ -30,9 +30,9 @@ use rewrite::{Rewrite, RewriteContext}; use shape::{Indent, Shape}; use spanned::Spanned; use types::join_bounds; -use utils::{colon_spaces, contains_skip, end_typaram, first_line_width, format_abi, - format_constness, format_defaultness, format_mutability, format_unsafety, - format_visibility, is_attributes_extendable, last_line_contains_single_line_comment, +use utils::{colon_spaces, contains_skip, first_line_width, format_abi, format_constness, + format_defaultness, format_mutability, format_unsafety, format_visibility, + is_attributes_extendable, last_line_contains_single_line_comment, last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, starts_with_newline, stmt_expr, trim_newlines, trimmed_last_line_width}; use vertical::rewrite_with_alignment; @@ -1871,12 +1871,8 @@ fn rewrite_fn_base( .generics .params .iter() - .filter_map(|p| match p { - &ast::GenericParam::Type(ref t) => Some(t), - _ => None, - }) .last() - .map_or(lo_after_visibility, |tp| end_typaram(tp)); + .map_or(lo_after_visibility, |param| param.span().hi()); let args_end = if fd.inputs.is_empty() { context .codemap @@ -2346,47 +2342,13 @@ fn rewrite_generics_inner( // FIXME: convert bounds to where clauses where they get too big or if // there is a where clause at all. - // Wrapper type - enum GenericsArg<'a> { - Lifetime(&'a ast::LifetimeDef), - TyParam(&'a ast::TyParam), - } - impl<'a> Rewrite for GenericsArg<'a> { - fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - match *self { - GenericsArg::Lifetime(lifetime) => lifetime.rewrite(context, shape), - GenericsArg::TyParam(ty) => ty.rewrite(context, shape), - } - } - } - impl<'a> Spanned for GenericsArg<'a> { - fn span(&self) -> Span { - match *self { - GenericsArg::Lifetime(lifetime) => lifetime.span(), - GenericsArg::TyParam(ty) => ty.span(), - } - } - } - if generics.params.is_empty() { return Some(String::new()); } - let generics_args = generics - .params - .iter() - .filter_map(|p| match p { - &ast::GenericParam::Lifetime(ref l) => Some(l), - _ => None, - }) - .map(|lt| GenericsArg::Lifetime(lt)) - .chain(generics.params.iter().filter_map(|ty| match ty { - &ast::GenericParam::Type(ref ty) => Some(GenericsArg::TyParam(ty)), - _ => None, - })); let items = itemize_list( context.codemap, - generics_args, + generics.params.iter(), ">", ",", |arg| arg.span().lo(), @@ -2868,3 +2830,12 @@ impl Rewrite for ast::ForeignItem { ) } } + +impl Rewrite for ast::GenericParam { + fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { + match *self { + ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.rewrite(context, shape), + ast::GenericParam::Type(ref ty) => ty.rewrite(context, shape), + } + } +} diff --git a/src/spanned.rs b/src/spanned.rs index 7181512273e..eb36a117428 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -105,6 +105,15 @@ impl Spanned for ast::Arg { } } +impl Spanned for ast::GenericParam { + fn span(&self) -> Span { + match *self { + ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.span(), + ast::GenericParam::Type(ref ty) => ty.span(), + } + } +} + impl Spanned for ast::StructField { fn span(&self) -> Span { span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi()) diff --git a/src/utils.rs b/src/utils.rs index 7d3fcf73d56..1bad8aecdf4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -213,19 +213,6 @@ pub fn contains_skip(attrs: &[Attribute]) -> bool { .any(|a| a.meta().map_or(false, |a| is_skip(&a))) } -// Find the end of a TyParam -#[inline] -pub fn end_typaram(typaram: &ast::TyParam) -> BytePos { - typaram - .bounds - .last() - .map_or(typaram.span, |bound| match *bound { - ast::RegionTyParamBound(ref lt) => lt.span, - ast::TraitTyParamBound(ref prt, _) => prt.span, - }) - .hi() -} - #[inline] pub fn semicolon_for_expr(context: &RewriteContext, expr: &ast::Expr) -> bool { match expr.node {