Skip to content

Commit 114f1f6

Browse files
committed
Auto merge of #117610 - compiler-errors:object-hmm, r=aliemjay
Only instantiate binder during dyn's built-in trait candidate probe once See UI test for demonstration of the issue. This was "caused" by #117131, but only because we're using the `normalize_param_env` (which has been augmented with a projection clause used to normalize GATs) which features non-lifetime bound vars in it. Fixes #117602 technically, though that's also fixed by #117542. r? types
2 parents 504f63e + 171d558 commit 114f1f6

File tree

5 files changed

+64
-14
lines changed

5 files changed

+64
-14
lines changed

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
628628
}
629629

630630
self.infcx.probe(|_snapshot| {
631-
if obligation.has_non_region_late_bound() {
632-
return;
633-
}
631+
let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate);
632+
let placeholder_trait_predicate =
633+
self.infcx.instantiate_binder_with_placeholders(poly_trait_predicate);
634634

635-
// The code below doesn't care about regions, and the
636-
// self-ty here doesn't escape this probe, so just erase
637-
// any LBR.
638-
let self_ty = self.tcx().erase_late_bound_regions(obligation.self_ty());
639-
let poly_trait_ref = match self_ty.kind() {
635+
let self_ty = placeholder_trait_predicate.self_ty();
636+
let principal_trait_ref = match self_ty.kind() {
640637
ty::Dynamic(ref data, ..) => {
641638
if data.auto_traits().any(|did| did == obligation.predicate.def_id()) {
642639
debug!(
@@ -668,18 +665,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
668665
_ => return,
669666
};
670667

671-
debug!(?poly_trait_ref, "assemble_candidates_from_object_ty");
672-
673-
let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate);
674-
let placeholder_trait_predicate =
675-
self.infcx.instantiate_binder_with_placeholders(poly_trait_predicate);
668+
debug!(?principal_trait_ref, "assemble_candidates_from_object_ty");
676669

677670
// Count only those upcast versions that match the trait-ref
678671
// we are looking for. Specifically, do not only check for the
679672
// correct trait, but also the correct type parameters.
680673
// For example, we may be trying to upcast `Foo` to `Bar<i32>`,
681674
// but `Foo` is declared as `trait Foo: Bar<u32>`.
682-
let candidate_supertraits = util::supertraits(self.tcx(), poly_trait_ref)
675+
let candidate_supertraits = util::supertraits(self.tcx(), principal_trait_ref)
683676
.enumerate()
684677
.filter(|&(_, upcast_trait_ref)| {
685678
self.infcx.probe(|_| {
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/non-lifetime-via-dyn-builtin.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/non-lifetime-via-dyn-builtin.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,16 @@
1+
// revisions: current next
2+
//[next] compile-flags: -Ztrait-solver=next
3+
// check-pass
4+
5+
#![feature(non_lifetime_binders)]
6+
//~^ WARN the feature `non_lifetime_binders` is incomplete and may not be safe
7+
8+
fn trivial<A>()
9+
where
10+
for<B> dyn Fn(A, *const B): Fn(A, *const B),
11+
{
12+
}
13+
14+
fn main() {
15+
trivial::<u8>();
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// check-pass
2+
3+
trait Foo {
4+
type Bar<T>
5+
where
6+
dyn Send + 'static: Send;
7+
}
8+
9+
impl Foo for () {
10+
type Bar<T> = i32;
11+
// We take `<() as Foo>::Bar<T>: Sized` and normalize it under the where clause
12+
// of `for<S> <() as Foo>::Bar<S> = i32`. This gives us back `i32: Send` with
13+
// the nested obligation `(dyn Send + 'static): Send`. However, during candidate
14+
// assembly for object types, we disqualify any obligations that has non-region
15+
// late-bound vars in the param env(!), rather than just the predicate. This causes
16+
// the where clause to not hold even though it trivially should.
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)