Skip to content

Do not register Self: AutoTrait when confirming auto trait (in old solver) #138249

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
Mar 13, 2025

Conversation

compiler-errors
Copy link
Member

Every built-in auto impl for a trait goal like Ty: Auto immediately registers another obligation of Ty: Auto as one of its nested obligations, leading to us stressing the cycle detection machinery a lot more than we need to. This is because all traits have a Self: Trait predicate.

To fix this, remove the call to impl_or_trait_obligations in vtable_auto_impl, since auto traits do not have where clauses.

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 Mar 9, 2025
@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 Mar 9, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 9, 2025
Do not register `Self: AutoTrait` when confirming auto trait

Every built-in auto impl for a trait goal like `Ty: Auto` immediately registers another obligation of `Ty: Auto` as one of its nested obligations, leading to us stressing the cycle detection machinery a lot more than we need to. This is because all traits have a `Self: Trait` predicate.

To fix this, remove the call to `impl_or_trait_obligations` in `vtable_auto_impl`, since auto traits do not have where clauses.

r? lcnr
@bors
Copy link
Collaborator

bors commented Mar 9, 2025

⌛ Trying commit 3129802 with merge 4e748ea...

@compiler-errors compiler-errors changed the title Do not register Self: AutoTrait when confirming auto trait Do not register Self: AutoTrait when confirming auto trait (in old solver) Mar 9, 2025
@bors
Copy link
Collaborator

bors commented Mar 9, 2025

☀️ Try build successful - checks-actions
Build commit: 4e748ea (4e748eada6c10c5cf70f4f463d72c402cedbe007)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4e748ea): comparison URL.

Overall result: ✅ improvements - 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 is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.7% [-5.2%, -0.2%] 23
Improvements ✅
(secondary)
-1.5% [-4.3%, -0.3%] 4
All ❌✅ (primary) -1.7% [-5.2%, -0.2%] 23

Max RSS (memory usage)

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

Cycles

Results (primary -2.4%, secondary -2.8%)

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)
-2.4% [-3.9%, -1.3%] 8
Improvements ✅
(secondary)
-2.8% [-3.7%, -1.9%] 2
All ❌✅ (primary) -2.4% [-3.9%, -1.3%] 8

Binary size

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

Bootstrap: 765.603s -> 765.667s (0.01%)
Artifact size: 361.95 MiB -> 362.01 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 9, 2025
@@ -463,29 +463,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);

assert_eq!(obligation.predicate.polarity(), ty::PredicatePolarity::Positive);
let trait_ref =
self.infcx.enter_forall_and_leak_universe(obligation.predicate).trait_ref;
let trait_obligations = self.impl_or_trait_obligations(
Copy link
Member Author

@compiler-errors compiler-errors Mar 9, 2025

Choose a reason for hiding this comment

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

To be very clear, this impl_or_trait_obligations returns a single predicate, which is this one here:

if tcx.is_trait(def_id) {
// For traits, add `Self: Trait` predicate. This is
// not part of the predicates that a user writes, but it
// is something that one must prove in order to invoke a
// method or project an associated type.
//
// In the chalk setup, this predicate is not part of the
// "predicates" for a trait item. But it is useful in
// rustc because if you directly (e.g.) invoke a trait
// method like `Trait::method(...)`, you must naturally
// prove that the trait applies to the types that were
// used, and adding the predicate into this list ensures
// that this is done.
//
// We use a DUMMY_SP here as a way to signal trait bounds that come
// from the trait itself that *shouldn't* be shown as the source of
// an obligation and instead be skipped. Otherwise we'd use
// `tcx.def_span(def_id);`
let span = DUMMY_SP;
result.predicates = tcx.arena.alloc_from_iter(
result
.predicates
.iter()
.copied()
.chain(std::iter::once((ty::TraitRef::identity(tcx, def_id).upcast(tcx), span))),
);
}

Arguably that predicate shouldn't even be in the predicates_of and perhaps should be put into the param_env of traits and trait items... but that seems a lot harder to do.

And we would still need to make sure to add the predicate in when calling methods and stuff.

@lcnr
Copy link
Contributor

lcnr commented Mar 10, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 10, 2025

📌 Commit 3129802 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-review Status: Awaiting review from the assignee but also interested parties. labels Mar 10, 2025
@bors
Copy link
Collaborator

bors commented Mar 13, 2025

⌛ Testing commit 3129802 with merge 961351c...

@bors
Copy link
Collaborator

bors commented Mar 13, 2025

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

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 13, 2025
@bors bors merged commit 961351c into rust-lang:master Mar 13, 2025
7 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Mar 13, 2025
Copy link

Post-merge analysis result

Test differences

  • aarch64-apple,aarch64-gnu,i686-gnu-2,i686-gnu-nopt-2,i686-mingw-2 (and 10 others)
    • compiler/rustc_trait_selection/src/traits/select/confirmation.rs - traits::select::confirmation::SelectionContext<'cx,'tcx>::equate_trait_refs (line 1004): [missing] -> ignore
    • compiler/rustc_trait_selection/src/traits/select/confirmation.rs - traits::select::confirmation::SelectionContext<'cx,'tcx>::equate_trait_refs (line 1018): ignore -> [missing]

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (961351c): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.8% [-5.2%, -0.2%] 21
Improvements ✅
(secondary)
-1.5% [-4.3%, -0.3%] 4
All ❌✅ (primary) -1.8% [-5.2%, -0.2%] 21

Max RSS (memory usage)

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

Cycles

Results (primary -2.6%, secondary -3.9%)

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)
-2.6% [-4.3%, -1.4%] 9
Improvements ✅
(secondary)
-3.9% [-3.9%, -3.9%] 1
All ❌✅ (primary) -2.6% [-4.3%, -1.4%] 9

Binary size

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

Bootstrap: 777.662s -> 777.245s (-0.05%)
Artifact size: 365.21 MiB -> 365.15 MiB (-0.02%)

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.

5 participants