Skip to content

re-equate the object bound's trait ref with the goal's when considering object bounds - #160831

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
sjwang05:equate-again
Sep 10, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
sjwang05:equate-again

Conversation

@sjwang05

@sjwang05 sjwang05 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

rust-lang/trait-system-refactor-initiative#295 bisects to #152859, which changed probe_and_consider_object_bound_candidate to pass the trait object's trait ref in a binder to predicates_for_object_candidate, which calls instantiate_binder_with_infer on the binder.

Re-instantiating the binder like this, however, loses the constraint that 'x: 'static, even though earlier in match_assumption we actually did equate the object trait ref with the goal's trait ref, which gave us the eq('x, 'static) relationship we needed. However, the new inference var has no relationship with the equality we established; hence we lose 'x: 'static and therefore Self: 'static and 's: 'static.

We therefore inline match_assumption and directly equate the assumption's trait ref with the goal's before passing the assumption to predicates_for_object_candidate instead and remove the re-instantiation.

fixes rust-lang/trait-system-refactor-initiative#295

cc @lcnr

  • I did not use an LLM to create a change in this PR.
  • I used an LLM to create a change in this PR, and I have explained below how it was used.

@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 Aug 10, 2026
@rustbot

rustbot commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 19 candidates

@sjwang05

sjwang05 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor Author

I was torn between this and modifying the then() closure to pass the already instantiated and equated assumption as an arg, in addition to ecx, but that would've had us modifying... like... 5 other unrelated call sites just to pass that arg and subsequently ignore it, which felt like too big of an impact for an otherwise relatively self-contained change.

@lcnr lcnr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was somewhat confused by why exactly this happens and why the status quo is not enough, cc #124336

The builtin impl here is

trait Trait<'a>: 'a {}
impl<'s, 'hr> Trait<'hr> for dyn for<'a> Trait<'a> + 's
where
    dyn for<'a> Trait<'a> + 's: 'hr,
{
}

This is one of these fun issues where now that I do get this, it feels obvious why this breaks. We need to make sure 'hr is actually constrained by the goal we're trying to prove

View changes since this review

unreachable!("expected trait or projection predicate as an assumption")
}
});
let trait_ref = ecx.instantiate_binder_with_infer(trait_ref);

@lcnr lcnr Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This feels insufficient, at least once we allow GATs in object types. We need to make sure all higher-ranked vars of the assumption get fully constrained. E.g. with

use std::any::Any;
trait Trait {
    type Assoc<'a>: 'a;
}

fn tr<T: Trait>(x: T::Assoc<'static>) -> Box<dyn Any> { Box::new(x) }
fn foo<'s>(x: &'s str) -> Box<dyn Any>
where
    dyn for<'hr> Trait<Assoc<'hr> = &'s str>: Trait<Assoc<'static> = &'s str>,
{
    tr::<dyn for<'hr> Trait<Assoc<'hr> = &'s str>>(x)
}

I feel like maybe we should actually just inline probe_and_match_goal_against_assumption here and then manually instantiate the binder before calling match_assumption? 🤔 this is very subtle.

also, please put a comment there explaining why this is important

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I inlined both probe_and_match_goal_against_assumption and match_assumption here, since match_assumption takes an I::Clause and always tries to instantiate the binder. I felt that changing the signature would be too messy, and instantiating it ourselves before re-binding with Binder::dummy felt kind of ick. I also added a comment explaining why everything is inlined + why we need to equate directly in the function; hopefully it's clear-ish.

I'm not sure what to do about the GAT case yet, since the 'hr does appear in the assumption's binder, but the 'static we want to equate it to doesn't appear in the goal itself. There is a ProjectionPredicate sitting in the param_env that does mention the 'static, but I don't know how we might be able to actually use it 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure what to do about the GAT case yet, since the 'hr does appear in the assumption's binder, but the 'static we want to equate it to doesn't appear in the goal itself. There is a ProjectionPredicate sitting in the param_env that does mention the 'static, but I don't know how we might be able to actually use it 🤔

want to just add it as a test for now so that we don't accidentally allow an unsoundness here?

@lcnr lcnr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

r=me after adding the test. Don't think we need to do anything specific there for now

View changes since this review

