-
-
Notifications
You must be signed in to change notification settings - Fork 1k
fix: preserve comments around associated type bounds #7014
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
Open
saberoueslati
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
saberoueslati:fix/issue-6761-associated-type-bounds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| #![feature(associated_type_defaults)] | ||
|
|
||
| // Exhaustive coverage of comments around associated-type bounds, keyed by | ||
| // position relative to the `:` / bounds / `where`, comment kind (line, | ||
| // block, multi-line line, multi-line block), and context (trait item with | ||
| // bounds, trait item without bounds, GAT, impl/free-alias shared path, | ||
| // bounds + RHS default). See #6761. | ||
| // | ||
| // Out of scope: a comment after bounds with no `where` clause at all (#6815) | ||
| // — that region has no span computed for it, so it needs separate handling. | ||
|
|
||
| // P1: between the ident/generics and the `:`. | ||
| trait P1 { | ||
| type A // line | ||
| : Bound | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type B /* inline */: Bound | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type C | ||
| // multi- | ||
| // line | ||
| : Bound | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type D | ||
| /* multi- | ||
| * line | ||
| */ | ||
| : Bound | ||
| where | ||
| Self: Copy; | ||
| } | ||
|
|
||
| // P2: between the `:` and the first bound. | ||
| trait P2 { | ||
| type A: // line | ||
| Bound | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type B: /* inline */ Bound | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type C: | ||
| // multi- | ||
| // line | ||
| Bound | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type D: | ||
| /* multi- | ||
| * line | ||
| */ | ||
| Bound | ||
| where | ||
| Self: Copy; | ||
| } | ||
|
|
||
| // P3: inside the bounds (generic args). Original #6761 repro. | ||
| trait P3 { | ||
| type A: Iterator< | ||
| // line | ||
| Item = Self, | ||
| > | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type B: Iterator< | ||
| /* inline */ | ||
| Item = Self, | ||
| > | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type C: Iterator< | ||
| // multi- | ||
| // line | ||
| Item = Self, | ||
| > | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type D: Iterator< | ||
| /* multi- | ||
| * line | ||
| */ | ||
| Item = Self, | ||
| > | ||
| where | ||
| Self: Copy; | ||
| } | ||
|
|
||
| // P4: between two bounds. | ||
| trait P4 { | ||
| type A: Bound + // line | ||
| Bound | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type B: Bound + /* inline */ Bound | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type C: Bound + | ||
| // multi- | ||
| // line | ||
| Bound | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type D: Bound + | ||
| /* multi- | ||
| * line | ||
| */ | ||
| Bound | ||
| where | ||
| Self: Copy; | ||
| } | ||
|
|
||
| // P5: after the last bound, before `where`. This is the duplication bug. | ||
| trait P5 { | ||
| type A: Bound | ||
| // line | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type B: Bound /* inline */ | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type C: Bound | ||
| // multi- | ||
| // line | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type D: Bound | ||
| /* multi- | ||
| * line | ||
| */ | ||
| where | ||
| Self: Copy; | ||
| } | ||
|
|
||
| // P5, same-line trailing comment: the reviewer's exact input on #7014. | ||
| // rustfmt moves this onto its own line. That is `rewrite_where_keyword`'s | ||
| // shared behavior for every item kind (see tests/target/issue-3194.rs for | ||
| // struct/enum and type-alias-where-clauses-with-comments.rs for aliases), | ||
| // not something this fix introduces. Pinned here so the behavior is explicit. | ||
| trait P5SameLine { | ||
| type A: Iterator<Item = u8> // trailing | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type B: Bound // trailing | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type C: Bound /* trailing */ | ||
| where | ||
| Self: Copy; | ||
| } | ||
|
|
||
| // P5, no bounds: exercises the preserved `generics.span.hi()` default. | ||
| trait P5NoBounds { | ||
| type A | ||
| // line | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type B /* inline */ | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type C | ||
| // multi- | ||
| // line | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type D | ||
| /* multi- | ||
| * line | ||
| */ | ||
| where | ||
| Self: Copy; | ||
| } | ||
|
|
||
| // Width: a colon comment must not wrap while the line still fits in | ||
| // max_width. `combine_strs_with_missing_comments` already measures the whole | ||
| // prefix, so it must be given the item shape, not the bounds-offset shape, | ||
| // or the prefix is charged twice and the line breaks early. | ||
| trait ColonCommentWidth { | ||
| type AssociatedTypeWithLongName: /* comment */ SomeModeratelyLongTraitName | ||
| where | ||
| Self: Copy; | ||
| } | ||
|
|
||
| // P6: after the `where` keyword, before the first predicate. | ||
| trait P6 { | ||
| type A: Bound | ||
| where | ||
| // line | ||
| Self: Copy; | ||
|
|
||
| type B: Bound | ||
| where | ||
| /* inline */ | ||
| Self: Copy; | ||
|
|
||
| type C: Bound | ||
| where | ||
| // multi- | ||
| // line | ||
| Self: Copy; | ||
|
|
||
| type D: Bound | ||
| where | ||
| /* multi- | ||
| * line | ||
| */ | ||
| Self: Copy; | ||
| } | ||
|
|
||
| // GAT with bounds: non-empty `generics.span`. | ||
| trait GatP5 { | ||
| type A<U>: Bound | ||
| // line | ||
| where | ||
| U: Copy; | ||
|
|
||
| type B<U>: Bound /* inline */ | ||
| where | ||
| U: Copy; | ||
| } | ||
|
|
||
| // Impl associated type: shared rewrite_ty/where-clause path. | ||
| impl ImplTarget for S { | ||
| type A<U> | ||
| // line, impl assoc type | ||
| where | ||
| U: Copy, | ||
| = Vec<U>; | ||
| } | ||
|
|
||
| // Free type alias: shared rewrite_ty/where-clause path. | ||
| type Free<U> | ||
| // line, free alias | ||
| where | ||
| U: Copy, | ||
| = Vec<U>; | ||
|
|
||
| // Bounds + RHS default, with comments (was Case 4, previously untested). | ||
| trait Rhs { | ||
| type A: Bound = () // line | ||
| where | ||
| Self: Copy; | ||
|
|
||
| type B: Bound /* inline */ = () | ||
| where | ||
| Self: Copy; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Case 1: exact issue repro (comment inside bounds + before-where clause). | ||
| pub trait Trait { | ||
| type I: Iterator< | ||
| // This is an item | ||
| Item = Self, | ||
| > | ||
| where | ||
| Self: Copy; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
These test cases are a good start, but I'd like them to be more exhaustive. We should make sure that we're testing both
// line commentsand/* inline comments */in all of these positions. And we should make sure that we're testing comments that span multiple lines.For example,
View changes since the review
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 expanded the coverage into a new
associated-type-bounds-with-comments.rsfixture.For each position around the associated-type bounds and
whereclause, it now covers://line comments/* inline */block comments//comments/* ... */commentsThe covered positions are before
:, after:, inside generic bounds, between bounds, after the final bound beforewhere, and afterwhere. The fixture also retains targeted cases for the no-bounds fallback, GATs, impl/free-alias paths, and associated-type defaults with an RHS.