Skip to content

Commit

Permalink
Merge pull request rust-lang#3576 from rchaser53/issue-3567
Browse files Browse the repository at this point in the history
add the handling for vec! with paren inside macro
  • Loading branch information
topecongiro authored May 21, 2019
2 parents 2787138 + bee1a32 commit 86dad6e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 41 deletions.
107 changes: 66 additions & 41 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,51 +361,35 @@ fn rewrite_macro_inner(

match style {
DelimToken::Paren => {
// Format macro invocation as function call, preserve the trailing
// comma because not all macros support them.
overflow::rewrite_with_parens(
context,
&macro_name,
arg_vec.iter(),
shape,
mac.span,
context.config.width_heuristics().fn_call_width,
if trailing_comma {
Some(SeparatorTactic::Always)
} else {
Some(SeparatorTactic::Never)
},
)
.map(|rw| match position {
MacroPosition::Item => format!("{};", rw),
_ => rw,
})
// Handle special case: `vec!(expr; expr)`
if vec_with_semi {
handle_vec_semi(context, shape, arg_vec, macro_name, style)
} else {
// Format macro invocation as function call, preserve the trailing
// comma because not all macros support them.
overflow::rewrite_with_parens(
context,
&macro_name,
arg_vec.iter(),
shape,
mac.span,
context.config.width_heuristics().fn_call_width,
if trailing_comma {
Some(SeparatorTactic::Always)
} else {
Some(SeparatorTactic::Never)
},
)
.map(|rw| match position {
MacroPosition::Item => format!("{};", rw),
_ => rw,
})
}
}
DelimToken::Bracket => {
// Handle special case: `vec![expr; expr]`
if vec_with_semi {
let mac_shape = shape.offset_left(macro_name.len())?;
// 8 = `vec![]` + `; `
let total_overhead = 8;
let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
let lhs = arg_vec[0].rewrite(context, nested_shape)?;
let rhs = arg_vec[1].rewrite(context, nested_shape)?;
if !lhs.contains('\n')
&& !rhs.contains('\n')
&& lhs.len() + rhs.len() + total_overhead <= shape.width
{
Some(format!("{}[{}; {}]", macro_name, lhs, rhs))
} else {
Some(format!(
"{}[{}{};{}{}{}]",
macro_name,
nested_shape.indent.to_string_with_newline(context.config),
lhs,
nested_shape.indent.to_string_with_newline(context.config),
rhs,
shape.indent.to_string_with_newline(context.config),
))
}
handle_vec_semi(context, shape, arg_vec, macro_name, style)
} else {
// If we are rewriting `vec!` macro or other special macros,
// then we can rewrite this as an usual array literal.
Expand Down Expand Up @@ -453,6 +437,47 @@ fn rewrite_macro_inner(
}
}

fn handle_vec_semi(
context: &RewriteContext<'_>,
shape: Shape,
arg_vec: Vec<MacroArg>,
macro_name: String,
delim_token: DelimToken,
) -> Option<String> {
let (left, right) = match delim_token {
DelimToken::Paren => ("(", ")"),
DelimToken::Bracket => ("[", "]"),
_ => unreachable!(),
};

let mac_shape = shape.offset_left(macro_name.len())?;
// 8 = `vec![]` + `; ` or `vec!()` + `; `
let total_overhead = 8;
let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
let lhs = arg_vec[0].rewrite(context, nested_shape)?;
let rhs = arg_vec[1].rewrite(context, nested_shape)?;
if !lhs.contains('\n')
&& !rhs.contains('\n')
&& lhs.len() + rhs.len() + total_overhead <= shape.width
{
// macro_name(lhs; rhs) or macro_name[lhs; rhs]
Some(format!("{}{}{}; {}{}", macro_name, left, lhs, rhs, right))
} else {
// macro_name(\nlhs;\nrhs\n) or macro_name[\nlhs;\nrhs\n]
Some(format!(
"{}{}{}{};{}{}{}{}",
macro_name,
left,
nested_shape.indent.to_string_with_newline(context.config),
lhs,
nested_shape.indent.to_string_with_newline(context.config),
rhs,
shape.indent.to_string_with_newline(context.config),
right
))
}
}

pub(crate) fn rewrite_macro_def(
context: &RewriteContext<'_>,
shape: Shape,
Expand Down
3 changes: 3 additions & 0 deletions tests/target/issue-3567.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn check() {
vec![vec!(0; 10); 10];
}

0 comments on commit 86dad6e

Please sign in to comment.