-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Amend style guide section for formatting where clauses in type aliases #114901
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
Changes from 2 commits
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 |
---|---|---|
|
@@ -367,26 +367,49 @@ where | |
## Type aliases | ||
|
||
Keep type aliases on one line when they fit. If necessary to break the line, do | ||
so after the `=`, and block-indent the right-hand side: | ||
so before the `=`, and block-indent the right-hand side: | ||
|
||
```rust | ||
pub type Foo = Bar<T>; | ||
|
||
// If multi-line is required | ||
type VeryLongType<T, U: SomeBound> = | ||
AnEvenLongerType<T, U, Foo<T>>; | ||
type VeryLongType<T, U: SomeBound> | ||
= AnEvenLongerType<T, U, Foo<T>>; | ||
``` | ||
|
||
Where possible avoid `where` clauses and keep type constraints inline. Where | ||
that is not possible split the line before and after the `where` clause (and | ||
split the `where` clause as normal), e.g., | ||
that is not possible, prefer a trailing `where` clause over one that precedes | ||
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. Part of what was also done in #113380 was removing some (but not all) usages of "prefer". My interpretation of this is that this one is indeed intentional, targeted more at the human developer, aware that the clause can go in either/both position, and trying to encourage the latter because we feel like the style guide has to provide rules for both positions? (edits: repeated typos, oh my) 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. I don't think we should use "prefer" in any case where we expect a tool to do any kind of enforcement. But in this case, I don't think we expect a tool to convert between the two 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. Tools should not convert. Prefix WC is an error in the positions where it matters, so maybe we don't even need to tell them about preference at all. Happy to reword or remove. 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. Tools, including rustfmt, certainly could and it's probably worth noting that during some of the earlier rfc and implementation discussions that was explicitly suggested/requested. I know there's differing philosophies around the should part of the question though 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. Well given that I'm probably in the position of having the least context and caring the least about this specific wording, one of y'all are free to suggest a rewording here. 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. I think (A) is the best way to go here, or to just remove the wording altogether and leave it up to the language to enforce what to do with where clause placement. The guide could perhaps just explain what to do in the cases of prefix, suffix, and prefix+suffix where clauses and take no opinions. 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. FWIW, I don't have objections to either (A) or (C) in isolation; however, I'd object to doing (C) if we're not waiting for a new style edition, because that would produce more churn. As for (B), I think the style guide should be providing documentation for how to format any language construct that exists in the language. If something hasn't been removed from the language, we should document how to handle it (even if the handling is (C), "convert it to something else"). We shouldn't leave things undocumented/implied. 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. OK, there's a bigger overriding consideration here. @compiler-errors just pointed out to me that the two locations have a semantic difference: That means we should just describe both positions, and state that since there's a semantic difference between them, the Rust style does not express a preference or suggest moving constraints from one position to the other. 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. Will update, which should resolve this convo. Hopefully after that we can kick off an FCP? 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. Yup, agreed with moving ahead
I know this is more a framing/way of thinking moving forward, but I wanted to add before I forget that one potential area of nuance to this is to what degree, if any, the guide must account for formatters that operate prior to the AST validation stage (as rustfmt does), and things that aren't technically valid/exist in the language but which do pass the initial parsing phase |
||
the type. Split the line before and after a trailing `where` clause (and split | ||
the `where` clause as normal) and indent before the `=` and type, e.g., | ||
|
||
```rust | ||
type VeryLongType<T, U> | ||
= AnEvenLongerType<T, U, Foo<T>> | ||
where | ||
T: U::AnAssociatedType, | ||
U: SomeBound; | ||
``` | ||
|
||
When there is a `where` clause before the type, format it normally, and break | ||
after the last clause. Do not indent before the `=` to leave it visually | ||
distinct from the indented clauses. | ||
|
||
``` | ||
type WithPrecedingWC<T, U> | ||
where | ||
T: U::AnAssociatedType, | ||
U: SomeBound, | ||
= AnEvenLongerType<T, U, Foo<T>>; | ||
|
||
// Or with both a preceding and trailing where clause. | ||
type WithPrecedingWC<T, U> | ||
where | ||
T: U::AnAssociatedType, | ||
U: SomeBound, | ||
= AnEvenLongerType<T, U, Foo<T>> | ||
where | ||
T: U::AnAssociatedType2, | ||
U: SomeBound2; | ||
``` | ||
|
||
## Associated types | ||
|
Uh oh!
There was an error while loading. Please reload this page.