diff options
| author | Marcus Klaas <mail@marcusklaas.nl> | 2015-12-06 01:11:26 +0100 |
|---|---|---|
| committer | Marcus Klaas <mail@marcusklaas.nl> | 2015-12-06 01:11:26 +0100 |
| commit | 97e4e7e5ba2010fe98c1775e96c6444a68e3cb80 (patch) | |
| tree | 52bc78ba3f5e1e3b6d55b4bbc268ae17013dcd60 /src | |
| parent | 22353ca8c74feabbec005b6de77ce421fe0871a4 (diff) | |
Fixed named arguments in bare function types
Diffstat (limited to 'src')
| -rw-r--r-- | src/expr.rs | 26 | ||||
| -rw-r--r-- | src/items.rs | 2 | ||||
| -rw-r--r-- | src/lib.rs | 12 | ||||
| -rw-r--r-- | src/patterns.rs | 8 | ||||
| -rw-r--r-- | src/types.rs | 23 |
5 files changed, 53 insertions, 18 deletions
diff --git a/src/expr.rs b/src/expr.rs index 78c3a82e223..350b9c53a25 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -11,6 +11,8 @@ use std::cmp::Ordering; use std::borrow::Borrow; use std::mem::swap; +use std::ops::Deref; +use std::iter::ExactSizeIterator; use {Indent, Spanned}; use rewrite::{Rewrite, RewriteContext}; @@ -75,7 +77,11 @@ impl Rewrite for ast::Expr { offset) } ast::Expr_::ExprTup(ref items) => { - rewrite_tuple(context, items, self.span, width, offset) + rewrite_tuple(context, + items.iter().map(|x| &**x), + self.span, + width, + offset) } ast::Expr_::ExprWhile(ref cond, ref block, label) => { Loop::new_while(None, cond, block, label).rewrite(context, width, offset) @@ -960,7 +966,7 @@ impl Rewrite for ast::Arm { let budget = context.config.max_width - line_start - comma.len() - 4; let offset = Indent::new(offset.block_indent, line_start + 4 - offset.block_indent); let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget); - let is_block = if let ast::ExprBlock(ref block) = body.node { + let is_block = if let ast::ExprBlock(..) = body.node { true } else { false @@ -1431,25 +1437,27 @@ fn rewrite_field(context: &RewriteContext, expr.map(|s| format!("{}: {}", name, s)) } -pub fn rewrite_tuple<'a, R>(context: &RewriteContext, - items: &'a [ptr::P<R>], +pub fn rewrite_tuple<'a, I>(context: &RewriteContext, + mut items: I, span: Span, width: usize, offset: Indent) -> Option<String> - where R: Rewrite + Spanned + 'a + where I: ExactSizeIterator, + <I as Iterator>::Item: Deref, + <I::Item as Deref>::Target: Rewrite + Spanned + 'a { - debug!("rewrite_tuple_lit: width: {}, offset: {:?}", width, offset); let indent = offset + 1; // In case of length 1, need a trailing comma if items.len() == 1 { // 3 = "(" + ",)" let budget = try_opt!(width.checked_sub(3)); - return items[0].rewrite(context, budget, indent).map(|s| format!("({},)", s)); + return items.next().unwrap().rewrite(context, budget, indent).map(|s| format!("({},)", s)); } + let list_lo = span_after(span, "(", context.codemap); let items = itemize_list(context.codemap, - items.iter(), + items, ")", |item| item.span().lo, |item| item.span().hi, @@ -1460,7 +1468,7 @@ pub fn rewrite_tuple<'a, R>(context: &RewriteContext, 1)); item.rewrite(context, inner_width, indent) }, - span.lo + BytePos(1), // Remove parens + list_lo, span.hi - BytePos(1)); let budget = try_opt!(width.checked_sub(2)); let list_str = try_opt!(format_fn_args(items, budget, indent, context.config)); diff --git a/src/items.rs b/src/items.rs index 6af01bd1af1..3e08abd461a 100644 --- a/src/items.rs +++ b/src/items.rs @@ -968,7 +968,7 @@ pub fn span_hi_for_arg(arg: &ast::Arg) -> BytePos { } } -fn is_named_arg(arg: &ast::Arg) -> bool { +pub fn is_named_arg(arg: &ast::Arg) -> bool { if let ast::Pat_::PatIdent(_, ident, _) = arg.pat.node { ident.node != token::special_idents::invalid } else { diff --git a/src/lib.rs b/src/lib.rs index ad3e3b27e2e..86ab1a73022 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ extern crate diff; extern crate term; use syntax::ast; -use syntax::codemap::Span; +use syntax::codemap::{mk_sp, Span}; use syntax::diagnostic::{EmitterWriter, Handler}; use syntax::parse::{self, ParseSess}; @@ -88,6 +88,16 @@ impl Spanned for ast::Ty { } } +impl Spanned for ast::Arg { + fn span(&self) -> Span { + if items::is_named_arg(self) { + mk_sp(self.pat.span.lo, self.ty.span.hi) + } else { + self.ty.span + } + } +} + #[derive(Copy, Clone, Debug)] pub struct Indent { // Width of the block indent, in characters. Must be a multiple of diff --git a/src/patterns.rs b/src/patterns.rs index af0bd827751..fdc418733fb 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -48,7 +48,13 @@ impl Rewrite for Pat { let prefix = format!("&{}", format_mutability(mutability)); rewrite_unary_prefix(context, &prefix, &**pat, width, offset) } - Pat_::PatTup(ref items) => rewrite_tuple(context, items, self.span, width, offset), + Pat_::PatTup(ref items) => { + rewrite_tuple(context, + items.iter().map(|x| &**x), + self.span, + width, + offset) + } Pat_::PatEnum(ref path, Some(ref pat_vec)) => { let path_str = try_opt!(::types::rewrite_path(context, true, diff --git a/src/types.rs b/src/types.rs index c483032b451..1d9522d0f90 100644 --- a/src/types.rs +++ b/src/types.rs @@ -8,12 +8,15 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::ops::Deref; +use std::iter::ExactSizeIterator; + use syntax::ast::{self, Mutability, FunctionRetTy}; use syntax::print::pprust; use syntax::codemap::{self, Span, BytePos}; use syntax::abi; -use Indent; +use {Indent, Spanned}; use lists::{format_item_list, itemize_list, format_fn_args}; use rewrite::{Rewrite, RewriteContext}; use utils::{extra_offset, span_after, format_mutability, wrap_str}; @@ -239,7 +242,9 @@ fn format_function_type<'a, I>(inputs: I, width: usize, offset: Indent) -> Option<String> - where I: Iterator<Item = &'a ast::Ty> + where I: ExactSizeIterator, + <I as Iterator>::Item: Deref, + <I::Item as Deref>::Target: Rewrite + Spanned + 'a { // 2 for () let budget = try_opt!(width.checked_sub(2)); @@ -249,8 +254,8 @@ fn format_function_type<'a, I>(inputs: I, let items = itemize_list(context.codemap, inputs, ")", - |ty| ty.span.lo, - |ty| ty.span.hi, + |ty| ty.span().lo, + |ty| ty.span().hi, |ty| ty.rewrite(context, budget, offset), list_lo, span.hi); @@ -506,7 +511,13 @@ impl Rewrite for ast::Ty { let budget = try_opt!(width.checked_sub(2)); ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("[{}]", ty_str)) } - ast::TyTup(ref items) => rewrite_tuple(context, items, self.span, width, offset), + ast::TyTup(ref items) => { + rewrite_tuple(context, + items.iter().map(|x| &**x), + self.span, + width, + offset) + } ast::TyPolyTraitRef(ref trait_ref) => trait_ref.rewrite(context, width, offset), ast::TyPath(ref q_self, ref path) => { rewrite_path(context, false, q_self.as_ref(), path, width, offset) @@ -548,7 +559,7 @@ fn rewrite_bare_fn(bare_fn: &ast::BareFnTy, let budget = try_opt!(width.checked_sub(result.len())); let indent = offset + result.len(); - let rewrite = try_opt!(format_function_type(bare_fn.decl.inputs.iter().map(|x| &*(x.ty)), + let rewrite = try_opt!(format_function_type(bare_fn.decl.inputs.iter(), &bare_fn.decl.output, span, context, |
