Skip to content

Commit ceac483

Browse files
authored
Merge pull request rust-lang#3080 from topecongiro/issue-3031
Format macro calls with item-like arguments
2 parents 1b2e727 + 70177a0 commit ceac483

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

src/macros.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use shape::{Indent, Shape};
4444
use source_map::SpanUtils;
4545
use spanned::Spanned;
4646
use utils::{format_visibility, mk_sp, rewrite_ident, wrap_str};
47+
use visitor::FmtVisitor;
4748

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

@@ -63,6 +64,15 @@ pub enum MacroArg {
6364
Item(ptr::P<ast::Item>),
6465
}
6566

67+
impl MacroArg {
68+
fn is_item(&self) -> bool {
69+
match self {
70+
MacroArg::Item(..) => true,
71+
_ => false,
72+
}
73+
}
74+
}
75+
6676
impl Rewrite for ast::Item {
6777
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
6878
let mut visitor = ::visitor::FmtVisitor::from_context(context);
@@ -259,6 +269,7 @@ pub fn rewrite_macro_inner(
259269
}
260270
return return_original_snippet_with_failure_marked(context, mac.span);
261271
}
272+
_ if arg_vec.last().map_or(false, MacroArg::is_item) => continue,
262273
_ => return return_original_snippet_with_failure_marked(context, mac.span),
263274
}
264275

@@ -271,6 +282,18 @@ pub fn rewrite_macro_inner(
271282
}
272283
}
273284

285+
if !arg_vec.is_empty() && arg_vec.iter().all(MacroArg::is_item) {
286+
return rewrite_macro_with_items(
287+
context,
288+
&arg_vec,
289+
&macro_name,
290+
shape,
291+
style,
292+
position,
293+
mac.span,
294+
);
295+
}
296+
274297
match style {
275298
DelimToken::Paren => {
276299
// Format macro invocation as function call, preserve the trailing
@@ -1428,3 +1451,45 @@ fn format_lazy_static(context: &RewriteContext, shape: Shape, ts: &TokenStream)
14281451

14291452
Some(result)
14301453
}
1454+
1455+
fn rewrite_macro_with_items(
1456+
context: &RewriteContext,
1457+
items: &[MacroArg],
1458+
macro_name: &str,
1459+
shape: Shape,
1460+
style: DelimToken,
1461+
position: MacroPosition,
1462+
span: Span,
1463+
) -> Option<String> {
1464+
let (opener, closer) = match style {
1465+
DelimToken::Paren => ("(", ")"),
1466+
DelimToken::Bracket => ("[", "]"),
1467+
DelimToken::Brace => (" {", "}"),
1468+
_ => return None,
1469+
};
1470+
let trailing_semicolon = match style {
1471+
DelimToken::Paren | DelimToken::Bracket if position == MacroPosition::Item => ";",
1472+
_ => "",
1473+
};
1474+
1475+
let mut visitor = FmtVisitor::from_context(context);
1476+
visitor.block_indent = shape.indent.block_indent(context.config);
1477+
visitor.last_pos = context.snippet_provider.span_after(span, opener.trim());
1478+
for item in items {
1479+
let item = match item {
1480+
MacroArg::Item(item) => item,
1481+
_ => return None,
1482+
};
1483+
visitor.visit_item(&item);
1484+
}
1485+
1486+
let mut result = String::with_capacity(256);
1487+
result.push_str(&macro_name);
1488+
result.push_str(opener);
1489+
result.push_str(&visitor.block_indent.to_string_with_newline(context.config));
1490+
result.push_str(visitor.buffer.trim());
1491+
result.push_str(&shape.indent.to_string_with_newline(context.config));
1492+
result.push_str(closer);
1493+
result.push_str(trailing_semicolon);
1494+
return Some(result);
1495+
}

tests/source/macros.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,29 @@ named!(
426426

427427
// #2857
428428
convert_args!(vec!(1, 2, 3));
429+
430+
// #3031
431+
thread_local!(
432+
/// TLV Holds a set of JSTraceables that need to be rooted
433+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
434+
RefCell::new(RootedTraceableSet::new()) ;
435+
) ;
436+
437+
thread_local![
438+
/// TLV Holds a set of JSTraceables that need to be rooted
439+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
440+
RefCell::new(RootedTraceableSet::new()) ;
441+
442+
/// TLV Holds a set of JSTraceables that need to be rooted
443+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
444+
RefCell::new(RootedTraceableSet::new(0)) ;
445+
446+
/// TLV Holds a set of JSTraceables that need to be rooted
447+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
448+
RefCell::new(RootedTraceableSet::new(), xxx, yyy) ;
449+
450+
/// TLV Holds a set of JSTraceables that need to be rooted
451+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
452+
RefCell::new(RootedTraceableSet::new(1234)) ;
453+
454+
] ;

tests/target/issue-2523.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
// Format items that appear as arguments of macro call.
1212
//! ```rust
1313
//! let x = 3;
14-
//! some_macro!(pub fn foo() {
15-
//! println!("Don't unindent me!");
16-
//! });
14+
//! some_macro!(
15+
//! pub fn foo() {
16+
//! println!("Don't unindent me!");
17+
//! }
18+
//! );
1719
//! ```

tests/target/macros.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,3 +1007,26 @@ named!(
10071007

10081008
// #2857
10091009
convert_args!(vec!(1, 2, 3));
1010+
1011+
// #3031
1012+
thread_local!(
1013+
/// TLV Holds a set of JSTraceables that need to be rooted
1014+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> = RefCell::new(RootedTraceableSet::new());
1015+
);
1016+
1017+
thread_local![
1018+
/// TLV Holds a set of JSTraceables that need to be rooted
1019+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> = RefCell::new(RootedTraceableSet::new());
1020+
1021+
/// TLV Holds a set of JSTraceables that need to be rooted
1022+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
1023+
RefCell::new(RootedTraceableSet::new(0));
1024+
1025+
/// TLV Holds a set of JSTraceables that need to be rooted
1026+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
1027+
RefCell::new(RootedTraceableSet::new(), xxx, yyy);
1028+
1029+
/// TLV Holds a set of JSTraceables that need to be rooted
1030+
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
1031+
RefCell::new(RootedTraceableSet::new(1234));
1032+
];

0 commit comments

Comments
 (0)