Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly deal with function pointer Fn candidates with escaping bound vars #104929

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions compiler/rustc_middle/src/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,88 @@ where

value.fold_with(&mut Shifter::new(tcx, amount))
}

/// Takes a nested binder and flattens it into one, by shifting the outer binder's
/// bound variables down out one de Bruijn index and reindexing them into a
/// concatenated the bound vars list.
pub fn flatten_binders<'tcx, T: TypeFoldable<'tcx>>(
tcx: TyCtxt<'tcx>,
bound: ty::Binder<'tcx, ty::Binder<'tcx, T>>,
) -> Binder<'tcx, T> {
assert!(!bound.has_escaping_bound_vars());

let outer_bound_vars = bound.bound_vars();
let inner_binder = bound.skip_binder();
let inner_bound_vars = inner_binder.bound_vars();

let combined_vars =
tcx.mk_bound_variable_kinds(inner_bound_vars.iter().chain(outer_bound_vars));
ty::Binder::bind_with_vars(
inner_binder.skip_binder().fold_with(&mut BinderFlattener {
tcx,
index: ty::INNERMOST,
bound_vars_to_shift: inner_bound_vars.len(),
}),
combined_vars,
)
}

struct BinderFlattener<'tcx> {
tcx: TyCtxt<'tcx>,
index: ty::DebruijnIndex,
bound_vars_to_shift: usize,
}

impl BinderFlattener<'_> {
fn shift_bv(&self, bv: ty::BoundVar) -> ty::BoundVar {
ty::BoundVar::from_usize(bv.as_usize() + self.bound_vars_to_shift)
}
}

impl<'tcx> TypeFolder<'tcx> for BinderFlattener<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn fold_binder<T: TypeFoldable<'tcx>>(
&mut self,
t: ty::Binder<'tcx, T>,
) -> ty::Binder<'tcx, T> {
self.index.shift_in(1);
let t = t.super_fold_with(self);
self.index.shift_out(1);
t
}

fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
match *t.kind() {
ty::Bound(debruijn, bt) if debruijn > self.index => self.tcx.mk_ty(ty::Bound(
self.index,
ty::BoundTy { var: self.shift_bv(bt.var), kind: bt.kind },
)),
_ if t.has_vars_bound_at_or_above(self.index) => t.super_fold_with(self),
_ => t,
}
}

fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
match *r {
ty::ReLateBound(debruijn, br) if debruijn > self.index => {
self.tcx.mk_region(ty::ReLateBound(
self.index,
ty::BoundRegion { var: self.shift_bv(br.var), kind: br.kind },
))
}
_ => r,
}
}

fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
match ct.kind() {
ty::ConstKind::Bound(debruijn, bv) if debruijn > self.index => {
self.tcx.mk_const(ty::ConstKind::Bound(self.index, self.shift_bv(bv)), ct.ty())
}
_ => ct.super_fold_with(self),
}
}
}
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,7 @@ fn confirm_callable_candidate<'cx, 'tcx>(
obligation.predicate.self_ty(),
fn_sig,
flag,
false,
)
.map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
projection_ty: ty::ProjectionTy {
Expand Down
19 changes: 13 additions & 6 deletions compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_infer::infer::InferOk;
use rustc_infer::infer::LateBoundRegionConversionTime::HigherRankedType;
use rustc_middle::ty::{
self, GenericArg, GenericArgKind, GenericParamDefKind, InternalSubsts, SubstsRef,
ToPolyTraitRef, ToPredicate, Ty, TyCtxt,
ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeVisitable,
};
use rustc_span::def_id::DefId;

Expand Down Expand Up @@ -598,20 +598,27 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
{
debug!(?obligation, "confirm_fn_pointer_candidate");

let self_ty = self
.infcx
.shallow_resolve(obligation.self_ty().no_bound_vars())
.expect("fn pointer should not capture bound vars from predicate");
// Skipping binder here (*)
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
let sig = self_ty.fn_sig(self.tcx());
let trait_ref = closure_trait_ref_and_return_type(
let mut trait_ref = closure_trait_ref_and_return_type(
self.tcx(),
obligation.predicate.def_id(),
self_ty,
sig,
util::TupleArgumentsFlag::Yes,
// Only function pointers (currently) can have bound vars that reference
// the predicate, since those are the only ones we can name in where clauses.
self_ty.is_fn_ptr(),
)
.map_bound(|(trait_ref, _)| trait_ref);

// (*) ... and the binder's escaping bound vars are dealt with here
if trait_ref.has_escaping_bound_vars() {
trait_ref =
ty::fold::flatten_binders(self.infcx.tcx, obligation.predicate.rebind(trait_ref));
}

let mut nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;

// Confirm the `type Output: Sized;` bound that is present on `FnOnce`
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
self_ty,
closure_sig,
util::TupleArgumentsFlag::No,
false,
)
.map_bound(|(trait_ref, _)| trait_ref)
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_trait_selection/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,15 @@ pub fn get_vtable_index_of_object_method<'tcx, N>(
pub fn closure_trait_ref_and_return_type<'tcx>(
tcx: TyCtxt<'tcx>,
fn_trait_def_id: DefId,
self_ty: Ty<'tcx>,
mut self_ty: Ty<'tcx>,
sig: ty::PolyFnSig<'tcx>,
tuple_arguments: TupleArgumentsFlag,
allow_escaping_bound_vars: bool,
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>)> {
assert!(!self_ty.has_escaping_bound_vars());
assert!(allow_escaping_bound_vars || !self_ty.has_escaping_bound_vars());
if self_ty.has_escaping_bound_vars() {
self_ty = ty::fold::shift_vars(tcx, self_ty, 1);
}
let arguments_tuple = match tuple_arguments {
TupleArgumentsFlag::No => sig.skip_binder().inputs()[0],
TupleArgumentsFlag::Yes => tcx.intern_tup(sig.skip_binder().inputs()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// check-pass

fn main() {
foo();
foo2();
}

fn foo()
where
for<'a> for<'b> fn(&'a (), &'b ()): Fn(&'a (), &'static ()),
{
}

fn foo2()
where
for<'a> fn(&'a ()): Fn(&'a ()),
{
}