Skip to content

Rework dyn trait lowering to stop being so intertwined with trait alias expansion #133830

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

Merged
merged 1 commit into from
Jan 21, 2025

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Dec 3, 2024

This PR reworks the trait object lowering code to stop handling trait aliases so funky, and removes the TraitAliasExpander in favor of a much simpler design. This refactoring is important for making the code that I'm writing in #133397 understandable and easy to maintain, so the diagnostics regressions are IMO inevitable.

In the old trait object lowering code, we used to be a bit sloppy with the lists of traits in their unexpanded and expanded forms. This PR largely rewrites this logic to expand the trait aliases once and handle them more responsibly throughout afterwards.

Please review this with whitespace disabled.

r? lcnr

@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 3, 2024
@rustbot
Copy link
Collaborator

rustbot commented Dec 3, 2024

HIR ty lowering was modified

cc @fmease

@compiler-errors compiler-errors changed the title Dont use span as key when collecting missing associated types from dyn Rework dyn trait lowering to stop being so intertwined with trait alias expansion Dec 4, 2024
@rust-log-analyzer

This comment has been minimized.

@lcnr
Copy link
Contributor

lcnr commented Dec 4, 2024

second commit is more than I thought, might take a few days to fully review that one

@compiler-errors
Copy link
Member Author

@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 4, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 4, 2024
Rework dyn trait lowering to stop being so intertwined with trait alias expansion

This PR reworks the trait object lowering code to stop handling trait aliases so funky, and removes the `TraitAliasExpander` in favor of a much simpler design. This refactoring is important for making the code that I'm writing in rust-lang#133397 understandable and easy to maintain, so the diagnostics regressions are IMO inevitable.

##### First commit:

We used to use Span to maintain a per-principal list of missing associated types. We don't support multiple object principals, and I don't believe we intend to do so anytime soon. Let's remove this to greatly simplify the bookkeeping of this function.

##### Second commit:

In the old trait object lowering code, we used to be a bit sloppy with the lists of traits in their unexpanded and expanded forms. This PR largely rewrites this logic to expand the trait aliases *once* and handle them more responsibly throughout afterwards.

Please review both of these with whitespace disabled.

r? lcnr
@bors
Copy link
Collaborator

bors commented Dec 4, 2024

⌛ Trying commit af94314 with merge b020ef4...

@bors
Copy link
Collaborator

bors commented Dec 4, 2024

☀️ Try build successful - checks-actions
Build commit: b020ef4 (b020ef4229c5573e06021c20dc22f314db4f0782)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b020ef4): 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 (primary -1.0%)

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
Improvements ✅
(primary)
-1.0% [-1.0%, -1.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.0% [-1.0%, -1.0%] 1

Cycles

Results (secondary -1.5%)

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
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.5% [-1.5%, -1.5%] 1
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 767.015s -> 767.953s (0.12%)
Artifact size: 330.85 MiB -> 330.86 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 4, 2024
@compiler-errors compiler-errors force-pushed the span-key branch 2 times, most recently from c91411b to 9ec2929 Compare December 16, 2024 18:15
@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

a few nits and (more than a few) further cleanups, though all of them should hopefully be mechanical.

for (base_trait_ref, original_span) in regular_traits_refs_spans {
let base_pred: ty::Predicate<'tcx> = base_trait_ref.upcast(tcx);
let mut needed_associated_types = FxIndexSet::default();
if let Some((principal_trait, spans)) = &principal_trait {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move this into a separate fn collect_needed_associated_types function?

Copy link
Contributor

Choose a reason for hiding this comment

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

also, the comment further down is incorrect, preexisting, but pls fix

// Here, the user could theoretically write `dyn MyTrait<Output = X>`,

should be

// Here, the user could theoretically write `dyn MyTrait<MyOutput = X>`,

*span,
crate::errors::UnusedAssociatedTypeBounds { span: *span },
span,
crate::errors::UnusedAssociatedTypeBounds { span },
);
}
}

if let Err(guar) = self.check_for_required_assoc_tys(
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this should be part of fn check_for_required_assoc_tys which should get moved to this file?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not gonna do this I think.

@@ -254,7 +233,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.args
.iter()
.enumerate()
.skip(1) // Remove `Self` for `ExistentialPredicate`.
// Skip `Self`
.skip(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move the

// Verify that `dummy_self` did not leak inside default type parameters. This
// could not be done at path creation, since we need to see through trait aliases.

part into a sub-fn

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think I will do this.

)
let mut v = principal_trait_ref
.into_iter()
.chain(existential_projections)
.chain(auto_trait_predicates)
.collect::<SmallVec<[_; 8]>>();
v.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder()));
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move the // Use explicitly-specified region bound, unless the bound is missing. part into a sub-fn as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think I will do this.

@lcnr
Copy link
Contributor

lcnr commented Dec 17, 2024

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 17, 2024
@lcnr
Copy link
Contributor

lcnr commented Jan 21, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 21, 2025

📌 Commit 824a867 has been approved by lcnr

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 21, 2025
@bors
Copy link
Collaborator

bors commented Jan 21, 2025

⌛ Testing commit 824a867 with merge cd805f0...

@bors
Copy link
Collaborator

bors commented Jan 21, 2025

☀️ Test successful - checks-actions
Approved by: lcnr
Pushing cd805f0 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 21, 2025
@bors bors merged commit cd805f0 into rust-lang:master Jan 21, 2025
7 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 21, 2025
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (cd805f0): 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 (primary 5.1%)

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)
5.1% [5.1%, 5.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 5.1% [5.1%, 5.1%] 1

Cycles

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

Binary size

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

Bootstrap: 763.46s -> 764.328s (0.11%)
Artifact size: 326.05 MiB -> 326.09 MiB (0.01%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

6 participants