Skip to content

Conversation

@compiler-errors
Copy link
Contributor

@compiler-errors compiler-errors commented Mar 19, 2025

Right now we compute all associated types' values in push_program_clauses_for_associated_type_values_in_impls_of. This results in a cycle in rust-analyzer's implementation of RPITITs, since we need to be able to compute some associated type values before others. For example in:

trait Trait {
    type Assoc;
    fn foo() -> impl Sized;
}
impl Trait for () {
    type Assoc = ();
    fn foo() -> Self::Assoc;
}

When solving for the hidden type of fn fooI() -> impl Sized, we need to be able to normalize Self::Assoc before we know the value of the RPITIT in fn foo() -> impl Sized, which we are currently solving for :D

This PR adds associated_ty_from_impl, which will compute that the associated type that is actually the one we're assembling clauses for, rather than iterating through all associated types in the impl.

This should break the cycle and should be reasonably easy to implement in chalk, though I'd like to know if this method would be difficult to implement: cc @ChayimFriedman2

@ChayimFriedman2
Copy link
Contributor

I tested this and this fixes the cycle in r-a.

@compiler-errors
Copy link
Contributor Author

Updated the strategy: #t-compiler/help > Cycle on RPITITs computation @ 💬

@compiler-errors compiler-errors changed the title Add associated_ty_matches to Chalk db to avoid computing RPITIT associated types eagerly Add associated_ty_from_impl to Chalk db to avoid computing associated types eagerly Mar 20, 2025
@ChayimFriedman2
Copy link
Contributor

Can we get this merged and released? Acknowledged, some RPITITs still cause cycles, but this fixes at least some of them.

@jackh726 jackh726 merged commit d9bd343 into rust-lang:master Apr 9, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants