about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCaleb Cartwright <caleb.cartwright@outlook.com>2023-02-15 22:24:03 -0600
committerYacin Tmimi <yacintmimi@gmail.com>2023-02-16 00:03:36 -0500
commit34f9ca28f2f4ae3ac462c6dcd862b513e107e459 (patch)
tree76ada2bf2c1292795f838498d336a4eee742d60f
parent5391847ea5a12262704657a0708b1674fb344403 (diff)
downloadrust-34f9ca28f2f4ae3ac462c6dcd862b513e107e459.tar.gz
rust-34f9ca28f2f4ae3ac462c6dcd862b513e107e459.zip
fix: use correct span for struct generics
-rw-r--r--src/items.rs2
-rw-r--r--tests/target/issue_5691.rs16
2 files changed, 17 insertions, 1 deletions
diff --git a/src/items.rs b/src/items.rs
index 25e8a024857..3c5293b6bf5 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -1278,7 +1278,7 @@ pub(crate) fn format_struct_struct(
     let header_hi = struct_parts.ident.span.hi();
     let body_lo = if let Some(generics) = struct_parts.generics {
         // Adjust the span to start at the end of the generic arguments before searching for the '{'
-        let span = span.with_lo(generics.span.hi());
+        let span = span.with_lo(generics.where_clause.span.hi());
         context.snippet_provider.span_after(span, "{")
     } else {
         context.snippet_provider.span_after(span, "{")
diff --git a/tests/target/issue_5691.rs b/tests/target/issue_5691.rs
new file mode 100644
index 00000000000..e3aad15db0d
--- /dev/null
+++ b/tests/target/issue_5691.rs
@@ -0,0 +1,16 @@
+struct S<const C: usize>
+where
+    [(); { num_slots!(C) }]:, {
+    /* An asterisk-based, or a double-slash-prefixed, comment here is
+       required to trigger the fmt bug.
+
+    A single-line triple-slash-prefixed comment (with a field following it) is not enough - it will not trigger the fmt bug.
+
+    Side note: If you have a combination of two, or all three of the
+    above mentioned types of comments here, some of them disappear
+    after `cargo fmt`.
+
+    The bug gets triggered even if a field definition following the
+    (asterisk-based, or a double-slash-prefixed) comment, too.
+    */
+}