]> git.lizzy.rs Git - rust.git/blob - src/macros.rs
d25106c49616eae6501da6a3e92821482fd1d11e
[rust.git] / src / macros.rs
1 // Format list-like macro invocations. These are invocations whose token trees
2 // can be interpreted as expressions and separated by commas.
3 // Note that these token trees do not actually have to be interpreted as
4 // expressions by the compiler. An example of an invocation we would reformat is
5 // foo!( x, y, z ). The token x may represent an identifier in the code, but we
6 // interpreted as an expression.
7 // Macro uses which are not-list like, such as bar!(key => val), will not be
8 // reformatted.
9 // List-like invocations with parentheses will be formatted as function calls,
10 // and those with brackets will be formatted as array literals.
11
12 use std::collections::HashMap;
13 use std::panic::{catch_unwind, AssertUnwindSafe};
14
15 use syntax::parse::new_parser_from_tts;
16 use syntax::parse::parser::Parser;
17 use syntax::parse::token::{BinOpToken, DelimToken, Token};
18 use syntax::print::pprust;
19 use syntax::source_map::{BytePos, Span};
20 use syntax::symbol::keywords;
21 use syntax::tokenstream::{Cursor, TokenStream, TokenTree};
22 use syntax::ThinVec;
23 use syntax::{ast, parse, ptr};
24
25 use crate::comment::{
26     contains_comment, CharClasses, FindUncommented, FullCodeCharKind, LineClasses,
27 };
28 use crate::config::lists::*;
29 use crate::expr::rewrite_array;
30 use crate::lists::{itemize_list, write_list, ListFormatting};
31 use crate::overflow;
32 use crate::rewrite::{Rewrite, RewriteContext};
33 use crate::shape::{Indent, Shape};
34 use crate::source_map::SpanUtils;
35 use crate::spanned::Spanned;
36 use crate::utils::{
37     format_visibility, indent_next_line, is_empty_line, mk_sp, remove_trailing_white_spaces,
38     rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
39 };
40 use crate::visitor::FmtVisitor;
41
42 const FORCED_BRACKET_MACROS: &[&str] = &["vec!"];
43
44 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
45 pub(crate) enum MacroPosition {
46     Item,
47     Statement,
48     Expression,
49     Pat,
50 }
51
52 #[derive(Debug)]
53 pub(crate) enum MacroArg {
54     Expr(ptr::P<ast::Expr>),
55     Ty(ptr::P<ast::Ty>),
56     Pat(ptr::P<ast::Pat>),
57     Item(ptr::P<ast::Item>),
58     Keyword(ast::Ident, Span),
59 }
60
61 impl MacroArg {
62     fn is_item(&self) -> bool {
63         match self {
64             MacroArg::Item(..) => true,
65             _ => false,
66         }
67     }
68 }
69
70 impl Rewrite for ast::Item {
71     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
72         let mut visitor = crate::visitor::FmtVisitor::from_context(context);
73         visitor.block_indent = shape.indent;
74         visitor.last_pos = self.span().lo();
75         visitor.visit_item(self);
76         Some(visitor.buffer.to_owned())
77     }
78 }
79
80 impl Rewrite for MacroArg {
81     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
82         match *self {
83             MacroArg::Expr(ref expr) => expr.rewrite(context, shape),
84             MacroArg::Ty(ref ty) => ty.rewrite(context, shape),
85             MacroArg::Pat(ref pat) => pat.rewrite(context, shape),
86             MacroArg::Item(ref item) => item.rewrite(context, shape),
87             MacroArg::Keyword(ident, _) => Some(ident.to_string()),
88         }
89     }
90 }
91
92 fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
93     macro_rules! parse_macro_arg {
94         ($macro_arg:ident, $parser:expr, $f:expr) => {
95             let mut cloned_parser = (*parser).clone();
96             match $parser(&mut cloned_parser) {
97                 Ok(x) => {
98                     if parser.sess.span_diagnostic.has_errors() {
99                         parser.sess.span_diagnostic.reset_err_count();
100                     } else {
101                         // Parsing succeeded.
102                         *parser = cloned_parser;
103                         return Some(MacroArg::$macro_arg($f(x)?));
104                     }
105                 }
106                 Err(mut e) => {
107                     e.cancel();
108                     parser.sess.span_diagnostic.reset_err_count();
109                 }
110             }
111         };
112     }
113
114     parse_macro_arg!(
115         Expr,
116         |parser: &mut parse::parser::Parser<'b>| parser.parse_expr(),
117         |x: ptr::P<ast::Expr>| Some(x)
118     );
119     parse_macro_arg!(
120         Ty,
121         |parser: &mut parse::parser::Parser<'b>| parser.parse_ty(),
122         |x: ptr::P<ast::Ty>| Some(x)
123     );
124     parse_macro_arg!(
125         Pat,
126         |parser: &mut parse::parser::Parser<'b>| parser.parse_pat(None),
127         |x: ptr::P<ast::Pat>| Some(x)
128     );
129     // `parse_item` returns `Option<ptr::P<ast::Item>>`.
130     parse_macro_arg!(
131         Item,
132         |parser: &mut parse::parser::Parser<'b>| parser.parse_item(),
133         |x: Option<ptr::P<ast::Item>>| x
134     );
135
136     None
137 }
138
139 /// Rewrite macro name without using pretty-printer if possible.
140 fn rewrite_macro_name(
141     context: &RewriteContext<'_>,
142     path: &ast::Path,
143     extra_ident: Option<ast::Ident>,
144 ) -> String {
145     let name = if path.segments.len() == 1 {
146         // Avoid using pretty-printer in the common case.
147         format!("{}!", rewrite_ident(context, path.segments[0].ident))
148     } else {
149         format!("{}!", path)
150     };
151     match extra_ident {
152         Some(ident) if ident != keywords::Invalid.ident() => format!("{} {}", name, ident),
153         _ => name,
154     }
155 }
156
157 // Use this on failing to format the macro call.
158 fn return_macro_parse_failure_fallback(
159     context: &RewriteContext<'_>,
160     indent: Indent,
161     span: Span,
162 ) -> Option<String> {
163     // Mark this as a failure however we format it
164     context.macro_rewrite_failure.replace(true);
165
166     // Heuristically determine whether the last line of the macro uses "Block" style
167     // rather than using "Visual" style, or another indentation style.
168     let is_like_block_indent_style = context
169         .snippet(span)
170         .lines()
171         .last()
172         .map(|closing_line| {
173             closing_line.trim().chars().all(|ch| match ch {
174                 '}' | ')' | ']' => true,
175                 _ => false,
176             })
177         })
178         .unwrap_or(false);
179     if is_like_block_indent_style {
180         return trim_left_preserve_layout(context.snippet(span), indent, &context.config);
181     }
182
183     // Return the snippet unmodified if the macro is not block-like
184     Some(context.snippet(span).to_owned())
185 }
186
187 struct InsideMacroGuard<'a> {
188     context: &'a RewriteContext<'a>,
189     is_nested: bool,
190 }
191
192 impl<'a> InsideMacroGuard<'a> {
193     fn inside_macro_context(context: &'a RewriteContext<'_>) -> InsideMacroGuard<'a> {
194         let is_nested = context.inside_macro.replace(true);
195         InsideMacroGuard { context, is_nested }
196     }
197 }
198
199 impl<'a> Drop for InsideMacroGuard<'a> {
200     fn drop(&mut self) {
201         self.context.inside_macro.replace(self.is_nested);
202     }
203 }
204
205 pub(crate) fn rewrite_macro(
206     mac: &ast::Mac,
207     extra_ident: Option<ast::Ident>,
208     context: &RewriteContext<'_>,
209     shape: Shape,
210     position: MacroPosition,
211 ) -> Option<String> {
212     catch_unwind(AssertUnwindSafe(|| {
213         let should_skip = context
214             .skip_macro_names
215             .borrow()
216             .contains(&context.snippet(mac.node.path.span).to_owned());
217         if should_skip {
218             None
219         } else {
220             let guard = InsideMacroGuard::inside_macro_context(context);
221             let result =
222                 rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested);
223             if result.is_none() {
224                 context.macro_rewrite_failure.replace(true);
225             }
226             result
227         }
228     }))
229     .ok()?
230 }
231
232 fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
233     for &keyword in RUST_KEYWORDS.iter() {
234         if parser.token.is_keyword(keyword)
235             && parser.look_ahead(1, |t| {
236                 *t == Token::Eof
237                     || *t == Token::Comma
238                     || *t == Token::CloseDelim(DelimToken::NoDelim)
239             })
240         {
241             let macro_arg = MacroArg::Keyword(keyword.ident(), parser.span);
242             parser.bump();
243             return Some(macro_arg);
244         }
245     }
246     None
247 }
248
249 fn rewrite_macro_inner(
250     mac: &ast::Mac,
251     extra_ident: Option<ast::Ident>,
252     context: &RewriteContext<'_>,
253     shape: Shape,
254     position: MacroPosition,
255     is_nested_macro: bool,
256 ) -> Option<String> {
257     if context.config.use_try_shorthand() {
258         if let Some(expr) = convert_try_mac(mac, context) {
259             context.inside_macro.replace(false);
260             return expr.rewrite(context, shape);
261         }
262     }
263
264     let original_style = macro_style(mac, context);
265
266     let macro_name = rewrite_macro_name(context, &mac.node.path, extra_ident);
267
268     let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) && !is_nested_macro {
269         DelimToken::Bracket
270     } else {
271         original_style
272     };
273
274     let ts: TokenStream = mac.node.stream();
275     let has_comment = contains_comment(context.snippet(mac.span));
276     if ts.is_empty() && !has_comment {
277         return match style {
278             DelimToken::Paren if position == MacroPosition::Item => {
279                 Some(format!("{}();", macro_name))
280             }
281             DelimToken::Bracket if position == MacroPosition::Item => {
282                 Some(format!("{}[];", macro_name))
283             }
284             DelimToken::Paren => Some(format!("{}()", macro_name)),
285             DelimToken::Bracket => Some(format!("{}[]", macro_name)),
286             DelimToken::Brace => Some(format!("{} {{}}", macro_name)),
287             _ => unreachable!(),
288         };
289     }
290     // Format well-known macros which cannot be parsed as a valid AST.
291     if macro_name == "lazy_static!" && !has_comment {
292         if let success @ Some(..) = format_lazy_static(context, shape, &ts) {
293             return success;
294         }
295     }
296
297     let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
298     let mut arg_vec = Vec::new();
299     let mut vec_with_semi = false;
300     let mut trailing_comma = false;
301
302     if DelimToken::Brace != style {
303         loop {
304             if let Some(arg) = parse_macro_arg(&mut parser) {
305                 arg_vec.push(arg);
306             } else if let Some(arg) = check_keyword(&mut parser) {
307                 arg_vec.push(arg);
308             } else {
309                 return return_macro_parse_failure_fallback(context, shape.indent, mac.span);
310             }
311
312             match parser.token {
313                 Token::Eof => break,
314                 Token::Comma => (),
315                 Token::Semi => {
316                     // Try to parse `vec![expr; expr]`
317                     if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {
318                         parser.bump();
319                         if parser.token != Token::Eof {
320                             match parse_macro_arg(&mut parser) {
321                                 Some(arg) => {
322                                     arg_vec.push(arg);
323                                     parser.bump();
324                                     if parser.token == Token::Eof && arg_vec.len() == 2 {
325                                         vec_with_semi = true;
326                                         break;
327                                     }
328                                 }
329                                 None => {
330                                     return return_macro_parse_failure_fallback(
331                                         context,
332                                         shape.indent,
333                                         mac.span,
334                                     );
335                                 }
336                             }
337                         }
338                     }
339                     return return_macro_parse_failure_fallback(context, shape.indent, mac.span);
340                 }
341                 _ if arg_vec.last().map_or(false, MacroArg::is_item) => continue,
342                 _ => return return_macro_parse_failure_fallback(context, shape.indent, mac.span),
343             }
344
345             parser.bump();
346
347             if parser.token == Token::Eof {
348                 trailing_comma = true;
349                 break;
350             }
351         }
352     }
353
354     if !arg_vec.is_empty() && arg_vec.iter().all(MacroArg::is_item) {
355         return rewrite_macro_with_items(
356             context,
357             &arg_vec,
358             &macro_name,
359             shape,
360             style,
361             position,
362             mac.span,
363         );
364     }
365
366     match style {
367         DelimToken::Paren => {
368             // Handle special case: `vec!(expr; expr)`
369             if vec_with_semi {
370                 handle_vec_semi(context, shape, arg_vec, macro_name, style)
371             } else {
372                 // Format macro invocation as function call, preserve the trailing
373                 // comma because not all macros support them.
374                 overflow::rewrite_with_parens(
375                     context,
376                     &macro_name,
377                     arg_vec.iter(),
378                     shape,
379                     mac.span,
380                     context.config.width_heuristics().fn_call_width,
381                     if trailing_comma {
382                         Some(SeparatorTactic::Always)
383                     } else {
384                         Some(SeparatorTactic::Never)
385                     },
386                 )
387                 .map(|rw| match position {
388                     MacroPosition::Item => format!("{};", rw),
389                     _ => rw,
390                 })
391             }
392         }
393         DelimToken::Bracket => {
394             // Handle special case: `vec![expr; expr]`
395             if vec_with_semi {
396                 handle_vec_semi(context, shape, arg_vec, macro_name, style)
397             } else {
398                 // If we are rewriting `vec!` macro or other special macros,
399                 // then we can rewrite this as an usual array literal.
400                 // Otherwise, we must preserve the original existence of trailing comma.
401                 let macro_name = &macro_name.as_str();
402                 let mut force_trailing_comma = if trailing_comma {
403                     Some(SeparatorTactic::Always)
404                 } else {
405                     Some(SeparatorTactic::Never)
406                 };
407                 if FORCED_BRACKET_MACROS.contains(macro_name) && !is_nested_macro {
408                     context.inside_macro.replace(false);
409                     if context.use_block_indent() {
410                         force_trailing_comma = Some(SeparatorTactic::Vertical);
411                     };
412                 }
413                 let rewrite = rewrite_array(
414                     macro_name,
415                     arg_vec.iter(),
416                     mac.span,
417                     context,
418                     shape,
419                     force_trailing_comma,
420                     Some(original_style),
421                 )?;
422                 let comma = match position {
423                     MacroPosition::Item => ";",
424                     _ => "",
425                 };
426
427                 Some(format!("{}{}", rewrite, comma))
428             }
429         }
430         DelimToken::Brace => {
431             // For macro invocations with braces, always put a space between
432             // the `macro_name!` and `{ /* macro_body */ }` but skip modifying
433             // anything in between the braces (for now).
434             let snippet = context.snippet(mac.span).trim_start_matches(|c| c != '{');
435             match trim_left_preserve_layout(snippet, shape.indent, &context.config) {
436                 Some(macro_body) => Some(format!("{} {}", macro_name, macro_body)),
437                 None => Some(format!("{} {}", macro_name, snippet)),
438             }
439         }
440         _ => unreachable!(),
441     }
442 }
443
444 fn handle_vec_semi(
445     context: &RewriteContext<'_>,
446     shape: Shape,
447     arg_vec: Vec<MacroArg>,
448     macro_name: String,
449     delim_token: DelimToken,
450 ) -> Option<String> {
451     let (left, right) = match delim_token {
452         DelimToken::Paren => ("(", ")"),
453         DelimToken::Bracket => ("[", "]"),
454         _ => unreachable!(),
455     };
456
457     let mac_shape = shape.offset_left(macro_name.len())?;
458     // 8 = `vec![]` + `; ` or `vec!()` + `; `
459     let total_overhead = 8;
460     let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
461     let lhs = arg_vec[0].rewrite(context, nested_shape)?;
462     let rhs = arg_vec[1].rewrite(context, nested_shape)?;
463     if !lhs.contains('\n')
464         && !rhs.contains('\n')
465         && lhs.len() + rhs.len() + total_overhead <= shape.width
466     {
467         // macro_name(lhs; rhs) or macro_name[lhs; rhs]
468         Some(format!("{}{}{}; {}{}", macro_name, left, lhs, rhs, right))
469     } else {
470         // macro_name(\nlhs;\nrhs\n) or macro_name[\nlhs;\nrhs\n]
471         Some(format!(
472             "{}{}{}{};{}{}{}{}",
473             macro_name,
474             left,
475             nested_shape.indent.to_string_with_newline(context.config),
476             lhs,
477             nested_shape.indent.to_string_with_newline(context.config),
478             rhs,
479             shape.indent.to_string_with_newline(context.config),
480             right
481         ))
482     }
483 }
484
485 pub(crate) fn rewrite_macro_def(
486     context: &RewriteContext<'_>,
487     shape: Shape,
488     indent: Indent,
489     def: &ast::MacroDef,
490     ident: ast::Ident,
491     vis: &ast::Visibility,
492     span: Span,
493 ) -> Option<String> {
494     let snippet = Some(remove_trailing_white_spaces(context.snippet(span)));
495     if snippet.as_ref().map_or(true, |s| s.ends_with(';')) {
496         return snippet;
497     }
498
499     let mut parser = MacroParser::new(def.stream().into_trees());
500     let parsed_def = match parser.parse() {
501         Some(def) => def,
502         None => return snippet,
503     };
504
505     let mut result = if def.legacy {
506         String::from("macro_rules!")
507     } else {
508         format!("{}macro", format_visibility(context, vis))
509     };
510
511     result += " ";
512     result += rewrite_ident(context, ident);
513
514     let multi_branch_style = def.legacy || parsed_def.branches.len() != 1;
515
516     let arm_shape = if multi_branch_style {
517         shape
518             .block_indent(context.config.tab_spaces())
519             .with_max_width(context.config)
520     } else {
521         shape
522     };
523
524     let branch_items = itemize_list(
525         context.snippet_provider,
526         parsed_def.branches.iter(),
527         "}",
528         ";",
529         |branch| branch.span.lo(),
530         |branch| branch.span.hi(),
531         |branch| match branch.rewrite(context, arm_shape, multi_branch_style) {
532             Some(v) => Some(v),
533             // if the rewrite returned None because a macro could not be rewritten, then return the
534             // original body
535             None if *context.macro_rewrite_failure.borrow() => {
536                 Some(context.snippet(branch.body).trim().to_string())
537             }
538             None => None,
539         },
540         context.snippet_provider.span_after(span, "{"),
541         span.hi(),
542         false,
543     )
544     .collect::<Vec<_>>();
545
546     let fmt = ListFormatting::new(arm_shape, context.config)
547         .separator(if def.legacy { ";" } else { "" })
548         .trailing_separator(SeparatorTactic::Always)
549         .preserve_newline(true);
550
551     if multi_branch_style {
552         result += " {";
553         result += &arm_shape.indent.to_string_with_newline(context.config);
554     }
555
556     match write_list(&branch_items, &fmt) {
557         Some(ref s) => result += s,
558         None => return snippet,
559     }
560
561     if multi_branch_style {
562         result += &indent.to_string_with_newline(context.config);
563         result += "}";
564     }
565
566     Some(result)
567 }
568
569 fn register_metavariable(
570     map: &mut HashMap<String, String>,
571     result: &mut String,
572     name: &str,
573     dollar_count: usize,
574 ) {
575     let mut new_name = "$".repeat(dollar_count - 1);
576     let mut old_name = "$".repeat(dollar_count);
577
578     new_name.push('z');
579     new_name.push_str(name);
580     old_name.push_str(name);
581
582     result.push_str(&new_name);
583     map.insert(old_name, new_name);
584 }
585
586 // Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
587 // aren't causing problems.
588 // This should also work for escaped `$` variables, where we leave earlier `$`s.
589 fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
590     // Each substitution will require five or six extra bytes.
591     let mut result = String::with_capacity(input.len() + 64);
592     let mut substs = HashMap::new();
593     let mut dollar_count = 0;
594     let mut cur_name = String::new();
595
596     for (kind, c) in CharClasses::new(input.chars()) {
597         if kind != FullCodeCharKind::Normal {
598             result.push(c);
599         } else if c == '$' {
600             dollar_count += 1;
601         } else if dollar_count == 0 {
602             result.push(c);
603         } else if !c.is_alphanumeric() && !cur_name.is_empty() {
604             // Terminates a name following one or more dollars.
605             register_metavariable(&mut substs, &mut result, &cur_name, dollar_count);
606
607             result.push(c);
608             dollar_count = 0;
609             cur_name.clear();
610         } else if c == '(' && cur_name.is_empty() {
611             // FIXME: Support macro def with repeat.
612             return None;
613         } else if c.is_alphanumeric() || c == '_' {
614             cur_name.push(c);
615         }
616     }
617
618     if !cur_name.is_empty() {
619         register_metavariable(&mut substs, &mut result, &cur_name, dollar_count);
620     }
621
622     debug!("replace_names `{}` {:?}", result, substs);
623
624     Some((result, substs))
625 }
626
627 #[derive(Debug, Clone)]
628 enum MacroArgKind {
629     /// e.g., `$x: expr`.
630     MetaVariable(ast::Ident, String),
631     /// e.g., `$($foo: expr),*`
632     Repeat(
633         /// `()`, `[]` or `{}`.
634         DelimToken,
635         /// Inner arguments inside delimiters.
636         Vec<ParsedMacroArg>,
637         /// Something after the closing delimiter and the repeat token, if available.
638         Option<Box<ParsedMacroArg>>,
639         /// The repeat token. This could be one of `*`, `+` or `?`.
640         Token,
641     ),
642     /// e.g., `[derive(Debug)]`
643     Delimited(DelimToken, Vec<ParsedMacroArg>),
644     /// A possible separator. e.g., `,` or `;`.
645     Separator(String, String),
646     /// Other random stuff that does not fit to other kinds.
647     /// e.g., `== foo` in `($x: expr == foo)`.
648     Other(String, String),
649 }
650
651 fn delim_token_to_str(
652     context: &RewriteContext<'_>,
653     delim_token: DelimToken,
654     shape: Shape,
655     use_multiple_lines: bool,
656     inner_is_empty: bool,
657 ) -> (String, String) {
658     let (lhs, rhs) = match delim_token {
659         DelimToken::Paren => ("(", ")"),
660         DelimToken::Bracket => ("[", "]"),
661         DelimToken::Brace => {
662             if inner_is_empty || use_multiple_lines {
663                 ("{", "}")
664             } else {
665                 ("{ ", " }")
666             }
667         }
668         DelimToken::NoDelim => ("", ""),
669     };
670     if use_multiple_lines {
671         let indent_str = shape.indent.to_string_with_newline(context.config);
672         let nested_indent_str = shape
673             .indent
674             .block_indent(context.config)
675             .to_string_with_newline(context.config);
676         (
677             format!("{}{}", lhs, nested_indent_str),
678             format!("{}{}", indent_str, rhs),
679         )
680     } else {
681         (lhs.to_owned(), rhs.to_owned())
682     }
683 }
684
685 impl MacroArgKind {
686     fn starts_with_brace(&self) -> bool {
687         match *self {
688             MacroArgKind::Repeat(DelimToken::Brace, _, _, _)
689             | MacroArgKind::Delimited(DelimToken::Brace, _) => true,
690             _ => false,
691         }
692     }
693
694     fn starts_with_dollar(&self) -> bool {
695         match *self {
696             MacroArgKind::Repeat(..) | MacroArgKind::MetaVariable(..) => true,
697             _ => false,
698         }
699     }
700
701     fn ends_with_space(&self) -> bool {
702         match *self {
703             MacroArgKind::Separator(..) => true,
704             _ => false,
705         }
706     }
707
708     fn has_meta_var(&self) -> bool {
709         match *self {
710             MacroArgKind::MetaVariable(..) => true,
711             MacroArgKind::Repeat(_, ref args, _, _) => args.iter().any(|a| a.kind.has_meta_var()),
712             _ => false,
713         }
714     }
715
716     fn rewrite(
717         &self,
718         context: &RewriteContext<'_>,
719         shape: Shape,
720         use_multiple_lines: bool,
721     ) -> Option<String> {
722         let rewrite_delimited_inner = |delim_tok, args| -> Option<(String, String, String)> {
723             let inner = wrap_macro_args(context, args, shape)?;
724             let (lhs, rhs) = delim_token_to_str(context, delim_tok, shape, false, inner.is_empty());
725             if lhs.len() + inner.len() + rhs.len() <= shape.width {
726                 return Some((lhs, inner, rhs));
727             }
728
729             let (lhs, rhs) = delim_token_to_str(context, delim_tok, shape, true, false);
730             let nested_shape = shape
731                 .block_indent(context.config.tab_spaces())
732                 .with_max_width(context.config);
733             let inner = wrap_macro_args(context, args, nested_shape)?;
734             Some((lhs, inner, rhs))
735         };
736
737         match *self {
738             MacroArgKind::MetaVariable(ty, ref name) => {
739                 Some(format!("${}:{}", name, ty.name.as_str()))
740             }
741             MacroArgKind::Repeat(delim_tok, ref args, ref another, ref tok) => {
742                 let (lhs, inner, rhs) = rewrite_delimited_inner(delim_tok, args)?;
743                 let another = another
744                     .as_ref()
745                     .and_then(|a| a.rewrite(context, shape, use_multiple_lines))
746                     .unwrap_or_else(|| "".to_owned());
747                 let repeat_tok = pprust::token_to_string(tok);
748
749                 Some(format!("${}{}{}{}{}", lhs, inner, rhs, another, repeat_tok))
750             }
751             MacroArgKind::Delimited(delim_tok, ref args) => {
752                 rewrite_delimited_inner(delim_tok, args)
753                     .map(|(lhs, inner, rhs)| format!("{}{}{}", lhs, inner, rhs))
754             }
755             MacroArgKind::Separator(ref sep, ref prefix) => Some(format!("{}{} ", prefix, sep)),
756             MacroArgKind::Other(ref inner, ref prefix) => Some(format!("{}{}", prefix, inner)),
757         }
758     }
759 }
760
761 #[derive(Debug, Clone)]
762 struct ParsedMacroArg {
763     kind: MacroArgKind,
764     span: Span,
765 }
766
767 impl ParsedMacroArg {
768     fn rewrite(
769         &self,
770         context: &RewriteContext<'_>,
771         shape: Shape,
772         use_multiple_lines: bool,
773     ) -> Option<String> {
774         self.kind.rewrite(context, shape, use_multiple_lines)
775     }
776 }
777
778 /// Parses macro arguments on macro def.
779 struct MacroArgParser {
780     /// Either a name of the next metavariable, a separator, or junk.
781     buf: String,
782     /// The start position on the current buffer.
783     lo: BytePos,
784     /// The first token of the current buffer.
785     start_tok: Token,
786     /// `true` if we are parsing a metavariable or a repeat.
787     is_meta_var: bool,
788     /// The position of the last token.
789     hi: BytePos,
790     /// The last token parsed.
791     last_tok: Token,
792     /// Holds the parsed arguments.
793     result: Vec<ParsedMacroArg>,
794 }
795
796 fn last_tok(tt: &TokenTree) -> Token {
797     match *tt {
798         TokenTree::Token(_, ref t) => t.clone(),
799         TokenTree::Delimited(_, delim, _) => Token::CloseDelim(delim),
800     }
801 }
802
803 impl MacroArgParser {
804     fn new() -> MacroArgParser {
805         MacroArgParser {
806             lo: BytePos(0),
807             hi: BytePos(0),
808             buf: String::new(),
809             is_meta_var: false,
810             last_tok: Token::Eof,
811             start_tok: Token::Eof,
812             result: vec![],
813         }
814     }
815
816     fn set_last_tok(&mut self, tok: &TokenTree) {
817         self.hi = tok.span().hi();
818         self.last_tok = last_tok(tok);
819     }
820
821     fn add_separator(&mut self) {
822         let prefix = if self.need_space_prefix() {
823             " ".to_owned()
824         } else {
825             "".to_owned()
826         };
827         self.result.push(ParsedMacroArg {
828             kind: MacroArgKind::Separator(self.buf.clone(), prefix),
829             span: mk_sp(self.lo, self.hi),
830         });
831         self.buf.clear();
832     }
833
834     fn add_other(&mut self) {
835         let prefix = if self.need_space_prefix() {
836             " ".to_owned()
837         } else {
838             "".to_owned()
839         };
840         self.result.push(ParsedMacroArg {
841             kind: MacroArgKind::Other(self.buf.clone(), prefix),
842             span: mk_sp(self.lo, self.hi),
843         });
844         self.buf.clear();
845     }
846
847     fn add_meta_variable(&mut self, iter: &mut Cursor) -> Option<()> {
848         match iter.next() {
849             Some(TokenTree::Token(sp, Token::Ident(ref ident, _))) => {
850                 self.result.push(ParsedMacroArg {
851                     kind: MacroArgKind::MetaVariable(*ident, self.buf.clone()),
852                     span: mk_sp(self.lo, sp.hi()),
853                 });
854
855                 self.buf.clear();
856                 self.is_meta_var = false;
857                 Some(())
858             }
859             _ => None,
860         }
861     }
862
863     fn add_delimited(&mut self, inner: Vec<ParsedMacroArg>, delim: DelimToken, span: Span) {
864         self.result.push(ParsedMacroArg {
865             kind: MacroArgKind::Delimited(delim, inner),
866             span,
867         });
868     }
869
870     // $($foo: expr),?
871     fn add_repeat(
872         &mut self,
873         inner: Vec<ParsedMacroArg>,
874         delim: DelimToken,
875         iter: &mut Cursor,
876         span: Span,
877     ) -> Option<()> {
878         let mut buffer = String::new();
879         let mut first = true;
880         let mut lo = span.lo();
881         let mut hi = span.hi();
882
883         // Parse '*', '+' or '?.
884         for tok in iter {
885             self.set_last_tok(&tok);
886             if first {
887                 first = false;
888                 lo = tok.span().lo();
889             }
890
891             match tok {
892                 TokenTree::Token(_, Token::BinOp(BinOpToken::Plus))
893                 | TokenTree::Token(_, Token::Question)
894                 | TokenTree::Token(_, Token::BinOp(BinOpToken::Star)) => {
895                     break;
896                 }
897                 TokenTree::Token(sp, ref t) => {
898                     buffer.push_str(&pprust::token_to_string(t));
899                     hi = sp.hi();
900                 }
901                 _ => return None,
902             }
903         }
904
905         // There could be some random stuff between ')' and '*', '+' or '?'.
906         let another = if buffer.trim().is_empty() {
907             None
908         } else {
909             Some(Box::new(ParsedMacroArg {
910                 kind: MacroArgKind::Other(buffer, "".to_owned()),
911                 span: mk_sp(lo, hi),
912             }))
913         };
914
915         self.result.push(ParsedMacroArg {
916             kind: MacroArgKind::Repeat(delim, inner, another, self.last_tok.clone()),
917             span: mk_sp(self.lo, self.hi),
918         });
919         Some(())
920     }
921
922     fn update_buffer(&mut self, lo: BytePos, t: &Token) {
923         if self.buf.is_empty() {
924             self.lo = lo;
925             self.start_tok = t.clone();
926         } else {
927             let needs_space = match next_space(&self.last_tok) {
928                 SpaceState::Ident => ident_like(t),
929                 SpaceState::Punctuation => !ident_like(t),
930                 SpaceState::Always => true,
931                 SpaceState::Never => false,
932             };
933             if force_space_before(t) || needs_space {
934                 self.buf.push(' ');
935             }
936         }
937
938         self.buf.push_str(&pprust::token_to_string(t));
939     }
940
941     fn need_space_prefix(&self) -> bool {
942         if self.result.is_empty() {
943             return false;
944         }
945
946         let last_arg = self.result.last().unwrap();
947         if let MacroArgKind::MetaVariable(..) = last_arg.kind {
948             if ident_like(&self.start_tok) {
949                 return true;
950             }
951             if self.start_tok == Token::Colon {
952                 return true;
953             }
954         }
955
956         if force_space_before(&self.start_tok) {
957             return true;
958         }
959
960         false
961     }
962
963     /// Returns a collection of parsed macro def's arguments.
964     fn parse(mut self, tokens: TokenStream) -> Option<Vec<ParsedMacroArg>> {
965         let mut iter = tokens.trees();
966
967         while let Some(tok) = iter.next() {
968             match tok {
969                 TokenTree::Token(sp, Token::Dollar) => {
970                     // We always want to add a separator before meta variables.
971                     if !self.buf.is_empty() {
972                         self.add_separator();
973                     }
974
975                     // Start keeping the name of this metavariable in the buffer.
976                     self.is_meta_var = true;
977                     self.lo = sp.lo();
978                     self.start_tok = Token::Dollar;
979                 }
980                 TokenTree::Token(_, Token::Colon) if self.is_meta_var => {
981                     self.add_meta_variable(&mut iter)?;
982                 }
983                 TokenTree::Token(sp, ref t) => self.update_buffer(sp.lo(), t),
984                 TokenTree::Delimited(delimited_span, delimited, ref tts) => {
985                     if !self.buf.is_empty() {
986                         if next_space(&self.last_tok) == SpaceState::Always {
987                             self.add_separator();
988                         } else {
989                             self.add_other();
990                         }
991                     }
992
993                     // Parse the stuff inside delimiters.
994                     let mut parser = MacroArgParser::new();
995                     parser.lo = delimited_span.open.lo();
996                     let delimited_arg = parser.parse(tts.clone())?;
997
998                     let span = delimited_span.entire();
999                     if self.is_meta_var {
1000                         self.add_repeat(delimited_arg, delimited, &mut iter, span)?;
1001                         self.is_meta_var = false;
1002                     } else {
1003                         self.add_delimited(delimited_arg, delimited, span);
1004                     }
1005                 }
1006             }
1007
1008             self.set_last_tok(&tok);
1009         }
1010
1011         // We are left with some stuff in the buffer. Since there is nothing
1012         // left to separate, add this as `Other`.
1013         if !self.buf.is_empty() {
1014             self.add_other();
1015         }
1016
1017         Some(self.result)
1018     }
1019 }
1020
1021 fn wrap_macro_args(
1022     context: &RewriteContext<'_>,
1023     args: &[ParsedMacroArg],
1024     shape: Shape,
1025 ) -> Option<String> {
1026     wrap_macro_args_inner(context, args, shape, false)
1027         .or_else(|| wrap_macro_args_inner(context, args, shape, true))
1028 }
1029
1030 fn wrap_macro_args_inner(
1031     context: &RewriteContext<'_>,
1032     args: &[ParsedMacroArg],
1033     shape: Shape,
1034     use_multiple_lines: bool,
1035 ) -> Option<String> {
1036     let mut result = String::with_capacity(128);
1037     let mut iter = args.iter().peekable();
1038     let indent_str = shape.indent.to_string_with_newline(context.config);
1039
1040     while let Some(ref arg) = iter.next() {
1041         result.push_str(&arg.rewrite(context, shape, use_multiple_lines)?);
1042
1043         if use_multiple_lines
1044             && (arg.kind.ends_with_space() || iter.peek().map_or(false, |a| a.kind.has_meta_var()))
1045         {
1046             if arg.kind.ends_with_space() {
1047                 result.pop();
1048             }
1049             result.push_str(&indent_str);
1050         } else if let Some(ref next_arg) = iter.peek() {
1051             let space_before_dollar =
1052                 !arg.kind.ends_with_space() && next_arg.kind.starts_with_dollar();
1053             let space_before_brace = next_arg.kind.starts_with_brace();
1054             if space_before_dollar || space_before_brace {
1055                 result.push(' ');
1056             }
1057         }
1058     }
1059
1060     if !use_multiple_lines && result.len() >= shape.width {
1061         None
1062     } else {
1063         Some(result)
1064     }
1065 }
1066
1067 // This is a bit sketchy. The token rules probably need tweaking, but it works
1068 // for some common cases. I hope the basic logic is sufficient. Note that the
1069 // meaning of some tokens is a bit different here from usual Rust, e.g., `*`
1070 // and `(`/`)` have special meaning.
1071 //
1072 // We always try and format on one line.
1073 // FIXME: Use multi-line when every thing does not fit on one line.
1074 fn format_macro_args(
1075     context: &RewriteContext<'_>,
1076     token_stream: TokenStream,
1077     shape: Shape,
1078 ) -> Option<String> {
1079     if !context.config.format_macro_matchers() {
1080         let span = span_for_token_stream(&token_stream);
1081         return Some(match span {
1082             Some(span) => context.snippet(span).to_owned(),
1083             None => String::new(),
1084         });
1085     }
1086     let parsed_args = MacroArgParser::new().parse(token_stream)?;
1087     wrap_macro_args(context, &parsed_args, shape)
1088 }
1089
1090 fn span_for_token_stream(token_stream: &TokenStream) -> Option<Span> {
1091     token_stream.trees().next().map(|tt| tt.span())
1092 }
1093
1094 // We should insert a space if the next token is a:
1095 #[derive(Copy, Clone, PartialEq)]
1096 enum SpaceState {
1097     Never,
1098     Punctuation,
1099     Ident, // Or ident/literal-like thing.
1100     Always,
1101 }
1102
1103 fn force_space_before(tok: &Token) -> bool {
1104     debug!("tok: force_space_before {:?}", tok);
1105
1106     match tok {
1107         Token::Eq
1108         | Token::Lt
1109         | Token::Le
1110         | Token::EqEq
1111         | Token::Ne
1112         | Token::Ge
1113         | Token::Gt
1114         | Token::AndAnd
1115         | Token::OrOr
1116         | Token::Not
1117         | Token::Tilde
1118         | Token::BinOpEq(_)
1119         | Token::At
1120         | Token::RArrow
1121         | Token::LArrow
1122         | Token::FatArrow
1123         | Token::BinOp(_)
1124         | Token::Pound
1125         | Token::Dollar => true,
1126         _ => false,
1127     }
1128 }
1129
1130 fn ident_like(tok: &Token) -> bool {
1131     match tok {
1132         Token::Ident(..) | Token::Literal(..) | Token::Lifetime(_) => true,
1133         _ => false,
1134     }
1135 }
1136
1137 fn next_space(tok: &Token) -> SpaceState {
1138     debug!("next_space: {:?}", tok);
1139
1140     match tok {
1141         Token::Not
1142         | Token::BinOp(BinOpToken::And)
1143         | Token::Tilde
1144         | Token::At
1145         | Token::Comma
1146         | Token::Dot
1147         | Token::DotDot
1148         | Token::DotDotDot
1149         | Token::DotDotEq
1150         | Token::Question => SpaceState::Punctuation,
1151
1152         Token::ModSep
1153         | Token::Pound
1154         | Token::Dollar
1155         | Token::OpenDelim(_)
1156         | Token::CloseDelim(_)
1157         | Token::Whitespace => SpaceState::Never,
1158
1159         Token::Literal(..) | Token::Ident(..) | Token::Lifetime(_) => SpaceState::Ident,
1160
1161         _ => SpaceState::Always,
1162     }
1163 }
1164
1165 /// Tries to convert a macro use into a short hand try expression. Returns `None`
1166 /// when the macro is not an instance of `try!` (or parsing the inner expression
1167 /// failed).
1168 pub(crate) fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext<'_>) -> Option<ast::Expr> {
1169     if &mac.node.path.to_string() == "try" {
1170         let ts: TokenStream = mac.node.tts.clone();
1171         let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
1172
1173         Some(ast::Expr {
1174             id: ast::NodeId::root(), // dummy value
1175             node: ast::ExprKind::Try(parser.parse_expr().ok()?),
1176             span: mac.span, // incorrect span, but shouldn't matter too much
1177             attrs: ThinVec::new(),
1178         })
1179     } else {
1180         None
1181     }
1182 }
1183
1184 fn macro_style(mac: &ast::Mac, context: &RewriteContext<'_>) -> DelimToken {
1185     let snippet = context.snippet(mac.span);
1186     let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
1187     let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
1188     let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
1189
1190     if paren_pos < bracket_pos && paren_pos < brace_pos {
1191         DelimToken::Paren
1192     } else if bracket_pos < brace_pos {
1193         DelimToken::Bracket
1194     } else {
1195         DelimToken::Brace
1196     }
1197 }
1198
1199 // A very simple parser that just parses a macros 2.0 definition into its branches.
1200 // Currently we do not attempt to parse any further than that.
1201 #[derive(new)]
1202 struct MacroParser {
1203     toks: Cursor,
1204 }
1205
1206 impl MacroParser {
1207     // (`(` ... `)` `=>` `{` ... `}`)*
1208     fn parse(&mut self) -> Option<Macro> {
1209         let mut branches = vec![];
1210         while self.toks.look_ahead(1).is_some() {
1211             branches.push(self.parse_branch()?);
1212         }
1213
1214         Some(Macro { branches })
1215     }
1216
1217     // `(` ... `)` `=>` `{` ... `}`
1218     fn parse_branch(&mut self) -> Option<MacroBranch> {
1219         let tok = self.toks.next()?;
1220         let (lo, args_paren_kind) = match tok {
1221             TokenTree::Token(..) => return None,
1222             TokenTree::Delimited(delimited_span, d, _) => (delimited_span.open.lo(), d),
1223         };
1224         let args = tok.joint();
1225         match self.toks.next()? {
1226             TokenTree::Token(_, Token::FatArrow) => {}
1227             _ => return None,
1228         }
1229         let (mut hi, body, whole_body) = match self.toks.next()? {
1230             TokenTree::Token(..) => return None,
1231             TokenTree::Delimited(delimited_span, ..) => {
1232                 let data = delimited_span.entire().data();
1233                 (
1234                     data.hi,
1235                     Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt),
1236                     delimited_span.entire(),
1237                 )
1238             }
1239         };
1240         if let Some(TokenTree::Token(sp, Token::Semi)) = self.toks.look_ahead(0) {
1241             self.toks.next();
1242             hi = sp.hi();
1243         }
1244         Some(MacroBranch {
1245             span: mk_sp(lo, hi),
1246             args_paren_kind,
1247             args,
1248             body,
1249             whole_body,
1250         })
1251     }
1252 }
1253
1254 // A parsed macros 2.0 macro definition.
1255 struct Macro {
1256     branches: Vec<MacroBranch>,
1257 }
1258
1259 // FIXME: it would be more efficient to use references to the token streams
1260 // rather than clone them, if we can make the borrowing work out.
1261 struct MacroBranch {
1262     span: Span,
1263     args_paren_kind: DelimToken,
1264     args: TokenStream,
1265     body: Span,
1266     whole_body: Span,
1267 }
1268
1269 impl MacroBranch {
1270     fn rewrite(
1271         &self,
1272         context: &RewriteContext<'_>,
1273         shape: Shape,
1274         multi_branch_style: bool,
1275     ) -> Option<String> {
1276         // Only attempt to format function-like macros.
1277         if self.args_paren_kind != DelimToken::Paren {
1278             // FIXME(#1539): implement for non-sugared macros.
1279             return None;
1280         }
1281
1282         // 5 = " => {"
1283         let mut result = format_macro_args(context, self.args.clone(), shape.sub_width(5)?)?;
1284
1285         if multi_branch_style {
1286             result += " =>";
1287         }
1288
1289         if !context.config.format_macro_bodies() {
1290             result += " ";
1291             result += context.snippet(self.whole_body);
1292             return Some(result);
1293         }
1294
1295         // The macro body is the most interesting part. It might end up as various
1296         // AST nodes, but also has special variables (e.g, `$foo`) which can't be
1297         // parsed as regular Rust code (and note that these can be escaped using
1298         // `$$`). We'll try and format like an AST node, but we'll substitute
1299         // variables for new names with the same length first.
1300
1301         let old_body = context.snippet(self.body).trim();
1302         let (body_str, substs) = replace_names(old_body)?;
1303         let has_block_body = old_body.starts_with('{');
1304
1305         let mut config = context.config.clone();
1306         config.set().hide_parse_errors(true);
1307
1308         result += " {";
1309
1310         let body_indent = if has_block_body {
1311             shape.indent
1312         } else {
1313             shape.indent.block_indent(&config)
1314         };
1315         let new_width = config.max_width() - body_indent.width();
1316         config.set().max_width(new_width);
1317
1318         // First try to format as items, then as statements.
1319         let new_body_snippet = match crate::format_snippet(&body_str, &config) {
1320             Some(new_body) => new_body,
1321             None => {
1322                 let new_width = new_width + config.tab_spaces();
1323                 config.set().max_width(new_width);
1324                 match crate::format_code_block(&body_str, &config) {
1325                     Some(new_body) => new_body,
1326                     None => return None,
1327                 }
1328             }
1329         };
1330         let new_body = wrap_str(
1331             new_body_snippet.snippet.to_string(),
1332             config.max_width(),
1333             shape,
1334         )?;
1335
1336         // Indent the body since it is in a block.
1337         let indent_str = body_indent.to_string(&config);
1338         let mut new_body = LineClasses::new(new_body.trim_end())
1339             .enumerate()
1340             .fold(
1341                 (String::new(), true),
1342                 |(mut s, need_indent), (i, (kind, ref l))| {
1343                     if !is_empty_line(l)
1344                         && need_indent
1345                         && !new_body_snippet.is_line_non_formatted(i + 1)
1346                     {
1347                         s += &indent_str;
1348                     }
1349                     (s + l + "\n", indent_next_line(kind, &l, &config))
1350                 },
1351             )
1352             .0;
1353
1354         // Undo our replacement of macro variables.
1355         // FIXME: this could be *much* more efficient.
1356         for (old, new) in &substs {
1357             if old_body.find(new).is_some() {
1358                 debug!("rewrite_macro_def: bailing matching variable: `{}`", new);
1359                 return None;
1360             }
1361             new_body = new_body.replace(new, old);
1362         }
1363
1364         if has_block_body {
1365             result += new_body.trim();
1366         } else if !new_body.is_empty() {
1367             result += "\n";
1368             result += &new_body;
1369             result += &shape.indent.to_string(&config);
1370         }
1371
1372         result += "}";
1373
1374         Some(result)
1375     }
1376 }
1377
1378 /// Format `lazy_static!` from https://crates.io/crates/lazy_static.
1379 ///
1380 /// # Expected syntax
1381 ///
1382 /// ```ignore
1383 /// lazy_static! {
1384 ///     [pub] static ref NAME_1: TYPE_1 = EXPR_1;
1385 ///     [pub] static ref NAME_2: TYPE_2 = EXPR_2;
1386 ///     ...
1387 ///     [pub] static ref NAME_N: TYPE_N = EXPR_N;
1388 /// }
1389 /// ```
1390 fn format_lazy_static(
1391     context: &RewriteContext<'_>,
1392     shape: Shape,
1393     ts: &TokenStream,
1394 ) -> Option<String> {
1395     let mut result = String::with_capacity(1024);
1396     let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
1397     let nested_shape = shape
1398         .block_indent(context.config.tab_spaces())
1399         .with_max_width(context.config);
1400
1401     result.push_str("lazy_static! {");
1402     result.push_str(&nested_shape.indent.to_string_with_newline(context.config));
1403
1404     macro_rules! parse_or {
1405         ($method:ident $(,)* $($arg:expr),* $(,)*) => {
1406             match parser.$method($($arg,)*) {
1407                 Ok(val) => {
1408                     if parser.sess.span_diagnostic.has_errors() {
1409                         parser.sess.span_diagnostic.reset_err_count();
1410                         return None;
1411                     } else {
1412                         val
1413                     }
1414                 }
1415                 Err(mut err) => {
1416                     err.cancel();
1417                     parser.sess.span_diagnostic.reset_err_count();
1418                     return None;
1419                 }
1420             }
1421         }
1422     }
1423
1424     while parser.token != Token::Eof {
1425         // Parse a `lazy_static!` item.
1426         let vis = crate::utils::format_visibility(context, &parse_or!(parse_visibility, false));
1427         parser.eat_keyword(keywords::Static);
1428         parser.eat_keyword(keywords::Ref);
1429         let id = parse_or!(parse_ident);
1430         parser.eat(&Token::Colon);
1431         let ty = parse_or!(parse_ty);
1432         parser.eat(&Token::Eq);
1433         let expr = parse_or!(parse_expr);
1434         parser.eat(&Token::Semi);
1435
1436         // Rewrite as a static item.
1437         let mut stmt = String::with_capacity(128);
1438         stmt.push_str(&format!(
1439             "{}static ref {}: {} =",
1440             vis,
1441             id,
1442             ty.rewrite(context, nested_shape)?
1443         ));
1444         result.push_str(&crate::expr::rewrite_assign_rhs(
1445             context,
1446             stmt,
1447             &*expr,
1448             nested_shape.sub_width(1)?,
1449         )?);
1450         result.push(';');
1451         if parser.token != Token::Eof {
1452             result.push_str(&nested_shape.indent.to_string_with_newline(context.config));
1453         }
1454     }
1455
1456     result.push_str(&shape.indent.to_string_with_newline(context.config));
1457     result.push('}');
1458
1459     Some(result)
1460 }
1461
1462 fn rewrite_macro_with_items(
1463     context: &RewriteContext<'_>,
1464     items: &[MacroArg],
1465     macro_name: &str,
1466     shape: Shape,
1467     style: DelimToken,
1468     position: MacroPosition,
1469     span: Span,
1470 ) -> Option<String> {
1471     let (opener, closer) = match style {
1472         DelimToken::Paren => ("(", ")"),
1473         DelimToken::Bracket => ("[", "]"),
1474         DelimToken::Brace => (" {", "}"),
1475         _ => return None,
1476     };
1477     let trailing_semicolon = match style {
1478         DelimToken::Paren | DelimToken::Bracket if position == MacroPosition::Item => ";",
1479         _ => "",
1480     };
1481
1482     let mut visitor = FmtVisitor::from_context(context);
1483     visitor.block_indent = shape.indent.block_indent(context.config);
1484     visitor.last_pos = context.snippet_provider.span_after(span, opener.trim());
1485     for item in items {
1486         let item = match item {
1487             MacroArg::Item(item) => item,
1488             _ => return None,
1489         };
1490         visitor.visit_item(&item);
1491     }
1492
1493     let mut result = String::with_capacity(256);
1494     result.push_str(&macro_name);
1495     result.push_str(opener);
1496     result.push_str(&visitor.block_indent.to_string_with_newline(context.config));
1497     result.push_str(visitor.buffer.trim());
1498     result.push_str(&shape.indent.to_string_with_newline(context.config));
1499     result.push_str(closer);
1500     result.push_str(trailing_semicolon);
1501     Some(result)
1502 }
1503
1504 const RUST_KEYWORDS: [keywords::Keyword; 60] = [
1505     keywords::PathRoot,
1506     keywords::DollarCrate,
1507     keywords::Underscore,
1508     keywords::As,
1509     keywords::Box,
1510     keywords::Break,
1511     keywords::Const,
1512     keywords::Continue,
1513     keywords::Crate,
1514     keywords::Else,
1515     keywords::Enum,
1516     keywords::Extern,
1517     keywords::False,
1518     keywords::Fn,
1519     keywords::For,
1520     keywords::If,
1521     keywords::Impl,
1522     keywords::In,
1523     keywords::Let,
1524     keywords::Loop,
1525     keywords::Match,
1526     keywords::Mod,
1527     keywords::Move,
1528     keywords::Mut,
1529     keywords::Pub,
1530     keywords::Ref,
1531     keywords::Return,
1532     keywords::SelfLower,
1533     keywords::SelfUpper,
1534     keywords::Static,
1535     keywords::Struct,
1536     keywords::Super,
1537     keywords::Trait,
1538     keywords::True,
1539     keywords::Type,
1540     keywords::Unsafe,
1541     keywords::Use,
1542     keywords::Where,
1543     keywords::While,
1544     keywords::Abstract,
1545     keywords::Become,
1546     keywords::Do,
1547     keywords::Final,
1548     keywords::Macro,
1549     keywords::Override,
1550     keywords::Priv,
1551     keywords::Typeof,
1552     keywords::Unsized,
1553     keywords::Virtual,
1554     keywords::Yield,
1555     keywords::Dyn,
1556     keywords::Async,
1557     keywords::Try,
1558     keywords::UnderscoreLifetime,
1559     keywords::StaticLifetime,
1560     keywords::Auto,
1561     keywords::Catch,
1562     keywords::Default,
1563     keywords::Existential,
1564     keywords::Union,
1565 ];