Skip to content

Do not compute type_of for impl item if impl where clauses are unsatisfied #140276

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
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ where
.map(|pred| goal.with(cx, pred));
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);

// Bail if the nested goals don't hold here. This is to avoid unnecessarily
// computing the `type_of` query for associated types that never apply, as
// this may result in query cycles in the case of RPITITs.
// See <https://github.com/rust-lang/trait-system-refactor-initiative/issues/185>.
ecx.try_evaluate_added_goals()?;

// Add GAT where clauses from the trait's definition.
// FIXME: We don't need these, since these are the type's own WF obligations.
ecx.add_goals(
Expand Down
39 changes: 39 additions & 0 deletions tests/ui/async-await/in-trait/cycle-if-impl-doesnt-apply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//@ check-pass
//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] compile-flags: -Znext-solver
//@ edition: 2024

// Regression test for <https://github.com/rust-lang/trait-system-refactor-initiative/issues/185>.
// Avoid unnecessarily computing the RPITIT type of the first impl when checking the WF of the
// second impl, since the first impl relies on the hidden type of the second impl.

use std::future::Future;

trait Handler {}

struct W<T>(T);

trait SendTarget {
fn call(self) -> impl Future<Output = ()> + Send;
}

impl<T> SendTarget for W<T>
where
T: Handler + Send,
{
async fn call(self) {
todo!()
}
}

impl<T> SendTarget for T
where
T: Handler + Send,
{
async fn call(self) {
W(self).call().await
}
}

fn main() {}
32 changes: 32 additions & 0 deletions tests/ui/impl-trait/in-trait/cycle-if-impl-doesnt-apply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//@ check-pass
//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] compile-flags: -Znext-solver
//@ edition: 2024

// Regression test for <https://github.com/rust-lang/trait-system-refactor-initiative/issues/185>.
// Avoid unnecessarily computing the RPITIT type of the first impl when checking the WF of the
// second impl, since the first impl relies on the hidden type of the second impl.

trait Foo {
fn call(self) -> impl Send;
}

trait Nested {}
impl<T> Foo for T
where
T: Nested,
{
fn call(self) -> impl Sized {
NotSatisfied.call()
}
}

struct NotSatisfied;
impl Foo for NotSatisfied {
fn call(self) -> impl Sized {
todo!()
}
}

fn main() {}