Don't instantiate the binder twice when assembling object candidate

This commit is contained in:
Michael Goulet 2023-11-06 23:41:25 +00:00
parent 24e14dd8b4
commit 171d5587ca
4 changed files with 45 additions and 17 deletions

View File

@ -628,18 +628,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
} }
self.infcx.probe(|_snapshot| { self.infcx.probe(|_snapshot| {
// FIXME(non_lifetime_binders): Really, we care only to check that we don't let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate);
// have any non-region *escaping* bound vars, but that's hard to check and let placeholder_trait_predicate =
// we shouldn't really ever encounter those anyways. self.infcx.instantiate_binder_with_placeholders(poly_trait_predicate);
if obligation.self_ty().has_non_region_late_bound() {
return;
}
// The code below doesn't care about regions, and the let self_ty = placeholder_trait_predicate.self_ty();
// self-ty here doesn't escape this probe, so just erase let principal_trait_ref = match self_ty.kind() {
// any LBR.
let self_ty = self.tcx().erase_late_bound_regions(obligation.self_ty());
let poly_trait_ref = match self_ty.kind() {
ty::Dynamic(ref data, ..) => { ty::Dynamic(ref data, ..) => {
if data.auto_traits().any(|did| did == obligation.predicate.def_id()) { if data.auto_traits().any(|did| did == obligation.predicate.def_id()) {
debug!( debug!(
@ -671,18 +665,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
_ => return, _ => return,
}; };
debug!(?poly_trait_ref, "assemble_candidates_from_object_ty"); debug!(?principal_trait_ref, "assemble_candidates_from_object_ty");
let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate);
let placeholder_trait_predicate =
self.infcx.instantiate_binder_with_placeholders(poly_trait_predicate);
// Count only those upcast versions that match the trait-ref // Count only those upcast versions that match the trait-ref
// we are looking for. Specifically, do not only check for the // we are looking for. Specifically, do not only check for the
// correct trait, but also the correct type parameters. // correct trait, but also the correct type parameters.
// For example, we may be trying to upcast `Foo` to `Bar<i32>`, // For example, we may be trying to upcast `Foo` to `Bar<i32>`,
// but `Foo` is declared as `trait Foo: Bar<u32>`. // but `Foo` is declared as `trait Foo: Bar<u32>`.
let candidate_supertraits = util::supertraits(self.tcx(), poly_trait_ref) let candidate_supertraits = util::supertraits(self.tcx(), principal_trait_ref)
.enumerate() .enumerate()
.filter(|&(_, upcast_trait_ref)| { .filter(|&(_, upcast_trait_ref)| {
self.infcx.probe(|_| { self.infcx.probe(|_| {

View File

@ -0,0 +1,11 @@
warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/non-lifetime-via-dyn-builtin.rs:5:12
|
LL | #![feature(non_lifetime_binders)]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
= note: `#[warn(incomplete_features)]` on by default
warning: 1 warning emitted

View File

@ -0,0 +1,11 @@
warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/non-lifetime-via-dyn-builtin.rs:5:12
|
LL | #![feature(non_lifetime_binders)]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
= note: `#[warn(incomplete_features)]` on by default
warning: 1 warning emitted

View File

@ -0,0 +1,16 @@
// revisions: current next
//[next] compile-flags: -Ztrait-solver=next
// check-pass
#![feature(non_lifetime_binders)]
//~^ WARN the feature `non_lifetime_binders` is incomplete and may not be safe
fn trivial<A>()
where
for<B> dyn Fn(A, *const B): Fn(A, *const B),
{
}
fn main() {
trivial::<u8>();
}