Set the flag in RewriteContext when rewriting macro call failed

This commit is contained in:
Seiichi Uchida 2018-06-07 12:32:10 +09:00
parent 5e59e686c1
commit 94e68b1eb6
2 changed files with 15 additions and 4 deletions

View File

@ -129,6 +129,12 @@ fn rewrite_macro_name(path: &ast::Path, extra_ident: Option<ast::Ident>) -> Stri
}
}
// Use this on failing to format the macro call.
fn return_original_snippet_with_failure_marked(context: &RewriteContext, span: Span) -> Option<String> {
context.macro_rewrite_failure.replace(true);
Some(context.snippet(span).to_owned())
}
pub fn rewrite_macro(
mac: &ast::Mac,
extra_ident: Option<ast::Ident>,
@ -138,6 +144,9 @@ pub fn rewrite_macro(
) -> Option<String> {
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();

View File

@ -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<bool>,
pub snippet_provider: &'a SnippetProvider<'a>,
// Used for `format_snippet`
pub(crate) macro_rewrite_failure: RefCell<bool>,
pub(crate) report: FormatReport,
}