Add a field in Summary for notiyfing about formatting failure of macro

This commit is contained in:
Seiichi Uchida 2018-06-07 12:32:58 +09:00
parent 94e68b1eb6
commit d1477ca1de
3 changed files with 39 additions and 4 deletions

View File

@ -23,6 +23,9 @@ pub struct Summary {
// Code is valid, but it is impossible to format it properly.
has_formatting_errors: bool,
// Code contains macro call that was unable to format.
pub(crate) has_macro_format_failure: bool,
// Failed a check, such as the license check or other opt-in checking.
has_check_errors: bool,
@ -80,6 +83,10 @@ impl Summary {
self.has_check_errors
}
pub(crate) fn has_macro_formatting_failure(&self) -> bool {
self.has_macro_format_failure
}
pub fn add_operational_error(&mut self) {
self.has_operational_errors = true;
}
@ -100,6 +107,10 @@ impl Summary {
self.has_diff = true;
}
pub(crate) fn add_macro_foramt_failure(&mut self) {
self.has_macro_format_failure = true;
}
pub fn has_no_errors(&self) -> bool {
!(self.has_operational_errors
|| self.has_parsing_errors

View File

@ -422,13 +422,14 @@ fn format_ast<F>(
config: &Config,
report: FormatReport,
mut after_file: F,
) -> Result<(FileMap, bool), io::Error>
) -> Result<(FileMap, bool, bool), io::Error>
where
F: FnMut(&FileName, &mut String, &[(usize, usize)], &FormatReport) -> Result<bool, io::Error>,
{
let mut result = FileMap::new();
// diff mode: check if any files are differing
let mut has_diff = false;
let mut has_macro_rewrite_failure = false;
let skip_children = config.skip_children();
for (path, module) in modules::list_files(krate, parse_session.codemap())? {
@ -472,10 +473,12 @@ where
}
};
has_macro_rewrite_failure |= visitor.macro_rewrite_failure;
result.push((path.clone(), visitor.buffer));
}
Ok((result, has_diff))
Ok((result, has_diff, has_macro_rewrite_failure))
}
/// Returns true if the line with the given line number was skipped by `#[rustfmt::skip]`.
@ -902,7 +905,7 @@ fn format_input_inner<T: Write>(
}
match format_result {
Ok((file_map, has_diff)) => {
Ok((file_map, has_diff, has_macro_rewrite_failure)) => {
if report.has_warnings() {
summary.add_formatting_error();
}
@ -911,6 +914,10 @@ fn format_input_inner<T: Write>(
summary.add_diff();
}
if has_macro_rewrite_failure {
summary.add_macro_foramt_failure();
}
Ok((summary, file_map, report))
}
Err(e) => Err((From::from(e), summary)),

View File

@ -70,6 +70,7 @@ pub struct FmtVisitor<'a> {
pub snippet_provider: &'a SnippetProvider<'a>,
pub line_number: usize,
pub skipped_range: Vec<(usize, usize)>,
pub macro_rewrite_failure: bool,
pub(crate) report: FormatReport,
}
@ -519,7 +520,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
// 1 = ;
let shape = self.shape().sub_width(1).unwrap();
let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
let rewrite = self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos));
self.push_rewrite(mac.span, rewrite);
}
@ -578,6 +579,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
snippet_provider,
line_number: 0,
skipped_range: vec![],
macro_rewrite_failure: false,
report,
}
}
@ -736,6 +738,20 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
}
}
pub fn with_context<F>(&mut self, f: F) -> Option<String>
where
F: Fn(&RewriteContext) -> Option<String>,
{
let mut result;
let macro_rewrite_failure = {
let context = self.get_context();
result = f(&context);
unsafe { *context.macro_rewrite_failure.as_ptr() }
};
self.macro_rewrite_failure |= macro_rewrite_failure;
result
}
pub fn get_context(&self) -> RewriteContext {
RewriteContext {
parse_session: self.parse_session,
@ -746,6 +762,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
is_if_else_block: RefCell::new(false),
force_one_line_chain: RefCell::new(false),
snippet_provider: self.snippet_provider,
macro_rewrite_failure: RefCell::new(false),
report: self.report.clone(),
}
}