From 2bf67b6e5ca48479e5de9a14c49bd385d3d8285c Mon Sep 17 00:00:00 2001 From: rChaser53 Date: Wed, 4 Sep 2019 23:00:26 +0900 Subject: [PATCH] fix TrailingWhitespace when using line breaks in macros arguments (#3768) --- src/formatting.rs | 4 ++-- src/macros.rs | 5 +++++ src/rewrite.rs | 2 ++ src/visitor.rs | 8 +++++--- tests/target/issue-2916.rs | 2 ++ 5 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 tests/target/issue-2916.rs diff --git a/src/formatting.rs b/src/formatting.rs index ee44549b2bc..b58911705c0 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -188,7 +188,7 @@ fn format_file( format_lines( &mut visitor.buffer, &path, - &visitor.skipped_range, + &visitor.skipped_range.borrow(), &self.config, &self.report, ); @@ -203,7 +203,7 @@ fn format_file( self.report.add_macro_format_failure(); } self.report - .add_non_formatted_ranges(visitor.skipped_range.clone()); + .add_non_formatted_ranges(visitor.skipped_range.borrow().clone()); self.handler.handle_formatted_file( self.parse_session.source_map(), diff --git a/src/macros.rs b/src/macros.rs index de8e0651cd8..583844f1c5d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -181,6 +181,11 @@ fn return_macro_parse_failure_fallback( return trim_left_preserve_layout(context.snippet(span), indent, &context.config); } + context.skipped_range.borrow_mut().push(( + context.source_map.lookup_line(span.lo()).unwrap().line, + context.source_map.lookup_line(span.hi()).unwrap().line, + )); + // Return the snippet unmodified if the macro is not block-like Some(context.snippet(span).to_owned()) } diff --git a/src/rewrite.rs b/src/rewrite.rs index 606aafc9ebd..6f79cb14c60 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -1,6 +1,7 @@ // A generic trait to abstract the rewriting of an element (of the AST). use std::cell::RefCell; +use std::rc::Rc; use syntax::parse::ParseSess; use syntax::ptr; @@ -41,6 +42,7 @@ pub(crate) struct RewriteContext<'a> { pub(crate) macro_rewrite_failure: RefCell, pub(crate) report: FormatReport, pub(crate) skip_context: SkipContext, + pub(crate) skipped_range: Rc>>, } impl<'a> RewriteContext<'a> { diff --git a/src/visitor.rs b/src/visitor.rs index 58a94b12dfd..10c4e88756f 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::rc::Rc; use syntax::parse::ParseSess; use syntax::source_map::{self, BytePos, Pos, SourceMap, Span}; @@ -65,7 +66,7 @@ pub(crate) struct FmtVisitor<'a> { pub(crate) line_number: usize, /// List of 1-based line ranges which were annotated with skip /// Both bounds are inclusifs. - pub(crate) skipped_range: Vec<(usize, usize)>, + pub(crate) skipped_range: Rc>>, pub(crate) macro_rewrite_failure: bool, pub(crate) report: FormatReport, pub(crate) skip_context: SkipContext, @@ -652,7 +653,7 @@ pub(crate) fn push_skipped_with_span( let lo = std::cmp::min(attrs_end + 1, first_line); self.push_rewrite_inner(item_span, None); let hi = self.line_number + 1; - self.skipped_range.push((lo, hi)); + self.skipped_range.borrow_mut().push((lo, hi)); } pub(crate) fn from_context(ctx: &'a RewriteContext<'_>) -> FmtVisitor<'a> { @@ -684,7 +685,7 @@ pub(crate) fn from_source_map( is_if_else_block: false, snippet_provider, line_number: 0, - skipped_range: vec![], + skipped_range: Rc::new(RefCell::new(vec![])), macro_rewrite_failure: false, report, skip_context: Default::default(), @@ -877,6 +878,7 @@ pub(crate) fn get_context(&self) -> RewriteContext<'_> { macro_rewrite_failure: RefCell::new(false), report: self.report.clone(), skip_context: self.skip_context.clone(), + skipped_range: self.skipped_range.clone(), } } } diff --git a/tests/target/issue-2916.rs b/tests/target/issue-2916.rs new file mode 100644 index 00000000000..5a6471ca863 --- /dev/null +++ b/tests/target/issue-2916.rs @@ -0,0 +1,2 @@ +a_macro!(name, +);