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

Refactor AST trait bound modifiers #119163

Merged
merged 1 commit into from
Dec 22, 2023

Conversation

fmease
Copy link
Member

@fmease fmease commented Dec 20, 2023

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 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.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 20, 2023
@fmease
Copy link
Member Author

fmease commented Dec 20, 2023

x64 size_of(ast::GenericBound) 64→72

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 20, 2023
@bors
Copy link
Contributor

bors commented Dec 20, 2023

⌛ Trying commit 724dc1d with merge af09d7b...

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 20, 2023
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Dec 20, 2023

☀️ Try build successful - checks-actions
Build commit: af09d7b (af09d7b63401dfa2221e508a51292b0b226f8dca)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (af09d7b): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking 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
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This 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.

mean range count
Regressions ❌
(primary)
0.8% [0.5%, 1.1%] 2
Regressions ❌
(secondary)
2.6% [1.5%, 3.8%] 2
Improvements ✅
(primary)
-3.2% [-3.2%, -3.2%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.6% [-3.2%, 1.1%] 3

Cycles

Results

This 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.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.6% [3.6%, 3.6%] 1
Improvements ✅
(primary)
-0.6% [-0.8%, -0.4%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.6% [-0.8%, -0.4%] 2

Binary size

Results

This 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.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Bootstrap: 673.565s -> 673.39s (-0.03%)
Artifact size: 312.83 MiB -> 312.83 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 20, 2023
@fmease
Copy link
Member Author

fmease commented Dec 20, 2023

Fixing some things up, making some matches exhaustive and writing some review comments, then this should be ready.

@fmease fmease force-pushed the refactor-ast-trait-bound-modifiers branch from 724dc1d to 5e4f12b Compare December 20, 2023 18:40
compiler/rustc_ast/src/ast.rs Show resolved Hide resolved
@@ -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
Copy link
Member Author

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) => {
Copy link
Member Author

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…

Copy link
Contributor

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.

cc: @compiler-errors @calebcartwright

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member

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?

Copy link
Contributor

@ytmimi ytmimi Dec 28, 2023

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.

Copy link
Member Author

@fmease fmease Dec 28, 2023

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 :)

Copy link
Contributor

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 🙏🏼

@fmease fmease changed the title [perf] Refactor AST trait bound modifiers Refactor AST trait bound modifiers Dec 20, 2023
@rustbot

This comment was marked as resolved.

@fmease fmease marked this pull request as ready for review December 20, 2023 18:57
@rustbot
Copy link
Collaborator

rustbot commented Dec 20, 2023

Changes to the size of AST and/or HIR nodes.

cc @nnethercote

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@fmease
Copy link
Member Author

fmease commented Dec 20, 2023

r? compiler-errors or compiler depending on your current workload.

@fmease fmease added the C-cleanup Category: PRs that clean code up or issues documenting cleanup. label Dec 20, 2023
Copy link
Member

@compiler-errors compiler-errors left a 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
Copy link
Member

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?

Copy link
Member Author

@fmease fmease Dec 20, 2023

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

@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Dec 20, 2023

📌 Commit 5e4f12b has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 20, 2023
@bors
Copy link
Contributor

bors commented Dec 20, 2023

⌛ Testing commit 5e4f12b with merge a4aae8c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 20, 2023
…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.
@bors
Copy link
Contributor

bors commented Dec 21, 2023

💥 Test timed out

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 21, 2023
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@fmease
Copy link
Member Author

fmease commented Dec 21, 2023

Huh?
@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 21, 2023
@bors
Copy link
Contributor

bors commented Dec 22, 2023

⌛ Testing commit 5e4f12b with merge aaef5fe...

@bors
Copy link
Contributor

bors commented Dec 22, 2023

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing aaef5fe to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 22, 2023
@bors bors merged commit aaef5fe into rust-lang:master Dec 22, 2023
12 checks passed
@rustbot rustbot added this to the 1.77.0 milestone Dec 22, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (aaef5fe): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This 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.

mean range count
Regressions ❌
(primary)
1.3% [1.0%, 1.5%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.0% [-1.0%, -1.0%] 1
Improvements ✅
(secondary)
-0.8% [-0.8%, -0.8%] 1
All ❌✅ (primary) 0.5% [-1.0%, 1.5%] 3

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results

This 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.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Bootstrap: 672.574s -> 673.306s (0.11%)
Artifact size: 312.78 MiB -> 312.81 MiB (0.01%)

@fmease fmease deleted the refactor-ast-trait-bound-modifiers branch December 22, 2023 08:00
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 22, 2023
…, 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
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 22, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants