From e97b55bbc3d558ce3e4258cd7e4430361f445715 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Wed, 27 May 2020 10:49:52 +0900 Subject: [PATCH] Use the span after generics and where clause --- rustfmt-core/rustfmt-lib/src/formatting/items.rs | 5 +++-- rustfmt-core/rustfmt-lib/tests/source/const_generics.rs | 4 ++++ rustfmt-core/rustfmt-lib/tests/target/const_generics.rs | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/rustfmt-core/rustfmt-lib/src/formatting/items.rs b/rustfmt-core/rustfmt-lib/src/formatting/items.rs index d72a42c1d47..f2c703a1845 100644 --- a/rustfmt-core/rustfmt-lib/src/formatting/items.rs +++ b/rustfmt-core/rustfmt-lib/src/formatting/items.rs @@ -1197,14 +1197,15 @@ pub(crate) fn format_trait( } result.push('{'); - let snippet = context.snippet(item.span); + let block_span = mk_sp(generics.where_clause.span.lo(), item.span.hi()); + let snippet = context.snippet(block_span); let open_pos = snippet.find_uncommented("{")? + 1; let outer_indent_str = offset.block_only().to_string_with_newline(context.config); if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) { let mut visitor = FmtVisitor::from_context(context); visitor.block_indent = offset.block_only().block_indent(context.config); - visitor.last_pos = item.span.lo() + BytePos(open_pos as u32); + visitor.last_pos = block_span.lo() + BytePos(open_pos as u32); for item in trait_items { visitor.visit_trait_item(item); diff --git a/rustfmt-core/rustfmt-lib/tests/source/const_generics.rs b/rustfmt-core/rustfmt-lib/tests/source/const_generics.rs index 420810b92ea..07ef99107a8 100644 --- a/rustfmt-core/rustfmt-lib/tests/source/const_generics.rs +++ b/rustfmt-core/rustfmt-lib/tests/source/const_generics.rs @@ -30,3 +30,7 @@ fn foo() { } type Foo = [i32; N + 1]; + +pub trait Foo: Bar<{Baz::COUNT}> { + const ASD: usize; +} diff --git a/rustfmt-core/rustfmt-lib/tests/target/const_generics.rs b/rustfmt-core/rustfmt-lib/tests/target/const_generics.rs index f60b7eb0880..f49c5a1bb10 100644 --- a/rustfmt-core/rustfmt-lib/tests/target/const_generics.rs +++ b/rustfmt-core/rustfmt-lib/tests/target/const_generics.rs @@ -22,3 +22,7 @@ fn foo() { } type Foo = [i32; N + 1]; + +pub trait Foo: Bar<{ Baz::COUNT }> { + const ASD: usize; +}