-
Notifications
You must be signed in to change notification settings - Fork 901
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
Handle comments between Trait Generics and Bounds #4666
base: rustfmt-2.0.0-rc.2
Are you sure you want to change the base?
Changes from 3 commits
b63aeea
bceaf18
cc0daad
e4ab01e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Based on issue #2055: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the updates to the pre block span, we'd want to have tests here that included a mix of some other trait cases, including some with where clauses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Added some test cases mainly with |
||
pub trait A {} | ||
pub trait B {} | ||
pub trait C {} | ||
pub trait Foo1: | ||
A + C | ||
+ B | ||
{} | ||
pub trait Foo2: | ||
// A and C | ||
A + C | ||
+ B | ||
{} | ||
pub trait Foo3: | ||
/* A and C */ | ||
A + C | ||
+ B | ||
{} | ||
pub trait Foo4:// A and C | ||
A + C | ||
+ B | ||
{} | ||
pub trait Foo5:/* A and C */ | ||
A + C | ||
+ B | ||
{} | ||
pub trait Foo6:/* A and C */A + C + B{} | ||
pub trait Foo7: | ||
A+C | ||
// and B | ||
+B{} | ||
pub trait Foo8: | ||
// A and C | ||
A+C | ||
// and B | ||
+B{} | ||
|
||
// Other cases | ||
trait Person{ | ||
fn name(&self) -> String; | ||
} | ||
/*comment1*/trait Person{ | ||
fn name(&self) -> String; | ||
} | ||
trait Student:/* comment1 */Person/* comment2 */{ | ||
fn university(&self) -> String; | ||
} | ||
trait Programmer/* comment1 */{ | ||
fn fav_language(&self) -> String; | ||
} | ||
trait CompSciStudent1:/* comment1 */Programmer + Student/* comment2 */{ | ||
fn git_username(&self) -> String; | ||
} | ||
trait CompSciStudent2:/* comment1 Longggggggggggggggggggggggggggggggggggggggggggggggggg */Programmer + Student/* comment2 */{ | ||
fn git_username(&self) -> String; | ||
} | ||
trait CompSciStudent3:// comment1 | ||
Programmer + Student/* comment2 */{ | ||
fn git_username(&self) -> String; | ||
} | ||
trait CompSciStudent4:// comment1 Longgggggggggggggggggggggggggggggggggggggggggggggggggg | ||
Programmer + Student/* comment2 */{ | ||
fn git_username(&self) -> String; | ||
} | ||
|
||
// Comment before Ident | ||
trait /* comment1 */ Person { | ||
fn fav_language(&self) -> String; | ||
} | ||
trait // comment1 | ||
Person { | ||
fn fav_language(&self) -> String; | ||
} | ||
trait /* comment 1 */ Programmer /* comment2 */ { | ||
fn fav_language(&self) -> String; | ||
} | ||
trait /* comment1 */ CompSciStudent1: /* comment2 */ Programmer + Student /* comment3 */ { | ||
fn git_username(&self) -> String; | ||
} | ||
|
||
// Traits with where and comments | ||
trait Bar where Self: Sized, Option<Self>: Foo | ||
{} | ||
/*comment0*/trait Bar/*comment1*/where Self: Sized/*comment2*/,/*comment3*/Option<Self>: Foo/*comment4*/ | ||
{} | ||
trait Bar//comment1 Longgggggggggggggggggggggggggggggggggggggggggggggggggg | ||
where Self: Sized/*comment2*/,/*comment3*/Option<Self>: Foo/*comment4*/ | ||
{} | ||
trait Bar/*comment1*/where Self: Sized/*comment2*/,/*comment3*/Option<Self>: Foo//comment4 Longgggggggggggggggggggggggggggggggggggggggggggggggggg | ||
{} | ||
trait Bar/*comment1 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/where Self: Sized/*comment2 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/,/*comment3 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/Option<Self>: Foo/*comment4 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/ | ||
{} | ||
trait ConstCheck<T>:/*comment1*/Foo where T: Baz/*comment2*/{ | ||
const J: i32; | ||
} | ||
|
||
// Some other trait cases with comments | ||
/*comment0*/auto trait Example/*comment1*/{} | ||
pub unsafe auto trait PubUnsafeExample/*comment1*/{} | ||
pub unsafe auto trait PubUnsafeExample// comment1 | ||
{} | ||
trait Foo/*comment1*/{ type Bar: Baz; type Inner: Foo = Box< Foo >; } | ||
pub trait Iterator/*comment1*/{ | ||
type Item; | ||
fn next(&mut self) -> Option<Self::Item>; | ||
} | ||
pub trait Iterator//comment1 | ||
{ | ||
type Item; | ||
fn next(&mut self) -> Option<Self::Item>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the overall idea here is good, but think we're doing this in the wrong place in the codebase. Traits are by definition Items, not Expressions, and by design (encapsulation, etc.) we'd only ever need to call this from one place: the corresponding formatting flow for traits in items.rs.
As such, this function feels quite out of place here in expr.rs and the logic would be best placed back in items.
Additionally (regarding changes above this one), and a bit of a nit, it's quite reasonable to have one of these rewrite functions allow the caller to specify the separator, but that doesn't mean we can't also have a helper function that avoids the duplicative calls that each have to specify the assignment operator as the separator (which would be introduced here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved
rewrite_trait_rhs_with_comments
to items.rs.Added a helper function
rewrite_assign_rhs_expr
that specifies the'='
separator.