Skip to content

Commit

Permalink
Merge pull request rust-lang#3067 from topecongiro/refactor-toexpr
Browse files Browse the repository at this point in the history
Add println!-like heuristic to the fail attribute
  • Loading branch information
nrc committed Oct 7, 2018
2 parents b32cf4a + bc835b7 commit d0c6a6d
Show file tree
Hide file tree
Showing 13 changed files with 371 additions and 335 deletions.
8 changes: 0 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,12 @@ matrix:
- env: INTEGRATION=stdsimd
- env: INTEGRATION=tempdir
allow_failures:
# Needs `edition = "2018"` in rustfmt.toml
- env: INTEGRATION=chalk
# Fails tests, don't know why
- env: INTEGRATION=crater
# Doesn't build
- env: INTEGRATION=futures-rs
# Doesn't build - seems to be because of an option
- env: INTEGRATION=packed_simd
# Weird bug I can't reproduce: #2969
- env: INTEGRATION=rand
# Test failure
- env: INTEGRATION=rust-clippy
# Build failure
- env: INTEGRATION=rust-semverver

script:
- |
Expand Down
61 changes: 13 additions & 48 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ use config::lists::*;
use config::IndentStyle;
use expr::rewrite_literal;
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use overflow;
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
use types::{rewrite_path, PathContext};
use utils::{count_newlines, mk_sp};

use std::borrow::Cow;
use syntax::ast;
use syntax::source_map::{BytePos, Span, DUMMY_SP};

Expand Down Expand Up @@ -216,56 +216,21 @@ impl Rewrite for ast::MetaItem {
}
ast::MetaItemKind::List(ref list) => {
let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?;

let has_comma = ::expr::span_ends_with_comma(context, self.span);
let trailing_comma = if has_comma { "," } else { "" };
let combine = list.len() == 1 && match list[0].node {
ast::NestedMetaItemKind::Literal(..) => false,
ast::NestedMetaItemKind::MetaItem(ref inner_meta_item) => {
match inner_meta_item.node {
ast::MetaItemKind::List(..) => rewrite_path(
context,
PathContext::Type,
None,
&inner_meta_item.ident,
shape,
)
.map_or(false, |s| s.len() + path.len() + 2 <= shape.width),
_ => false,
}
}
};

let argument_shape = argument_shape(
path.len() + 1,
2 + trailing_comma.len(),
combine,
shape,
let has_trailing_comma = ::expr::span_ends_with_comma(context, self.span);
overflow::rewrite_with_parens(
context,
)?;
let item_str = format_arg_list(
&path,
list.iter(),
|nested_meta_item| nested_meta_item.span.lo(),
|nested_meta_item| nested_meta_item.span.hi(),
|nested_meta_item| nested_meta_item.rewrite(context, argument_shape),
// 1 = "]"
shape.sub_width(1)?,
self.span,
context,
argument_shape,
// 3 = "()" and "]"
shape
.offset_left(path.len())?
.sub_width(3 + trailing_comma.len())?,
Some(context.config.width_heuristics().fn_call_width),
combine,
)?;

let indent = if item_str.starts_with('\n') {
shape.indent.to_string_with_newline(context.config)
} else {
Cow::Borrowed("")
};

format!("{}({}{}{})", path, item_str, trailing_comma, indent)
context.config.width_heuristics().fn_call_width,
Some(if has_trailing_comma {
SeparatorTactic::Always
} else {
SeparatorTactic::Never
}),
)?
}
ast::MetaItemKind::NameValue(ref literal) => {
let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?;
Expand Down
19 changes: 7 additions & 12 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use syntax::parse::classify;
use syntax::source_map::Span;
use syntax::{ast, ptr};

use expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond, ToExpr};
use expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
use items::{span_hi_for_arg, span_lo_for_arg};
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use overflow::OverflowableItem;
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
use source_map::SpanUtils;
Expand Down Expand Up @@ -358,18 +359,12 @@ pub fn rewrite_last_closure(
}

/// Returns true if the given vector of arguments has more than one `ast::ExprKind::Closure`.
pub fn args_have_many_closure<T>(args: &[&T]) -> bool
where
T: ToExpr,
{
pub fn args_have_many_closure(args: &[OverflowableItem]) -> bool {
args.iter()
.filter(|arg| {
arg.to_expr()
.map(|e| match e.node {
ast::ExprKind::Closure(..) => true,
_ => false,
})
.unwrap_or(false)
.filter_map(|arg| arg.to_expr())
.filter(|expr| match expr.node {
ast::ExprKind::Closure(..) => true,
_ => false,
})
.count()
> 1
Expand Down
Loading

0 comments on commit d0c6a6d

Please sign in to comment.