Skip to content

Conversation

lcnr
Copy link
Contributor

@lcnr lcnr commented Sep 18, 2025

This strengthens the leak check to match the old trait solver. The new trait solver now also instantiates higher ranked goals in the same scope as candidate selection, so the leak check in each candidate detects placeholder errors involving this higher ranked goal.

E.g. let's look at tests/ui/higher-ranked/leak-check/leak-check-in-selection-2.rs

trait Trait<T, U> {}
impl<'a> Trait<&'a str, &'a str> for () {}
impl<'a> Trait<&'a str, String> for () {}
fn impls_trait<T: for<'a> Trait<&'a str, U>, U>() {}

fn main() {
    impls_trait::<(), _>();
}

Here proving (): for<'a> Trait<&'a str, ?u> via impl<'a> Trait<&'a str, &'a str> for () equates ?u with &'!a str which results in a leak check error as ?u cannot name 'a. If this leak check error happens while considering candidates we drop the first impl and infer ?u to String. If not, this remains ambiguous.

This behavior is a bit iffy, see the FCP proposal in #119820 for more details on why this current behavior is somewhat undesirable. However, considering placeholders from higher-ranked goals for candidate selection does allow more code to compile and a lot of the code feels like it should compile. This caused us to revert the change of #119820 in #127568.

I originally expected that we can avoid breakage with the new solver differently here, e.g. by considering OR-region constraints. However, doing so is a significant change and I don't have a great idea for how that should work. Matching the old solver behavior for now should not make this cleaner approach any more difficult in the future, so let's just go with what actually allows us to stabilize the new solver for now.

This PR changing the new solver to match the behavior of the old one wrt the leak check. As the new solver is already used by default in coherence, this allows more code to compile, see tests/ui/higher-ranked/leak-check/leak-check-in-selection-7-coherence.rs:

struct W<T, U>(T, U);

trait Trait<T> {}
// using this impl results in a higher-ranked region error.
impl<'a> Trait<W<&'a str, &'a str>> for () {}
impl<'a> Trait<W<&'a str, String>> for () {}

trait NotString {}
impl NotString for &str {}
impl NotString for u32 {}


trait Overlap<U> {}
impl<T: for<'a> Trait<W<&'a str, U>>, U> Overlap<U> for T {}
impl<U: NotString> Overlap<U> for () {}

fn main() {}

This behavior is quite arbitrary and not something I expect users to rely on in practice, however, it should still go through an FCP imo.

r? @BoxyUwU originally implemented by @compiler-errors in #136997. Closes rust-lang/trait-system-refactor-initiative#120.

@rustbot
Copy link
Collaborator

rustbot commented Sep 18, 2025

changes to inspect_obligations.rs

cc @compiler-errors, @lcnr

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Sep 18, 2025
@rustbot

This comment was marked as outdated.

@lcnr

This comment was marked as outdated.

@rustbot rustbot assigned BoxyUwU and unassigned fee1-dead Sep 18, 2025
@lcnr lcnr changed the title instantiate predicate binder without recanonicalizing goal -Znext-solver instantiate predicate binder without recanonicalizing goal Sep 18, 2025
@rustbot

This comment was marked as outdated.

@lcnr lcnr added T-types Relevant to the types team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 18, 2025
@lcnr
Copy link
Contributor Author

lcnr commented Sep 18, 2025

@rfcbot fcp merge

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Sep 18, 2025

Team member @lcnr has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 18, 2025
@lcnr
Copy link
Contributor Author

lcnr commented Sep 18, 2025

@bors rollup=never

@lcnr lcnr added the I-types-nominated Nominated for discussion during a types team meeting. label Sep 18, 2025
@BoxyUwU BoxyUwU added the S-no-work-capacity-tracking Status: Exempted from triagebot work capacity tracking. label Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-types-nominated Nominated for discussion during a types team meeting. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-no-work-capacity-tracking Status: Exempted from triagebot work capacity tracking. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-types Relevant to the types team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

higher-ranked goals in trait goal candidate selection
6 participants