Fix marker trait winnowing depending on impl order#153847
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
411054e to
85b6665
Compare
85b6665 to
516f56b
Compare
|
Good catch. We actually need to check for both. If we have |
That makes sense to me. On the other hand, I commented out the |
We don't even try to prove things if the self type is an inference variable, so you need sth like #![feature(marker_trait_attr)]
#[marker]
trait Marker<T> {}
impl<T: 'static> Marker<T> for u32 {}
impl<T> Marker<T> for u32 {}
fn foo<T: Marker<U>, U>() -> U { todo!() }
fn bar<'a>() {
let x = foo::<u32, _>();
1i32.abs(); // guarantees we do trait solving before constraining `U`
let _: *mut &'a () = x;
}would need to look at the logging in |
|
Just checking in from compiler team triage, I think this is waiting on a review from @lcnr. Is that right? |
|
I don't necessarily think so, either me or @traviscross needs to look into my example #153847 (comment) and figure out whether that's works as a test |
516f56b to
7259e03
Compare
This comment was marked as resolved.
This comment was marked as resolved.
The `TypeOutlives` handler in `evaluate_predicate_recursively` means to check whether a type in a `T: 'a` predicate has free regions, bound regions, non-region inference variables, or non-region generic parameters -- and if so, to return `EvaluatedToOkModuloRegions` rather than `EvaluatedToOk`. Correspondingly, the comment says "no free lifetimes or generic parameters". But the code was mistakenly checking `has_non_region_infer()` twice instead of checking both `has_non_region_infer()` and `has_non_region_param()`. This meant that `TypeOutlives(T, 'static)` where `T` is a type parameter returned `EvaluatedToOk` -- claiming the result holds unconditionally -- when the correct answer is `EvaluatedToOkModuloRegions`. The distinction matters during marker trait winnowing in `prefer_lhs_over_victim`, which uses `must_apply_considering_regions()` (true only for `EvaluatedToOk`) to decide whether one impl beats another when there are multiple candidates. With the bug, a `T: 'static`-bounded impl appeared equally as strong as an unrestricted impl, making the winner depend on source order. This caused spurious E0310 errors when the more-constrained impl happened to appear after the less-constrained one. This fixes Rust issue 109481. See PR 153847. This same symptom was originally filed as issue 84917 and fixed in PR 88139. Then PR 102472 rewrote the `TypeOutlives` handler, introducing the duplicate `has_non_region_infer()` and losing the param check, regressing this. Around this same time, issue 109481 was filed. It noted the connection to PR 102472 -- that the bug only appeared after it -- but the duplicate condition was not noticed.
It's harder than one would imagine to demonstrate that the `has_non_region_infer()` check in the `TypeOutlives` handler is load bearing (even though the check seems right analytically). Fortunately, we did find a way to show this. Let's add that test. In working that out, we found two other interesting ways of showing that the `has_non_region_param()` check matters. Let's add those too. Though we're concerned here with code in the old solver, we test against both.
7259e03 to
9a122c9
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
Thanks. I looked into that example. It didn't work as a test of the @rustbot review |
Story: I was looking through this code for an unrelated reason and happened to notice the duplicate condition. That seemed obviously wrong, especially in light of the comment, so I worked backward to what it affected, wrote a test that failed, and then found that the test matched an existing
known-bugtest, which pointed me to #109481.The
TypeOutliveshandler inevaluate_predicate_recursivelymeans to check whether a type in aT: 'apredicate has free regions, bound regions, non-region inference variables, or non-region generic parameters -- and if so, to returnEvaluatedToOkModuloRegionsrather thanEvaluatedToOk. Correspondingly, the comment says "no free lifetimes or generic parameters". But the code was mistakenly checkinghas_non_region_infer()twice instead of checking bothhas_non_region_infer()andhas_non_region_param().This meant that
TypeOutlives(T, 'static)whereTis a type parameter returnedEvaluatedToOk-- claiming the result holds unconditionally -- when the correct answer isEvaluatedToOkModuloRegions.The distinction matters during marker trait winnowing in
prefer_lhs_over_victim, which usesmust_apply_considering_regions()(true only forEvaluatedToOk) to decide whether one impl beats another when there are multiple candidates. With the bug, aT: 'static-bounded impl appeared equally as strong as an unrestricted impl, making the winner depend on source order. This caused spurious E0310 errors when the more-constrained impl happened to appear after the less-constrained one.Fixes #109481.
This same symptom was originally filed as #84917 and fixed in PR #88139. Then PR #102472 rewrote the
TypeOutliveshandler, introducing the duplicatehas_non_region_infer()and losing the param check, regressing this. Around this same time, #109481 was filed. It noted the connection to #102472 -- the bug only appeared after it -- but the duplicate condition was not noticed.r? @lcnr
cc @oli-obk @nikomatsakis