Repository navigation
re-equate the object bound's trait ref with the goal's when considering object bounds - #160831
Conversation
|
r? @wesleywiser rustbot has assigned @wesleywiser. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
I was torn between this and modifying the |
There was a problem hiding this comment.
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
| unreachable!("expected trait or projection predicate as an assumption") | ||
| } | ||
| }); | ||
| let trait_ref = ecx.instantiate_binder_with_infer(trait_ref); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
I'm not sure what to do about the GAT case yet, since the
'hrdoes appear in the assumption's binder, but the'staticwe want to equate it to doesn't appear in the goal itself. There is aProjectionPredicatesitting 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?
| unreachable!("expected trait or projection predicate as an assumption") | ||
| } | ||
| }); | ||
| let trait_ref = ecx.instantiate_binder_with_infer(trait_ref); |
There was a problem hiding this comment.
I'm not sure what to do about the GAT case yet, since the
'hrdoes appear in the assumption's binder, but the'staticwe want to equate it to doesn't appear in the goal itself. There is aProjectionPredicatesitting 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?
| // 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. |
There was a problem hiding this comment.
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 :>
…dering object bounds
3388e0f to
56e17cb
Compare
|
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 |
|
I think this is already approved 😄 |
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
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
…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)
…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)
…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)
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
…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)
rust-lang/trait-system-refactor-initiative#295 bisects to #152859, which changed
probe_and_consider_object_bound_candidateto pass the trait object's trait ref in a binder topredicates_for_object_candidate, which callsinstantiate_binder_with_inferon the binder.Re-instantiating the binder like this, however, loses the constraint that
'x: 'static, even though earlier inmatch_assumptionwe actually did equate the object trait ref with the goal's trait ref, which gave us theeq('x, 'static)relationship we needed. However, the new inference var has no relationship with the equality we established; hence we lose'x: 'staticand thereforeSelf: 'staticand's: 'static.We therefore inline
match_assumptionand directly equate the assumption's trait ref with the goal's before passing the assumption topredicates_for_object_candidateinstead and remove the re-instantiation.fixes rust-lang/trait-system-refactor-initiative#295
cc @lcnr