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

Format trailing where clauses in type aliases #5887

Merged
merged 2 commits into from
Sep 12, 2024

Conversation

compiler-errors
Copy link
Member

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.

@compiler-errors compiler-errors changed the title Format trailing where clauses in rustfmt Format trailing where clauses in type aliases Aug 16, 2023
@calebcartwright
Copy link
Member

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

@ytmimi
Copy link
Contributor

ytmimi commented Aug 28, 2023

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.

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.

Copy link
Member

@fee1-dead fee1-dead left a 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
Comment on lines 1660 to 1685
let rhs_hi = ty
.as_ref()
.map_or(where_clauses.0.1.hi(), |ty| ty.span.hi());
Copy link
Member

@fee1-dead fee1-dead Dec 1, 2023

Choose a reason for hiding this comment

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

Suggested change
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();

Comment on lines +16 to +20
type Foo
= E
where
F: G,
H: I;
Copy link
Contributor

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;

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

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;

Copy link
Contributor

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

Comment on lines +7 to +14
type Foo
where
A: B,
C: D,
= E
where
F: G,
H: I;
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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

Copy link
Member

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 🤣

@calebcartwright
Copy link
Member

@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?

Copy link
Contributor

@ytmimi ytmimi left a 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

@ytmimi ytmimi added pr-ready-to-merge release-notes Needs an associated changelog entry and removed pr-not-reviewed labels Dec 10, 2023
@ytmimi
Copy link
Contributor

ytmimi commented Dec 10, 2023

@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?

@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 subtree push. Maybe we can talk it over on Zulip and find a time that works.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 25, 2024

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.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 26, 2024

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:
note the output is a diff of diffs, so it might be a little noisy, by the highlighting should help identify the changes

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;

@compiler-errors
Copy link
Member Author

compiler-errors commented Jan 26, 2024

@ytmimi: Yes, that formatting looks correct. When there is a where clause present, there's always a line break, then = and the type, then a break, then the where clauses.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 26, 2024

@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?

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?

@ytmimi
Copy link
Contributor

ytmimi commented Jan 26, 2024

@ytmimi: Yes, that formatting looks correct. When there is a where clause present, there's always a line break, then = and the type, then a break, then the where clauses.

Awesome, thank you for confirming! I'm already anticipating someone asking for a single-line formatting option 😅

@compiler-errors
Copy link
Member Author

@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?

@calebcartwright
Copy link
Member

@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

@ytmimi
Copy link
Contributor

ytmimi commented Jun 27, 2024

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 pre-2024-must-have

src/items.rs Outdated Show resolved Hide resolved
@calebcartwright
Copy link
Member

What I think I'd like to do is get this merged such that it's timed to land with the addition of the style_edition option so that we can have a single blog post highlighting both

@calebcartwright
Copy link
Member

@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

@compiler-errors
Copy link
Member Author

@calebcartwright: Done. Sorry, was asleep by the time you sent that message otherwise I'd've done it sooner 😆

@calebcartwright
Copy link
Member

ugh 😞 i clicked the wrong button and added a merge commit instead of rebasing

@compiler-errors
Copy link
Member Author

Do you want me to rebase?

@compiler-errors
Copy link
Member Author

Well, I went ahead and did it anyways.

@calebcartwright calebcartwright merged commit d6d9e76 into rust-lang:master Sep 12, 2024
26 checks passed
@calebcartwright
Copy link
Member

Thank you 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot handle generic associated type with where statement
6 participants