From 171d5587ca1677ced9d1e70992a02091794cfb55 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 6 Nov 2023 23:41:25 +0000 Subject: [PATCH] Don't instantiate the binder twice when assembling object candidate --- .../src/traits/select/candidate_assembly.rs | 24 ++++++------------- ...on-lifetime-via-dyn-builtin.current.stderr | 11 +++++++++ .../non-lifetime-via-dyn-builtin.next.stderr | 11 +++++++++ .../ui/traits/non-lifetime-via-dyn-builtin.rs | 16 +++++++++++++ 4 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 tests/ui/traits/non-lifetime-via-dyn-builtin.current.stderr create mode 100644 tests/ui/traits/non-lifetime-via-dyn-builtin.next.stderr create mode 100644 tests/ui/traits/non-lifetime-via-dyn-builtin.rs diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index ba551127ce1b1..f4b6d3bcfda29 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -628,18 +628,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } self.infcx.probe(|_snapshot| { - // FIXME(non_lifetime_binders): Really, we care only to check that we don't - // have any non-region *escaping* bound vars, but that's hard to check and - // we shouldn't really ever encounter those anyways. - if obligation.self_ty().has_non_region_late_bound() { - return; - } + 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); - // The code below doesn't care about regions, and the - // self-ty here doesn't escape this probe, so just erase - // any LBR. - let self_ty = self.tcx().erase_late_bound_regions(obligation.self_ty()); - let poly_trait_ref = match self_ty.kind() { + let self_ty = placeholder_trait_predicate.self_ty(); + let principal_trait_ref = match self_ty.kind() { ty::Dynamic(ref data, ..) => { if data.auto_traits().any(|did| did == obligation.predicate.def_id()) { debug!( @@ -671,18 +665,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { _ => return, }; - debug!(?poly_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); + debug!(?principal_trait_ref, "assemble_candidates_from_object_ty"); // Count only those upcast versions that match the trait-ref // we are looking for. Specifically, do not only check for the // correct trait, but also the correct type parameters. // For example, we may be trying to upcast `Foo` to `Bar`, // but `Foo` is declared as `trait Foo: Bar`. - let candidate_supertraits = util::supertraits(self.tcx(), poly_trait_ref) + let candidate_supertraits = util::supertraits(self.tcx(), principal_trait_ref) .enumerate() .filter(|&(_, upcast_trait_ref)| { self.infcx.probe(|_| { diff --git a/tests/ui/traits/non-lifetime-via-dyn-builtin.current.stderr b/tests/ui/traits/non-lifetime-via-dyn-builtin.current.stderr new file mode 100644 index 0000000000000..9f373cc50f0ac --- /dev/null +++ b/tests/ui/traits/non-lifetime-via-dyn-builtin.current.stderr @@ -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 for more information + = note: `#[warn(incomplete_features)]` on by default + +warning: 1 warning emitted + diff --git a/tests/ui/traits/non-lifetime-via-dyn-builtin.next.stderr b/tests/ui/traits/non-lifetime-via-dyn-builtin.next.stderr new file mode 100644 index 0000000000000..9f373cc50f0ac --- /dev/null +++ b/tests/ui/traits/non-lifetime-via-dyn-builtin.next.stderr @@ -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 for more information + = note: `#[warn(incomplete_features)]` on by default + +warning: 1 warning emitted + diff --git a/tests/ui/traits/non-lifetime-via-dyn-builtin.rs b/tests/ui/traits/non-lifetime-via-dyn-builtin.rs new file mode 100644 index 0000000000000..9a8a5ced2e250 --- /dev/null +++ b/tests/ui/traits/non-lifetime-via-dyn-builtin.rs @@ -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() +where + for dyn Fn(A, *const B): Fn(A, *const B), +{ +} + +fn main() { + trivial::(); +}