From b558a7480ef746a69cbdc2f799a146df2c1280f6 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 27 Oct 2019 11:12:23 +0900 Subject: [PATCH 1/4] Backport: fix to extract comments to stop internal error (#3857) --- src/comment.rs | 10 ++++++++ src/items.rs | 51 ++++++++++++++++++++++++++++++++++++++ tests/source/issue-3851.rs | 22 ++++++++++++++++ tests/target/issue-3851.rs | 19 ++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 tests/source/issue-3851.rs create mode 100644 tests/target/issue-3851.rs diff --git a/src/comment.rs b/src/comment.rs index 4d565afc1e0..42bfa48037a 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -1988,6 +1988,16 @@ mod test { check("\"/* abc", "abc", Some(4)); } + #[test] + fn test_find_last_uncommented() { + fn check(haystack: &str, needle: &str, expected: Option) { + assert_eq!(expected, haystack.find_last_uncommented(needle)); + } + check("foo test bar test", "test", Some(13)); + check("test,", "test", Some(0)); + check("nothing", "test", None); + } + #[test] fn test_filter_normal_code() { let s = r#" diff --git a/src/items.rs b/src/items.rs index a76e88b77d4..eb5a9d79a7e 100644 --- a/src/items.rs +++ b/src/items.rs @@ -113,6 +113,57 @@ impl Rewrite for ast::Local { result.push_str(&infix); if let Some((init, _els)) = self.kind.init_else_opt() { + let base_span = if let Some(ref ty) = self.ty { + mk_sp(ty.span.hi(), self.span.hi()) + } else { + mk_sp(self.pat.span.hi(), self.span.hi()) + }; + + if let Some(offset) = context.snippet(base_span).find_uncommented("=") { + let base_span_lo = base_span.lo(); + + let assign_lo = base_span_lo + BytePos(offset as u32); + let comment_start_pos = if let Some(ref ty) = self.ty { + ty.span.hi() + } else { + self.pat.span.hi() + }; + let comment_before_assign = + context.snippet(mk_sp(comment_start_pos, assign_lo)).trim(); + + let assign_hi = base_span_lo + BytePos((offset + 1) as u32); + let rhs_span_lo = init.span.lo(); + let comment_end_pos = if init.attrs.is_empty() { + rhs_span_lo + } else { + let attr_span_lo = init.attrs.first().unwrap().span.lo(); + // for the case using block + // ex. let x = { #![my_attr]do_something(); } + if rhs_span_lo < attr_span_lo { + rhs_span_lo + } else { + attr_span_lo + } + }; + let comment_after_assign = + context.snippet(mk_sp(assign_hi, comment_end_pos)).trim(); + + if !comment_before_assign.is_empty() { + let new_indent_str = &pat_shape + .block_indent(0) + .to_string_with_newline(context.config); + result = format!("{}{}{}", comment_before_assign, new_indent_str, result); + } + + if !comment_after_assign.is_empty() { + let new_indent_str = + &shape.block_indent(0).to_string_with_newline(context.config); + result.push_str(new_indent_str); + result.push_str(comment_after_assign); + result.push_str(new_indent_str); + } + } + // 1 = trailing semicolon; let nested_shape = shape.sub_width(1)?; diff --git a/tests/source/issue-3851.rs b/tests/source/issue-3851.rs new file mode 100644 index 00000000000..c747a9d269e --- /dev/null +++ b/tests/source/issue-3851.rs @@ -0,0 +1,22 @@ +fn main() { + let before + // before_comment + = + if true { + 1.0 + }; + + let after = + // after_comment + if true { + 1.0 + }; + + let both + // before_comment + = + // after_comment + if true { + 1.0 + }; +} diff --git a/tests/target/issue-3851.rs b/tests/target/issue-3851.rs new file mode 100644 index 00000000000..975b8106469 --- /dev/null +++ b/tests/target/issue-3851.rs @@ -0,0 +1,19 @@ +fn main() { + // before_comment + let before = if true { + 1.0 + }; + + let after = + // after_comment + if true { + 1.0 + }; + + // before_comment + let both = + // after_comment + if true { + 1.0 + }; +} From d5a38ba2496205e4a0a152bbf3f8c488797b580c Mon Sep 17 00:00:00 2001 From: David Bar-On <61089727+davidBar-On@users.noreply.github.com> Date: Mon, 14 Dec 2020 20:38:11 +0200 Subject: [PATCH 2/4] Backport: Assignment indentation after one line comment (#4550) * Fix #4502 issue - assignment indentation after one line comment * Enhancement by using rewrite_assign_rhs_with_comments() * Changes per comments received --- src/items.rs | 71 ++++++++++++++++---------------------- tests/source/issue-4502.rs | 5 +++ tests/target/issue-3851.rs | 16 ++++----- tests/target/issue-4502.rs | 5 +++ 4 files changed, 48 insertions(+), 49 deletions(-) create mode 100644 tests/source/issue-4502.rs create mode 100644 tests/target/issue-4502.rs diff --git a/src/items.rs b/src/items.rs index eb5a9d79a7e..8cc97edd0a1 100644 --- a/src/items.rs +++ b/src/items.rs @@ -119,62 +119,51 @@ impl Rewrite for ast::Local { mk_sp(self.pat.span.hi(), self.span.hi()) }; - if let Some(offset) = context.snippet(base_span).find_uncommented("=") { - let base_span_lo = base_span.lo(); + let offset = context.snippet(base_span).find_uncommented("=")?; + let base_span_lo = base_span.lo(); - let assign_lo = base_span_lo + BytePos(offset as u32); - let comment_start_pos = if let Some(ref ty) = self.ty { - ty.span.hi() - } else { - self.pat.span.hi() - }; - let comment_before_assign = - context.snippet(mk_sp(comment_start_pos, assign_lo)).trim(); + let assign_lo = base_span_lo + BytePos(offset as u32); + let comment_start_pos = if let Some(ref ty) = self.ty { + ty.span.hi() + } else { + self.pat.span.hi() + }; + let comment_before_assign = context.snippet(mk_sp(comment_start_pos, assign_lo)).trim(); - let assign_hi = base_span_lo + BytePos((offset + 1) as u32); - let rhs_span_lo = init.span.lo(); - let comment_end_pos = if init.attrs.is_empty() { + let assign_hi = base_span_lo + BytePos((offset + 1) as u32); + let rhs_span_lo = init.span.lo(); + let comment_end_pos = if init.attrs.is_empty() { + rhs_span_lo + } else { + let attr_span_lo = init.attrs.first().unwrap().span.lo(); + // for the case using block + // ex. let x = { #![my_attr]do_something(); } + if rhs_span_lo < attr_span_lo { rhs_span_lo } else { - let attr_span_lo = init.attrs.first().unwrap().span.lo(); - // for the case using block - // ex. let x = { #![my_attr]do_something(); } - if rhs_span_lo < attr_span_lo { - rhs_span_lo - } else { - attr_span_lo - } - }; - let comment_after_assign = - context.snippet(mk_sp(assign_hi, comment_end_pos)).trim(); - - if !comment_before_assign.is_empty() { - let new_indent_str = &pat_shape - .block_indent(0) - .to_string_with_newline(context.config); - result = format!("{}{}{}", comment_before_assign, new_indent_str, result); + attr_span_lo } + }; - if !comment_after_assign.is_empty() { - let new_indent_str = - &shape.block_indent(0).to_string_with_newline(context.config); - result.push_str(new_indent_str); - result.push_str(comment_after_assign); - result.push_str(new_indent_str); - } + if !comment_before_assign.is_empty() { + let new_indent_str = &pat_shape + .block_indent(0) + .to_string_with_newline(context.config); + result = format!("{}{}{}", comment_before_assign, new_indent_str, result); } // 1 = trailing semicolon; let nested_shape = shape.sub_width(1)?; - - result = rewrite_assign_rhs( + result = rewrite_assign_rhs_with_comments( context, result, init, - &RhsAssignKind::Expr(&init.kind, init.span), nested_shape, + &RhsAssignKind::Expr(&init.kind, init.span), + RhsTactics::Default, + mk_sp(assign_hi, comment_end_pos), + true, )?; - // todo else } result.push(';'); diff --git a/tests/source/issue-4502.rs b/tests/source/issue-4502.rs new file mode 100644 index 00000000000..557ba4f7d88 --- /dev/null +++ b/tests/source/issue-4502.rs @@ -0,0 +1,5 @@ +fn main() { + let after = + // after_comment + 1.0; + } diff --git a/tests/target/issue-3851.rs b/tests/target/issue-3851.rs index 975b8106469..2a97a0d2e5a 100644 --- a/tests/target/issue-3851.rs +++ b/tests/target/issue-3851.rs @@ -5,15 +5,15 @@ fn main() { }; let after = - // after_comment - if true { - 1.0 - }; + // after_comment + if true { + 1.0 + }; // before_comment let both = - // after_comment - if true { - 1.0 - }; + // after_comment + if true { + 1.0 + }; } diff --git a/tests/target/issue-4502.rs b/tests/target/issue-4502.rs new file mode 100644 index 00000000000..e6e1adc8257 --- /dev/null +++ b/tests/target/issue-4502.rs @@ -0,0 +1,5 @@ +fn main() { + let after = + // after_comment + 1.0; +} From 7b92a06a34227d83ee576869d69be40ddf114b20 Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Mon, 28 Mar 2022 15:09:47 -0400 Subject: [PATCH 3/4] Add tests for issue 5017 Issue 5017 was fixed by backporting PR 3857 and PR 4550. --- tests/source/issue_5017.rs | 5 +++++ tests/target/issue_5017.rs | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 tests/source/issue_5017.rs create mode 100644 tests/target/issue_5017.rs diff --git a/tests/source/issue_5017.rs b/tests/source/issue_5017.rs new file mode 100644 index 00000000000..94e0193d036 --- /dev/null +++ b/tests/source/issue_5017.rs @@ -0,0 +1,5 @@ +fn main() { + let number = + // A comment + 1; +} diff --git a/tests/target/issue_5017.rs b/tests/target/issue_5017.rs new file mode 100644 index 00000000000..36ba5bb9e67 --- /dev/null +++ b/tests/target/issue_5017.rs @@ -0,0 +1,5 @@ +fn main() { + let number = + // A comment + 1; +} From b5225b7009a30a7c8eefd1628f2fdfeeb046b823 Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Tue, 19 Jul 2022 21:28:26 -0400 Subject: [PATCH 4/4] Add test for issue 4515 Closes 4515 After backporting 3857 and 4550 we were able to resolve 4515. --- tests/source/issue_4515.rs | 10 ++++++++++ tests/target/issue_4515.rs | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/source/issue_4515.rs create mode 100644 tests/target/issue_4515.rs diff --git a/tests/source/issue_4515.rs b/tests/source/issue_4515.rs new file mode 100644 index 00000000000..f70e32457ee --- /dev/null +++ b/tests/source/issue_4515.rs @@ -0,0 +1,10 @@ +pub unsafe fn as_chunks_mut_unchecked(&mut self) -> &mut [[T; N]] { + debug_assert_ne!(N, 0); + debug_assert_eq!(self.len() % N, 0); + let new_len = + // SAFETY: Our precondition is exactly what's needed to call this + unsafe { crate::intrinsics::exact_div(self.len(), N) }; + // SAFETY: We cast a slice of `new_len * N` elements into + // a slice of `new_len` many `N` elements chunks. + unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), new_len) } +} diff --git a/tests/target/issue_4515.rs b/tests/target/issue_4515.rs new file mode 100644 index 00000000000..fe45da9177f --- /dev/null +++ b/tests/target/issue_4515.rs @@ -0,0 +1,10 @@ +pub unsafe fn as_chunks_mut_unchecked(&mut self) -> &mut [[T; N]] { + debug_assert_ne!(N, 0); + debug_assert_eq!(self.len() % N, 0); + let new_len = + // SAFETY: Our precondition is exactly what's needed to call this + unsafe { crate::intrinsics::exact_div(self.len(), N) }; + // SAFETY: We cast a slice of `new_len * N` elements into + // a slice of `new_len` many `N` elements chunks. + unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), new_len) } +}