Skip to content

Commit 533cfde

Browse files
committed
Auto merge of rust-lang#119947 - compiler-errors:old-solver-instantiate-response, r=lcnr
Make sure to instantiate placeholders correctly in old solver When creating the query substitution guess for an input placeholder type like `!1_T` (in universe 1), we were guessing the response substitution with something like `!0_T`. This failed to unify with `!1_T`, causing an ICE. This PR reworks the query substitution guess code to work a bit more like the new solver. I'm *pretty* sure this is correct, though I'd really appreciate some scrutiny from someone (*cough* lcnr) who knows a bit more about query instantiation :) Fixes rust-lang#119941 r? lcnr
2 parents fa0dc20 + 985e1e0 commit 533cfde

7 files changed

+106
-4
lines changed

compiler/rustc_infer/src/infer/canonical/query_response.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -483,17 +483,25 @@ impl<'tcx> InferCtxt<'tcx> {
483483
let result_subst = CanonicalVarValues {
484484
var_values: self.tcx.mk_args_from_iter(
485485
query_response.variables.iter().enumerate().map(|(index, info)| {
486-
if info.is_existential() {
486+
if info.universe() != ty::UniverseIndex::ROOT {
487+
// A variable from inside a binder of the query. While ideally these shouldn't
488+
// exist at all, we have to deal with them for now.
489+
self.instantiate_canonical_var(cause.span, info, |u| {
490+
universe_map[u.as_usize()]
491+
})
492+
} else if info.is_existential() {
487493
match opt_values[BoundVar::new(index)] {
488494
Some(k) => k,
489495
None => self.instantiate_canonical_var(cause.span, info, |u| {
490496
universe_map[u.as_usize()]
491497
}),
492498
}
493499
} else {
494-
self.instantiate_canonical_var(cause.span, info, |u| {
495-
universe_map[u.as_usize()]
496-
})
500+
// For placeholders which were already part of the input, we simply map this
501+
// universal bound variable back the placeholder of the input.
502+
opt_values[BoundVar::new(index)].expect(
503+
"expected placeholder to be unified with itself during response",
504+
)
497505
}
498506
}),
499507
),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/unifying-placeholders-in-query-response-2.rs:5:12
3+
|
4+
LL | #![feature(non_lifetime_binders)]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/unifying-placeholders-in-query-response-2.rs:5:12
3+
|
4+
LL | #![feature(non_lifetime_binders)]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// revisions: current next
2+
//[next] compile-flags: -Znext-solver
3+
// check-pass
4+
5+
#![feature(non_lifetime_binders)]
6+
//~^ WARN the feature `non_lifetime_binders` is incomplete
7+
8+
trait Id {
9+
type Output: ?Sized;
10+
}
11+
12+
impl<T: ?Sized> Id for T {
13+
type Output = T;
14+
}
15+
16+
trait Everyone {}
17+
impl<T: ?Sized> Everyone for T {}
18+
19+
fn hello() where for<T> <T as Id>::Output: Everyone {}
20+
21+
fn main() {
22+
hello();
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/unifying-placeholders-in-query-response.rs:5:12
3+
|
4+
LL | #![feature(non_lifetime_binders)]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/unifying-placeholders-in-query-response.rs:5:12
3+
|
4+
LL | #![feature(non_lifetime_binders)]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// revisions: current next
2+
//[next] compile-flags: -Znext-solver
3+
// check-pass
4+
5+
#![feature(non_lifetime_binders)]
6+
//~^ WARN the feature `non_lifetime_binders` is incomplete
7+
8+
pub trait Foo<T: ?Sized> {
9+
type Bar<K: ?Sized>: ?Sized;
10+
}
11+
12+
impl Foo<usize> for () {
13+
type Bar<K: ?Sized> = K;
14+
}
15+
16+
pub fn f<T1, T2>(a: T1, b: T2)
17+
where
18+
T1: for<T> Foo<usize, Bar<T> = T>,
19+
T2: for<T> Foo<usize, Bar<T> = <T1 as Foo<usize>>::Bar<T>>,
20+
{
21+
}
22+
23+
fn it_works() {
24+
f((), ());
25+
}
26+
27+
fn main() {}

0 commit comments

Comments
 (0)