Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix brace_style="PreferSameLine" with trailing_comma="Vertical" results #5556

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ fn lorem(ipsum: usize) {

fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div, {
T: Add + Sub + Mul + Div {
// body
}
```
Expand Down Expand Up @@ -290,7 +290,7 @@ struct Lorem {

struct Dolor<T>
where
T: Eq, {
T: Eq {
sit: T,
}
```
Expand Down
10 changes: 9 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2751,6 +2751,7 @@ fn rewrite_where_clause_rfc_style(
span_end: Option<BytePos>,
span_end_before_where: BytePos,
where_clause_option: WhereClauseOption,
brace_style: BraceStyle,
) -> Option<String> {
let (where_keyword, allow_single_line) = rewrite_where_keyword(
context,
Expand Down Expand Up @@ -2779,6 +2780,7 @@ fn rewrite_where_clause_rfc_style(
span_end,
where_clause_option,
force_single_line,
brace_style,
)?;

// 6 = `where `
Expand Down Expand Up @@ -2850,6 +2852,7 @@ fn rewrite_bounds_on_where_clause(
span_end: Option<BytePos>,
where_clause_option: WhereClauseOption,
force_single_line: bool,
brace_style: BraceStyle,
) -> Option<String> {
let span_start = predicates[0].span().lo();
// If we don't have the start of the next span, then use the end of the
Expand All @@ -2869,7 +2872,11 @@ fn rewrite_bounds_on_where_clause(
span_end,
false,
);
let comma_tactic = if where_clause_option.suppress_comma || force_single_line {
let comma_tactic = if where_clause_option.suppress_comma
|| force_single_line
|| brace_style == BraceStyle::PreferSameLine
&& context.config.trailing_comma() == SeparatorTactic::Vertical
{
SeparatorTactic::Never
} else {
context.config.trailing_comma()
Expand Down Expand Up @@ -2916,6 +2923,7 @@ fn rewrite_where_clause(
span_end,
span_end_before_where,
where_clause_option,
brace_style,
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/target/configs/brace_style/fn_prefer_same_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ fn lorem(ipsum: usize) {

fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div, {
T: Add + Sub + Mul + Div {
// body
}
2 changes: 1 addition & 1 deletion tests/target/configs/brace_style/item_prefer_same_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Lorem {

struct Dolor<T>
where
T: Eq, {
T: Eq {
sit: T,
}

Expand Down
8 changes: 4 additions & 4 deletions tests/target/fn-custom-6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn bar(

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb)
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
Copy link
Contributor

@ytmimi ytmimi Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically changing existing tests is not something we want to do and is usually a sign that the change needs to be version gated to prevent breaking formatting changes. However brace_style is an unstable option so the version gate might not be necessary in this case

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it if I have to! 👍

foo();
}

Expand All @@ -42,13 +42,13 @@ fn bar(
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee,
) where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
bar();
}

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
foo();
}

Expand All @@ -60,7 +60,7 @@ fn bar(
e: Eeeeeeeeeeeeeee,
) -> String
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
bar();
}

Expand Down
10 changes: 5 additions & 5 deletions tests/target/fn-custom-8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn bar(

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb)
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
foo();
}

Expand All @@ -42,13 +42,13 @@ fn bar(
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee,
) where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
bar();
}

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
foo();
}

Expand All @@ -60,7 +60,7 @@ fn bar(
e: Eeeeeeeeeeeeeee,
) -> String
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
bar();
}

Expand All @@ -72,6 +72,6 @@ trait Test {
fn bar(a: u8) -> String
where
Foo: foooo,
Bar: barrr, {
Bar: barrr {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-brace_style: AlwaysNextLine
// rustfmt-trailing_comma: Always

fn lorem<S, T,>(lorem: S, ipsum: T,)
where
S: Add + Sub,
T: Mul + Div,
{
// body
}
Comment on lines +4 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where clauses can appear in other contexts besides function definitions. Can we also add some examples with enum / struct definitions, and impl blocks to highlight how brace_style interacts with trailing_comma for those other language constructs?

Also including some tests with a single where clause bound (e.g. just S: Add + Sub) would also be helpful to highlight the difference between how a list of bounds VS a single bound is formatted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-brace_style: PreferSameLine
// rustfmt-trailing_comma: Always

fn lorem<S, T,>(lorem: S, ipsum: T,)
where
S: Add + Sub,
T: Mul + Div, {
// body
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-brace_style: SameLineWhere
// rustfmt-trailing_comma: Always

fn lorem<S, T,>(lorem: S, ipsum: T,)
where
S: Add + Sub,
T: Mul + Div,
{
Comment on lines +5 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I don't understand the brace_style config fully, but with an option like SameLineWhere I'd expect this to format with the opening brace on the same line as the last where clause bound. Do you think the current behavior is correct?

// body
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-brace_style: AlwaysNextLine
// rustfmt-trailing_comma: Vertical

fn lorem<S, T>(lorem: S, ipsum: T)
where
S: Add + Sub,
T: Mul + Div,
{
// body
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-brace_style: PreferSameLine
// rustfmt-trailing_comma: Vertical

fn lorem<S, T>(lorem: S, ipsum: T)
where
S: Add + Sub,
T: Mul + Div {
Comment on lines +6 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this output, I'd actually expect there to be a trailing comma after T: Mul + Div, since the where clause bounds were formatted vertically.

// body
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-brace_style: SameLineWhere
// rustfmt-trailing_comma: Vertical

fn lorem<S, T>(lorem: S, ipsum: T)
where
S: Add + Sub,
T: Mul + Div,
{
// body
}
8 changes: 4 additions & 4 deletions tests/target/item-brace-style-prefer-same-line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ mod M {

enum A<T>
where
T: Copy, {
T: Copy {
A,
}

struct B<T>
where
T: Copy, {
T: Copy {
b: i32,
}

enum C<T>
where
T: Copy, {}
T: Copy {}

struct D<T>
where
T: Copy, {}
T: Copy {}
}