-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Refactor AST trait bound modifiers #119163
Refactor AST trait bound modifiers #119163
Conversation
x64 size_of(ast::GenericBound) 64→72 @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
…fiers, r=<try> [perf] Refactor AST trait bound modifiers Context: https://github.com/rust-lang/rust/pull/119099/files#r1430749981, https://github.com/rust-lang/rust/pull/119099/files#r1430752116. r? `@ghost`
This comment has been minimized.
This comment has been minimized.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (af09d7b): comparison URL. Overall result: no relevant changes - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 673.565s -> 673.39s (-0.03%) |
Fixing some things up, making some matches exhaustive and writing some review comments, then this should be ready. |
724dc1d
to
5e4f12b
Compare
@@ -1425,19 +1425,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { | |||
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound { | |||
GenericBound::Trait( | |||
ty, | |||
modifier @ (TraitBoundModifier::None |
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.
Negligible behavior change in the error path: We now lower_poly_trait_ref
for ~const !
(master: TBM::MaybeConstNegative
) instead of mapping it to None
. We now only map ?
& ~const ?
to None
which seems more logical. In any case, ~const !
gets rejected in AST validation, shouldn't have any user-facing effect.
@@ -537,28 +537,19 @@ impl Rewrite for ast::Lifetime { | |||
impl Rewrite for ast::GenericBound { | |||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> { | |||
match *self { | |||
ast::GenericBound::Trait(ref poly_trait_ref, trait_bound_modifier) => { | |||
ast::GenericBound::Trait(ref poly_trait_ref, modifiers) => { |
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.
The rustfmt changes might be a bit controversial…
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.
@fmease I know this has already been merged, but please avoid making unnecessary changes to subtrees like rustfmt. It would have been more appropriate to open a PR for this directly in rustfmt. I would have asked for this to be reverted had I seen it earlier but given the holiday season this one slipped through.
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.
Sorry, but I don't understand what you mean by "unnecessary changes" in this particular case. I'm not sure how this PR could've been done without modifying rustfmt -- that's just part of the reality of rustfmt using data structures from rustc_ast
.
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'm certain @fmease or I will be happy to assist with a subtree sync if this ends up causing conflicts the next time you perform one.
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.
Maybe I don't fully understand the AST changes, but could the diff have been minimized? It seems like a lot larger of a diff than most other AST modifications.
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 don't believe this could be minimized in a way that doesn't affect code quality. This PR completely replaces the TraitBoundModifier
enum.
What's the concern here? Synchronization conflicts? Do you expect this code to have been modified on the rustfmt side?
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 you offering to help out with the sync if there are issue. I'm trying to get a subtree push done and this change jumped out as odd to me. It's not causing any issues, but it just looked like a much larger change than I would have expected. If this was the only way to rewrite the code then that's fair.
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'm glad it's not causing you too much trouble. While this diff might seem large, the point of this refactoring PR was to eliminate boilerplate like the one in rustfmt where one had to explicitly match on each kind to obtain a textual representation.
Thanks for reaching out! I won't make a habit of making such kind of changes to rustfmt from within the rust repo ;)
In case you do need any help with the sync or something similar, I'm definitely available and happy to help :)
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.
Thanks for the clarification. I think I was quick to judge this as an unnecessary change. After taking another look the changes seem fine. Very much appreciate you offering to help in case there is an issue 🙏🏼
This comment was marked as resolved.
This comment was marked as resolved.
Changes to the size of AST and/or HIR nodes. cc @nnethercote Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt |
r? compiler-errors or compiler depending on your current workload. |
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.
one comment
for b in bounds.iter() { | ||
if let GenericBound::Trait(ptr, ast::TraitBoundModifier::Maybe) = b { | ||
for bound in bounds.iter() { | ||
if let GenericBound::Trait(ptr, modifiers) = bound |
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.
why is this arm special?
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.
it's old syntactic cruft, I'm already in the process of writing a PR for removing this & other occurrences of it in the HIR pretty-printer lol
@bors r+ |
…fiers, r=compiler-errors Refactor AST trait bound modifiers Instead of having two types to represent trait bound modifiers in the parser & the AST (`parser::ty::BoundModifiers` & `ast::TraitBoundModifier`), only to map one to the other later, just use `parser::ty::BoundModifiers` (moved & renamed to `ast::TraitBoundModifiers`). The struct type is more extensible and easier to deal with (see [here](https://github.com/rust-lang/rust/pull/119099/files#r1430749981) and [here](https://github.com/rust-lang/rust/pull/119099/files#r1430752116) for context) since it more closely models what it represents: A compound of two kinds of modifiers, constness and polarity. Modeling this as an enum (the now removed `ast::TraitBoundModifier`) meant one had to add a new variant per *combination* of modifier kind, which simply isn't scalable and which lead to a lot of explicit non-DRY matches. NB: `hir::TraitBoundModifier` being an enum is fine since HIR doesn't need to worry representing invalid modifier combinations as those get rejected during AST validation thereby immensely cutting down the number of possibilities.
💥 Test timed out |
Huh? |
☀️ Test successful - checks-actions |
Finished benchmarking commit (aaef5fe): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 672.574s -> 673.306s (0.11%) |
…, r=compiler-errors Rid the AST & HIR pretty printer of cruft Found while working on rust-lang#119163. For `trait Trait: ?Sized {}` (semantically malformed), we currently output `trait Trait for ? Sized {}` (sic!) / `trait Trait for ? Sized { }` (sic!) if `-Zunpretty=expanded` / `-Zunpretty=hir` is passed. `trait Tr for Sized? {}` (rust-lang#15521) and later also `trait Tr for ?Sized {}` (I guess, rust-lang#20194) is former Rust syntax. Hence I'm removing these outdated branches. ~~This will conflict with rust-lang#119163, therefore marking this PR as blocked.~~ Rebased
Rollup merge of rust-lang#119169 - fmease:pretty-yeet-syntactic-cruft, r=compiler-errors Rid the AST & HIR pretty printer of cruft Found while working on rust-lang#119163. For `trait Trait: ?Sized {}` (semantically malformed), we currently output `trait Trait for ? Sized {}` (sic!) / `trait Trait for ? Sized { }` (sic!) if `-Zunpretty=expanded` / `-Zunpretty=hir` is passed. `trait Tr for Sized? {}` (rust-lang#15521) and later also `trait Tr for ?Sized {}` (I guess, rust-lang#20194) is former Rust syntax. Hence I'm removing these outdated branches. ~~This will conflict with rust-lang#119163, therefore marking this PR as blocked.~~ Rebased
…fiers, r=compiler-errors Refactor AST trait bound modifiers Instead of having two types to represent trait bound modifiers in the parser / the AST (`parser::ty::BoundModifiers` & `ast::TraitBoundModifier`), only to map one to the other later, just use `parser::ty::BoundModifiers` (moved & renamed to `ast::TraitBoundModifiers`). The struct type is more extensible and easier to deal with (see [here](https://github.com/rust-lang/rust/pull/119099/files#r1430749981) and [here](https://github.com/rust-lang/rust/pull/119099/files#r1430752116) for context) since it more closely models what it represents: A compound of two kinds of modifiers, constness and polarity. Modeling this as an enum (the now removed `ast::TraitBoundModifier`) meant one had to add a new variant per *combination* of modifier kind, which simply isn't scalable and which lead to a lot of explicit non-DRY matches. NB: `hir::TraitBoundModifier` being an enum is fine since HIR doesn't need to worry representing invalid modifier kind combinations as those get rejected during AST validation thereby immensely cutting down the number of possibilities.
Instead of having two types to represent trait bound modifiers in the parser / the AST (
parser::ty::BoundModifiers
&ast::TraitBoundModifier
), only to map one to the other later, just useparser::ty::BoundModifiers
(moved & renamed toast::TraitBoundModifiers
).The struct type is more extensible and easier to deal with (see here and here for context) since it more closely models what it represents: A compound of two kinds of modifiers, constness and polarity. Modeling this as an enum (the now removed
ast::TraitBoundModifier
) meant one had to add a new variant per combination of modifier kind, which simply isn't scalable and which lead to a lot of explicit non-DRY matches.NB:
hir::TraitBoundModifier
being an enum is fine since HIR doesn't need to worry representing invalid modifier kind combinations as those get rejected during AST validation thereby immensely cutting down the number of possibilities.