unreachable!("expected trait or projection predicate as an assumption")
}
});
let trait_ref = ecx.instantiate_binder_with_infer(trait_ref);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure what to do about the GAT case yet, since the 'hr does appear in the assumption's binder, but the 'static we want to equate it to doesn't appear in the goal itself. There is a ProjectionPredicate sitting in the param_env that does mention the 'static, but I don't know how we might be able to actually use it 🤔

want to just add it as a test for now so that we don't accidentally allow an unsoundness here?

Comment on lines +139 to +144
// If we instead passed the binder to predicates_for_object_candidate and let
// it instantiate the binder itself, we would lose '?infer := 'static, since
// predicates_for_object_candidate has no way of equating the trait ref with
// the goal. We would simply have 'hr := '?infer, giving us the constraint
// `?temp: '?infer`, which is satisfiable for any lifetime, leading to
// unsoundness: trait-system-refactor-initiative#295.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hmm, I feel like what makes this issue clear to me would be the following.

If we pass in the uninstantiated assumption to predicates_for_object_candidate we'd instead get the builtin impl

impl<'s, 'hr, 'hr2> Trait<'hr> for dyn for<'x> Trait<'x> + 's
where
    dyn for<'a> Trait<'a> + 's: 'hr2,

The fact that 'hr2 is not actually constrained by the impl header is what's causing this problem. But yeah, the comment sgtm :>

@sjwang05

sjwang05 commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Added it as a test. Right now it just emits the dyn compatibility error, hopefully it'll be exercised soon-ish though.

(also I realize I never actually did this, so.)

r? lcnr

@rustbot rustbot assigned lcnr and unassigned wesleywiser Sep 2, 2026
@ShoyuVanilla

Copy link
Copy Markdown
Member

I think this is already approved 😄
@bors r=lcnr

@rust-bors

rust-bors Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 56e17cb has been approved by lcnr

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 Sep 10, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 10, 2026
re-equate the object bound's trait ref with the goal's when considering object bounds

rust-lang/trait-system-refactor-initiative#295 bisects to rust-lang#152859, which changed `probe_and_consider_object_bound_candidate` to pass the trait object's trait ref in a binder to `predicates_for_object_candidate`, which calls `instantiate_binder_with_infer` on the binder.

Re-instantiating the binder like this, however, loses the constraint that `'x: 'static`, even though earlier in `match_assumption` we actually did equate the object trait ref with the goal's trait ref, which gave us the `eq('x, 'static)` relationship we needed. However, the new inference var has no relationship with the equality we established; hence we lose `'x: 'static` and therefore `Self: 'static` and `'s: 'static`.

We therefore inline `match_assumption` and directly equate the assumption's trait ref with the goal's before passing the assumption to `predicates_for_object_candidate` instead and remove the re-instantiation.

fixes rust-lang/trait-system-refactor-initiative#295

cc @lcnr
rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
…uwer

Rollup of 3 pull requests

Successful merges:

 - #162581 (Subtree sync for rustc_codegen_cranelift)
 - #160831 (re-equate the object bound's trait ref with the goal's when considering object bounds)
 - #162437 (fix(traits): Remove expression references from index suggestions)
rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
…uwer

Rollup of 3 pull requests

Successful merges:

 - #162581 (Subtree sync for rustc_codegen_cranelift)
 - #160831 (re-equate the object bound's trait ref with the goal's when considering object bounds)
 - #162437 (fix(traits): Remove expression references from index suggestions)
rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
…uwer

Rollup of 3 pull requests

Successful merges:

 - #162581 (Subtree sync for rustc_codegen_cranelift)
 - #160831 (re-equate the object bound's trait ref with the goal's when considering object bounds)
 - #162437 (fix(traits): Remove expression references from index suggestions)
rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
…uwer

Rollup of 3 pull requests

Successful merges:

 - #162581 (Subtree sync for rustc_codegen_cranelift)
 - #160831 (re-equate the object bound's trait ref with the goal's when considering object bounds)
 - #162437 (fix(traits): Remove expression references from index suggestions)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 10, 2026
re-equate the object bound's trait ref with the goal's when considering object bounds

rust-lang/trait-system-refactor-initiative#295 bisects to rust-lang#152859, which changed `probe_and_consider_object_bound_candidate` to pass the trait object's trait ref in a binder to `predicates_for_object_candidate`, which calls `instantiate_binder_with_infer` on the binder.

Re-instantiating the binder like this, however, loses the constraint that `'x: 'static`, even though earlier in `match_assumption` we actually did equate the object trait ref with the goal's trait ref, which gave us the `eq('x, 'static)` relationship we needed. However, the new inference var has no relationship with the equality we established; hence we lose `'x: 'static` and therefore `Self: 'static` and `'s: 'static`.

