From e12ea9e3b7be9d63e8352cec99f1e3e7c1328e51 Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Thu, 23 Sep 2021 22:43:03 -0400 Subject: [PATCH 1/2] Update derive attibute span to start after opening '(' Fixes 4984 When parsing derive attributes we're only concerned about the traits and comments listed between the opening and closing parentheses. Derive attribute spans currently start at the '#'. Span starts here | v #[derive(...)] After this update the derive spans start after the opening '('. Span starts here | V #[derive(...)] --- src/attr.rs | 5 +++- tests/source/issue-4984/minimum_example.rs | 2 ++ tests/source/issue-4984/multi_line_derive.rs | 20 +++++++++++++++ tests/target/issue-4984/minimum_example.rs | 2 ++ tests/target/issue-4984/multi_line_derive.rs | 26 ++++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-4984/minimum_example.rs create mode 100644 tests/source/issue-4984/multi_line_derive.rs create mode 100644 tests/target/issue-4984/minimum_example.rs create mode 100644 tests/target/issue-4984/multi_line_derive.rs diff --git a/src/attr.rs b/src/attr.rs index 315eb10a9db..a5982820e3d 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -13,6 +13,7 @@ use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, use crate::overflow; use crate::rewrite::{Rewrite, RewriteContext}; use crate::shape::Shape; +use crate::source_map::SpanUtils; use crate::types::{rewrite_path, PathContext}; use crate::utils::{count_newlines, mk_sp}; @@ -116,7 +117,9 @@ fn format_derive( |span| span.lo(), |span| span.hi(), |span| Some(context.snippet(*span).to_owned()), - attr.span.lo(), + // We update derive attribute spans to start after the opening '(' + // This helps us focus parsing to just what's inside #[derive(...)] + context.snippet_provider.span_after(attr.span, "("), attr.span.hi(), false, ); diff --git a/tests/source/issue-4984/minimum_example.rs b/tests/source/issue-4984/minimum_example.rs new file mode 100644 index 00000000000..677f8737716 --- /dev/null +++ b/tests/source/issue-4984/minimum_example.rs @@ -0,0 +1,2 @@ +#[derive(/*Debug, */Clone)] +struct Foo; diff --git a/tests/source/issue-4984/multi_line_derive.rs b/tests/source/issue-4984/multi_line_derive.rs new file mode 100644 index 00000000000..73921dd1735 --- /dev/null +++ b/tests/source/issue-4984/multi_line_derive.rs @@ -0,0 +1,20 @@ +#[derive( +/* ---------- Some really important comment that just had to go inside the derive --------- */ +Debug, Clone, Eq, PartialEq, +)] +struct Foo { + a: i32, + b: T, +} + +#[derive( +/* + Some really important comment that just had to go inside the derive. + Also had to be put over multiple lines +*/ +Debug, Clone, Eq, PartialEq, +)] +struct Bar { + a: i32, + b: T, +} diff --git a/tests/target/issue-4984/minimum_example.rs b/tests/target/issue-4984/minimum_example.rs new file mode 100644 index 00000000000..f0599c5d694 --- /dev/null +++ b/tests/target/issue-4984/minimum_example.rs @@ -0,0 +1,2 @@ +#[derive(/*Debug, */ Clone)] +struct Foo; diff --git a/tests/target/issue-4984/multi_line_derive.rs b/tests/target/issue-4984/multi_line_derive.rs new file mode 100644 index 00000000000..5fbd9784adc --- /dev/null +++ b/tests/target/issue-4984/multi_line_derive.rs @@ -0,0 +1,26 @@ +#[derive( + /* ---------- Some really important comment that just had to go inside the derive --------- */ + Debug, + Clone, + Eq, + PartialEq, +)] +struct Foo { + a: i32, + b: T, +} + +#[derive( + /* + Some really important comment that just had to go inside the derive. + Also had to be put over multiple lines + */ + Debug, + Clone, + Eq, + PartialEq, +)] +struct Bar { + a: i32, + b: T, +} From f7cb26f12f22a917ee157dde2454d840044c304d Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Wed, 29 Sep 2021 14:18:54 -0400 Subject: [PATCH 2/2] Add additional test cases for issue 4984 --- tests/source/issue-4984/multiple_comments_within.rs | 8 ++++++++ tests/target/issue-4984/multiple_comments_within.rs | 11 +++++++++++ tests/target/issue-4984/should_not_change.rs | 5 +++++ 3 files changed, 24 insertions(+) create mode 100644 tests/source/issue-4984/multiple_comments_within.rs create mode 100644 tests/target/issue-4984/multiple_comments_within.rs create mode 100644 tests/target/issue-4984/should_not_change.rs diff --git a/tests/source/issue-4984/multiple_comments_within.rs b/tests/source/issue-4984/multiple_comments_within.rs new file mode 100644 index 00000000000..eb474a723d0 --- /dev/null +++ b/tests/source/issue-4984/multiple_comments_within.rs @@ -0,0 +1,8 @@ +#[derive( +/* ---------- Some really important comment that just had to go inside the derive --------- */ +Debug, Clone,/* Another comment */Eq, PartialEq, +)] +struct Foo { + a: i32, + b: T, +} diff --git a/tests/target/issue-4984/multiple_comments_within.rs b/tests/target/issue-4984/multiple_comments_within.rs new file mode 100644 index 00000000000..d2924f0d0f2 --- /dev/null +++ b/tests/target/issue-4984/multiple_comments_within.rs @@ -0,0 +1,11 @@ +#[derive( + /* ---------- Some really important comment that just had to go inside the derive --------- */ + Debug, + Clone, + /* Another comment */ Eq, + PartialEq, +)] +struct Foo { + a: i32, + b: T, +} diff --git a/tests/target/issue-4984/should_not_change.rs b/tests/target/issue-4984/should_not_change.rs new file mode 100644 index 00000000000..e46ee511084 --- /dev/null +++ b/tests/target/issue-4984/should_not_change.rs @@ -0,0 +1,5 @@ +#[derive(Clone, Debug, Eq, PartialEq)] +struct Foo; + +#[derive(Clone)] +struct Bar;