diff --git a/src/macros.rs b/src/macros.rs index ae95acaf301..674a652322d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -129,6 +129,12 @@ fn rewrite_macro_name(path: &ast::Path, extra_ident: Option) -> Stri } } +// Use this on failing to format the macro call. +fn return_original_snippet_with_failure_marked(context: &RewriteContext, span: Span) -> Option { + context.macro_rewrite_failure.replace(true); + Some(context.snippet(span).to_owned()) +} + pub fn rewrite_macro( mac: &ast::Mac, extra_ident: Option, @@ -138,6 +144,9 @@ pub fn rewrite_macro( ) -> Option { context.inside_macro.replace(true); let result = rewrite_macro_inner(mac, extra_ident, context, shape, position); + if result.is_none() { + context.macro_rewrite_failure.replace(true); + } context.inside_macro.replace(false); result } @@ -196,7 +205,7 @@ pub fn rewrite_macro_inner( loop { match parse_macro_arg(&mut parser) { Some(arg) => arg_vec.push(arg), - None => return Some(context.snippet(mac.span).to_owned()), + None => return return_original_snippet_with_failure_marked(context, mac.span), } match parser.token { @@ -216,13 +225,13 @@ pub fn rewrite_macro_inner( break; } } - None => return Some(context.snippet(mac.span).to_owned()), + None => return return_original_snippet_with_failure_marked(context, mac.span), } } } - return Some(context.snippet(mac.span).to_owned()); + return return_original_snippet_with_failure_marked(context, mac.span); } - _ => return Some(context.snippet(mac.span).to_owned()), + _ => return return_original_snippet_with_failure_marked(context, mac.span), } parser.bump(); diff --git a/src/rewrite.rs b/src/rewrite.rs index 00c03a3c879..90b613df6c4 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -39,6 +39,8 @@ pub struct RewriteContext<'a> { // When rewriting chain, veto going multi line except the last element pub force_one_line_chain: RefCell, pub snippet_provider: &'a SnippetProvider<'a>, + // Used for `format_snippet` + pub(crate) macro_rewrite_failure: RefCell, pub(crate) report: FormatReport, }