We therefore inline `match_assumption` and directly equate the assumption's trait ref with the goal's before passing the assumption to `predicates_for_object_candidate` instead and remove the re-instantiation.

fixes rust-lang/trait-system-refactor-initiative#295

cc @lcnr
rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #162581 (Subtree sync for rustc_codegen_cranelift)
 - #160831 (re-equate the object bound's trait ref with the goal's when considering object bounds)
 - #162057 (Some cleanups around fatal errors and delayed bugs)
 - #162437 (fix(traits): Remove expression references from index suggestions)
 - #162534 (Fix run-make/prune-link-args to work with MinGW)
 - #162576 (doc: Fix external mention of signum() return value regarding NaNs)
rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #162581 (Subtree sync for rustc_codegen_cranelift)
 - #160831 (re-equate the object bound's trait ref with the goal's when considering object bounds)
 - #162057 (Some cleanups around fatal errors and delayed bugs)
 - #162437 (fix(traits): Remove expression references from index suggestions)
 - #162534 (Fix run-make/prune-link-args to work with MinGW)
 - #162576 (doc: Fix external mention of signum() return value regarding NaNs)
rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - #162581 (Subtree sync for rustc_codegen_cranelift)
 - #160831 (re-equate the object bound's trait ref with the goal's when considering object bounds)
 - #162057 (Some cleanups around fatal errors and delayed bugs)
 - #162437 (fix(traits): Remove expression references from index suggestions)
 - #162534 (Fix run-make/prune-link-args to work with MinGW)
 - #162554 (Attemped fix for home dir problem on `dist-aarch64-llvm-mingw`)
 - #162576 (doc: Fix external mention of signum() return value regarding NaNs)
@rust-bors
rust-bors Bot merged commit a5fd214 into rust-lang:main Sep 10, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 10, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
Rollup merge of #160831 - sjwang05:equate-again, r=lcnr

re-equate the object bound's trait ref with the goal's when considering object bounds

rust-lang/trait-system-refactor-initiative#295 bisects to #152859, which changed `probe_and_consider_object_bound_candidate` to pass the trait object's trait ref in a binder to `predicates_for_object_candidate`, which calls `instantiate_binder_with_infer` on the binder.

Re-instantiating the binder like this, however, loses the constraint that `'x: 'static`, even though earlier in `match_assumption` we actually did equate the object trait ref with the goal's trait ref, which gave us the `eq('x, 'static)` relationship we needed. However, the new inference var has no relationship with the equality we established; hence we lose `'x: 'static` and therefore `Self: 'static` and `'s: 'static`.

We therefore inline `match_assumption` and directly equate the assumption's trait ref with the goal's before passing the assumption to `predicates_for_object_candidate` instead and remove the re-instantiation.

fixes rust-lang/trait-system-refactor-initiative#295

cc @lcnr
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this pull request Sep 14, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#162581 (Subtree sync for rustc_codegen_cranelift)
 - rust-lang/rust#160831 (re-equate the object bound's trait ref with the goal's when considering object bounds)
 - rust-lang/rust#162057 (Some cleanups around fatal errors and delayed bugs)
 - rust-lang/rust#162437 (fix(traits): Remove expression references from index suggestions)
 - rust-lang/rust#162534 (Fix run-make/prune-link-args to work with MinGW)
 - rust-lang/rust#162554 (Attemped fix for home dir problem on `dist-aarch64-llvm-mingw`)
 - rust-lang/rust#162576 (doc: Fix external mention of signum() return value regarding NaNs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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. 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.

next-solver soundness bug: dyn for<'x> Trait<'x> + 's: Trait<'static> is accepted for any 's

5 participants