-
Notifications
You must be signed in to change notification settings - Fork 893
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
Format trailing where clauses in type aliases #5887
Format trailing where clauses in type aliases #5887
Conversation
r? @ytmimi or @fee1-dead I'd like to hold off on merging this till the next-next sync & release (assuming we are still on track to get let-chains in quickly) given the diff it'll introduce on stable. Probably worth having common (and uncommon) comments in the test snippets too |
a3b562b
to
73cb507
Compare
Sounds good. I think we're still on track to get that out soon. I'll likely have that PR ready later this week. Given that I'll probably hold off on reviewing this until after the next sync. |
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.
implementation looks fine.
src/items.rs
Outdated
let rhs_hi = ty | ||
.as_ref() | ||
.map_or(where_clauses.0.1.hi(), |ty| ty.span.hi()); |
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.
let rhs_hi = ty | |
.as_ref() | |
.map_or(where_clauses.0.1.hi(), |ty| ty.span.hi()); | |
let rhs_hi = ty_opt.map_or(where_clauses.0.1, |ty| ty.span).hi(); |
type Foo | ||
= E | ||
where | ||
F: G, | ||
H: I; |
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.
@compiler-errors is there documentation in the style guide that you can link to for this?
I'm wondering why this wouldn't be formatted like:
type Foo = E
where
F: G,
H: I;
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.
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.
Specifically:
When there is a trailing where
clause after the type, and no where
clause
present before the type, break before the =
and indent. Then break before the
where
keyword and format the clauses normally, e.g.,
// With only a trailing where clause
type VeryLongType<T, U>
= AnEvenLongerType<T, U, Foo<T>>
where
T: U::AnAssociatedType,
U: SomeBound;
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.
Thank you. That's exactly what I was looking for
type Foo | ||
where | ||
A: B, | ||
C: D, | ||
= E | ||
where | ||
F: G, | ||
H: I; |
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.
This seems like an interesting case. Is it valid to have two where clauses?
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.
No, but it's not denied by the parser, so I think rustfmt shouldn't explode when we have it.
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 appreciate the clarification on this
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.
so I think rustfmt shouldn't explode when we have it.
I'd like to adopt this as a new motto for us 🤣
@ytmimi do you think we should pull this in as part of the next release? I imagine this would need to be prefaced with a blog post and some associated lead time due to the intro of formatting being applied for the first time? |
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've been taking a look at this for a while now and the implementation makes sense to me. I was trying to think of ways to simplify / improve what you've got here, but I don't think I can come up with any. I think we're good to move forward with the PR
@calebcartwright Yeah, I think we could pull this in for the next release. When did you want to do the sync? I'm still up for taking on the |
I'm expecting to see some changes now that we actually format trailing where clauses, but let's see what the Diff-Check reveals. Edit: The job failed but includes a lot of unrelated changes. Going to rebase and run it agin. |
Okay, This Diff-Check job shows the diffs I was expecting. @compiler-errors can when you have a chance, can you review the logs and double check that things are formatted here as expected based on the formatting rules you implemented. For your convenience I'm including the diffs inline: Diff in /tmp/rust-lang-rust-f0yK2vou/library/core/src/ops/async_function.rs:73:
where
<F as FnOnce<A>>::Output: Future,
{
- type CallFuture<'a> = <F as FnOnce<A>>::Output where Self: 'a;
+ type CallFuture<'a>
+ = <F as FnOnce<A>>::Output
+ where
+ Self: 'a;
extern "rust-call" fn async_call(&self, args: A) -> Self::CallFuture<'_> {
self.call(args)
Diff in /tmp/rust-lang-rust-f0yK2vou/library/core/src/ops/async_function.rs:85:
where
<F as FnOnce<A>>::Output: Future,
{
- type CallMutFuture<'a> = <F as FnOnce<A>>::Output where Self: 'a;
+ type CallMutFuture<'a>
+ = <F as FnOnce<A>>::Output
+ where
+ Self: 'a;
extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallMutFuture<'_> {
self.call_mut(args)
Diff in /tmp/rust-lang-rust-f0yK2vou/compiler/rustc_query_system/src/query/caches.rs:32:
pub struct DefaultCacheSelector<K>(PhantomData<K>);
impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<'tcx, V> for DefaultCacheSelector<K> {
- type Cache = DefaultCache<K, V>
+ type Cache
+ = DefaultCache<K, V>
where
V: Copy;
}
Diff in /tmp/rust-lang-rust-f0yK2vou/compiler/rustc_query_system/src/query/caches.rs:84:
pub struct SingleCacheSelector;
impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for SingleCacheSelector {
- type Cache = SingleCache<V>
+ type Cache
+ = SingleCache<V>
where
V: Copy;
}
Diff in /tmp/rust-lang-rust-f0yK2vou/compiler/rustc_query_system/src/query/caches.rs:126:
pub struct VecCacheSelector<K>(PhantomData<K>);
impl<'tcx, K: Idx, V: 'tcx> CacheSelector<'tcx, V> for VecCacheSelector<K> {
- type Cache = VecCache<K, V>
+ type Cache
+ = VecCache<K, V>
where
V: Copy;
}
Diff in /tmp/rust-lang-rust-f0yK2vou/compiler/rustc_query_system/src/query/caches.rs:177:
pub struct DefIdCacheSelector;
impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for DefIdCacheSelector {
- type Cache = DefIdCache<V>
+ type Cache
+ = DefIdCache<V>
where
V: Copy;
}
Diff in /tmp/rust-lang-rust-f0yK2vou/src/librustdoc/clean/types.rs:1059:
}
impl AttributesExt for [(Cow<'_, ast::Attribute>, Option<DefId>)] {
- type AttributeIterator<'a> = impl Iterator<Item = ast::NestedMetaItem> + 'a
- where Self: 'a;
- type Attributes<'a> = impl Iterator<Item = &'a ast::Attribute> + 'a
- where Self: 'a;
+ type AttributeIterator<'a>
+ = impl Iterator<Item = ast::NestedMetaItem> + 'a
+ where
+ Self: 'a;
+ type Attributes<'a>
+ = impl Iterator<Item = &'a ast::Attribute> + 'a
+ where
+ Self: 'a;
fn lists<'a>(&'a self, name: Symbol) -> Self::AttributeIterator<'a> {
AttributesExt::iter(self)
@ -7[54](https://github.com/rust-lang/rustfmt/actions/runs/7661954853/job/20882324738#step:4:55)9,0 +7[63](https://github.com/rust-lang/rustfmt/actions/runs/7661954853/job/20882324738#step:4:64)3,7 @@ Diff in /tmp/rust-lang-rust-f0yK2vou/src/tools/rust-analyzer/crates/parser/test_
Diff in /tmp/rust-lang-rust-f0yK2vou/src/tools/rust-analyzer/crates/parser/test_data/parser/inline/ok/0012_type_item_where_clause.rs:1:
-type Foo = () where Foo: Copy;
+type Foo
+ = ()
+where
+ Foo: Copy; |
@ytmimi: Yes, that formatting looks correct. When there is a where clause present, there's always a line break, then |
I think all the implementation work for this is done on the rustfmt side! @calebcartwright I missed the fact that you suggested we get a blog post together for this. I'd be happy to draft something in preparation for the release. Is the idea to have the blog post go out before the release? |
Awesome, thank you for confirming! I'm already anticipating someone asking for a |
@calebcartwright @ytmimi: It's been almost a year since this PR was opened. Is there any chance we could finally get this merged? What's the plan on doing the next rustfmt->rust sync? |
@ytmimi thoughts on how to label this one? it isn't technically part of the 2024 edition and can/should go before, but it is definitely something that can't slip past the 2024 edition |
Agreed that this one should get in no later than the 2024 edition, and really as soon as we can. Just brainstorming here, but maybe we can label it something like |
cdc4172
to
49931f0
Compare
What I think I'd like to do is get this merged such that it's timed to land with the addition of the |
@compiler-errors i hate to ask but would you have bandwidth to rebase? if not i understand (i know how busy you are) and i can grab your commits and pull them into a new branch but just wanted to check with you first |
49931f0
to
5dd874e
Compare
@calebcartwright: Done. Sorry, was asleep by the time you sent that message otherwise I'd've done it sooner 😆 |
ugh 😞 i clicked the wrong button and added a merge commit instead of rebasing |
Do you want me to rebase? |
cf88459
to
21314d1
Compare
Well, I went ahead and did it anyways. |
Thank you 🙏 |
Self-explanatory. cc rust-lang/rust#114901
This implementation is probably super busted. I have no idea what most of these options in rustfmt mean, lol.