Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 86dad6e

Browse files
authored
Merge pull request rust-lang#3576 from rchaser53/issue-3567
add the handling for vec! with paren inside macro
2 parents 2787138 + bee1a32 commit 86dad6e

File tree

2 files changed

+69
-41
lines changed

2 files changed

+69
-41
lines changed

src/macros.rs

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -361,51 +361,35 @@ fn rewrite_macro_inner(
361361

362362
match style {
363363
DelimToken::Paren => {
364-
// Format macro invocation as function call, preserve the trailing
365-
// comma because not all macros support them.
366-
overflow::rewrite_with_parens(
367-
context,
368-
&macro_name,
369-
arg_vec.iter(),
370-
shape,
371-
mac.span,
372-
context.config.width_heuristics().fn_call_width,
373-
if trailing_comma {
374-
Some(SeparatorTactic::Always)
375-
} else {
376-
Some(SeparatorTactic::Never)
377-
},
378-
)
379-
.map(|rw| match position {
380-
MacroPosition::Item => format!("{};", rw),
381-
_ => rw,
382-
})
364+
// Handle special case: `vec!(expr; expr)`
365+
if vec_with_semi {
366+
handle_vec_semi(context, shape, arg_vec, macro_name, style)
367+
} else {
368+
// Format macro invocation as function call, preserve the trailing
369+
// comma because not all macros support them.
370+
overflow::rewrite_with_parens(
371+
context,
372+
&macro_name,
373+
arg_vec.iter(),
374+
shape,
375+
mac.span,
376+
context.config.width_heuristics().fn_call_width,
377+
if trailing_comma {
378+
Some(SeparatorTactic::Always)
379+
} else {
380+
Some(SeparatorTactic::Never)
381+
},
382+
)
383+
.map(|rw| match position {
384+
MacroPosition::Item => format!("{};", rw),
385+
_ => rw,
386+
})
387+
}
383388
}
384389
DelimToken::Bracket => {
385390
// Handle special case: `vec![expr; expr]`
386391
if vec_with_semi {
387-
let mac_shape = shape.offset_left(macro_name.len())?;
388-
// 8 = `vec![]` + `; `
389-
let total_overhead = 8;
390-
let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
391-
let lhs = arg_vec[0].rewrite(context, nested_shape)?;
392-
let rhs = arg_vec[1].rewrite(context, nested_shape)?;
393-
if !lhs.contains('\n')
394-
&& !rhs.contains('\n')
395-
&& lhs.len() + rhs.len() + total_overhead <= shape.width
396-
{
397-
Some(format!("{}[{}; {}]", macro_name, lhs, rhs))
398-
} else {
399-
Some(format!(
400-
"{}[{}{};{}{}{}]",
401-
macro_name,
402-
nested_shape.indent.to_string_with_newline(context.config),
403-
lhs,
404-
nested_shape.indent.to_string_with_newline(context.config),
405-
rhs,
406-
shape.indent.to_string_with_newline(context.config),
407-
))
408-
}
392+
handle_vec_semi(context, shape, arg_vec, macro_name, style)
409393
} else {
410394
// If we are rewriting `vec!` macro or other special macros,
411395
// then we can rewrite this as an usual array literal.
@@ -453,6 +437,47 @@ fn rewrite_macro_inner(
453437
}
454438
}
455439

440+
fn handle_vec_semi(
441+
context: &RewriteContext<'_>,
442+
shape: Shape,
443+
arg_vec: Vec<MacroArg>,
444+
macro_name: String,
445+
delim_token: DelimToken,
446+
) -> Option<String> {
447+
let (left, right) = match delim_token {
448+
DelimToken::Paren => ("(", ")"),
449+
DelimToken::Bracket => ("[", "]"),
450+
_ => unreachable!(),
451+
};
452+
453+
let mac_shape = shape.offset_left(macro_name.len())?;
454+
// 8 = `vec![]` + `; ` or `vec!()` + `; `
455+
let total_overhead = 8;
456+
let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
457+
let lhs = arg_vec[0].rewrite(context, nested_shape)?;
458+
let rhs = arg_vec[1].rewrite(context, nested_shape)?;
459+
if !lhs.contains('\n')
460+
&& !rhs.contains('\n')
461+
&& lhs.len() + rhs.len() + total_overhead <= shape.width
462+
{
463+
// macro_name(lhs; rhs) or macro_name[lhs; rhs]
464+
Some(format!("{}{}{}; {}{}", macro_name, left, lhs, rhs, right))
465+
} else {
466+
// macro_name(\nlhs;\nrhs\n) or macro_name[\nlhs;\nrhs\n]
467+
Some(format!(
468+
"{}{}{}{};{}{}{}{}",
469+
macro_name,
470+
left,
471+
nested_shape.indent.to_string_with_newline(context.config),
472+
lhs,
473+
nested_shape.indent.to_string_with_newline(context.config),
474+
rhs,
475+
shape.indent.to_string_with_newline(context.config),
476+
right
477+
))
478+
}
479+
}
480+
456481
pub(crate) fn rewrite_macro_def(
457482
context: &RewriteContext<'_>,
458483
shape: Shape,

tests/target/issue-3567.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn check() {
2+
vec![vec!(0; 10); 10];
3+
}

0 commit comments

Comments
 (0)