Skip to content

Avoid drifting macro body which is unformattable #2480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rustfmt-bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ fn main() {
// (git not installed or if this is not a git repository) just return an empty string.
fn commit_info() -> String {
match (channel(), commit_hash(), commit_date()) {
(channel, Some(hash), Some(date)) => format!("{} ({} {})", channel, hash.trim_right(), date),
(channel, Some(hash), Some(date)) => {
format!("{} ({} {})", channel, hash.trim_right(), date)
}
_ => String::new(),
}
}
Expand Down
35 changes: 27 additions & 8 deletions rustfmt-core/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1908,12 +1908,16 @@ where
1
};
let used_width = extra_offset(callee_str, shape);
let one_line_width = shape.width.checked_sub(used_width + 2 * paren_overhead)?;
let one_line_width = shape
.width
.checked_sub(used_width + 2 * paren_overhead)
.unwrap_or(0);

// 1 = "(" or ")"
let one_line_shape = shape
.offset_left(last_line_width(callee_str) + 1)?
.sub_width(1)?;
.offset_left(last_line_width(callee_str) + 1)
.and_then(|shape| shape.sub_width(1))
.unwrap_or(Shape { width: 0, ..shape });
let nested_shape = shape_from_indent_style(
context,
shape,
Expand Down Expand Up @@ -1950,7 +1954,13 @@ where
);
}

let args_shape = shape.sub_width(last_line_width(callee_str))?;
let args_shape = Shape {
width: shape
.width
.checked_sub(last_line_width(callee_str))
.unwrap_or(0),
..shape
};
Some(format!(
"{}{}",
callee_str,
Expand Down Expand Up @@ -2317,9 +2327,16 @@ pub fn wrap_args_with_parens(
shape: Shape,
nested_shape: Shape,
) -> String {
let paren_overhead = paren_overhead(context);
let fits_one_line = args_str.len() + paren_overhead <= shape.width;
let extend_width = if args_str.is_empty() {
paren_overhead
} else {
paren_overhead / 2
};
if !context.use_block_indent()
|| (context.inside_macro && !args_str.contains('\n')
&& args_str.len() + paren_overhead(context) <= shape.width) || is_extendable
|| (context.inside_macro && !args_str.contains('\n') && fits_one_line)
|| (is_extendable && extend_width <= shape.width)
{
let mut result = String::with_capacity(args_str.len() + 4);
if context.config.spaces_within_parens_and_brackets() && !args_str.is_empty() {
Expand All @@ -2338,8 +2355,10 @@ pub fn wrap_args_with_parens(
let mut result =
String::with_capacity(args_str.len() + 2 + indent_str.len() + nested_indent_str.len());
result.push_str("(");
result.push_str(&nested_indent_str);
result.push_str(args_str);
if !args_str.is_empty() {
result.push_str(&nested_indent_str);
result.push_str(args_str);
}
result.push_str(&indent_str);
result.push_str(")");
result
Expand Down
3 changes: 2 additions & 1 deletion rustfmt-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use expr::{rewrite_array, rewrite_call_inner};
use lists::{itemize_list, write_list, ListFormatting};
use rewrite::{Rewrite, RewriteContext};
use shape::{Indent, Shape};
use utils::{format_visibility, mk_sp};
use utils::{format_visibility, mk_sp, wrap_str};

const FORCED_BRACKET_MACROS: &[&str] = &["vec!"];

Expand Down Expand Up @@ -810,6 +810,7 @@ impl MacroBranch {
None => return None,
},
};
let new_body = wrap_str(new_body, config.max_width(), shape)?;

// Indent the body since it is in a block.
let indent_str = body_indent.to_string(&config);
Expand Down
9 changes: 3 additions & 6 deletions rustfmt-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,9 @@ impl Rewrite for ast::TyParamBound {
ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => {
tref.rewrite(context, shape)
}
ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => {
Some(format!(
"?{}",
tref.rewrite(context, shape.offset_left(1)?)?
))
}
ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => Some(
format!("?{}", tref.rewrite(context, shape.offset_left(1)?)?),
),
ast::TyParamBound::RegionTyParamBound(ref l) => l.rewrite(context, shape),
}
}
Expand Down
35 changes: 31 additions & 4 deletions rustfmt-core/tests/source/macro_rules.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// rustfmt-error_on_line_overflow: false

macro_rules! m {
// a
($expr :expr, $( $func : ident ) * ) => {
Expand Down Expand Up @@ -50,12 +52,37 @@ macro m2 {
}
}


// #2438
// #2438, #2476
macro_rules! m {
() => {
fn foo() {
this_line_is_98_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
);
}
}
}
macro_rules! m {
() => {
fn foo() {
this_line_is_99_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
);
}
};
}
macro_rules! m {
() => {
fn foo() {
this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
);
}
};
}
macro_rules! m {
() => {
this_line_is_99_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
); // this line is drifting
fn foo() {
this_line_is_101_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
);
}
};
}

Expand Down
6 changes: 3 additions & 3 deletions rustfmt-core/tests/target/configs/indent_style/block_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fn main() {
"elit",
);
// #1501
let hyper = Arc::new(Client::with_connector(HttpsConnector::new(
TlsClient::new(),
)));
let hyper = Arc::new(Client::with_connector(
HttpsConnector::new(TlsClient::new()),
));

// chain
let x = yooooooooooooo
Expand Down
33 changes: 30 additions & 3 deletions rustfmt-core/tests/target/macro_rules.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// rustfmt-error_on_line_overflow: false

macro_rules! m {
// a
($expr: expr, $($func: ident)*) => {{
Expand Down Expand Up @@ -42,11 +44,36 @@ macro m2 {
}
}

// #2438
// #2438, #2476
macro_rules! m {
() => {
this_line_is_99_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
); // this line is drifting
fn foo() {
this_line_is_98_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx();
}
};
}
macro_rules! m {
() => {
fn foo() {
this_line_is_99_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
);
}
};
}
macro_rules! m {
() => {
fn foo() {
this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
);
}
};
}
macro_rules! m {
() => {
fn foo() {
this_line_is_101_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
);
}
};
}

Expand Down