Skip to content

Commit 85c4b4c

Browse files
committed
use the correct supertrait substitution in object_ty_for_trait
Fixes #57156.
1 parent 6861426 commit 85c4b4c

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/librustc/traits/object_safety.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
252252
method: &ty::AssociatedItem)
253253
-> Option<MethodViolationCode>
254254
{
255+
debug!("object_safety_violation_for_method({:?}, {:?})", trait_def_id, method);
255256
// Any method that has a `Self : Sized` requisite is otherwise
256257
// exempt from the regulations.
257258
if self.generics_require_sized_self(method.def_id) {
@@ -270,6 +271,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
270271
method: &ty::AssociatedItem)
271272
-> bool
272273
{
274+
debug!("is_vtable_safe_method({:?}, {:?})", trait_def_id, method);
273275
// Any method that has a `Self : Sized` requisite can't be called.
274276
if self.generics_require_sized_self(method.def_id) {
275277
return false;
@@ -389,6 +391,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
389391
fn receiver_for_self_ty(
390392
self, receiver_ty: Ty<'tcx>, self_ty: Ty<'tcx>, method_def_id: DefId
391393
) -> Ty<'tcx> {
394+
debug!("receiver_for_self_ty({:?}, {:?}, {:?})", receiver_ty, self_ty, method_def_id);
392395
let substs = Substs::for_item(self, method_def_id, |param, _| {
393396
if param.index == 0 {
394397
self_ty.into()
@@ -397,7 +400,10 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
397400
}
398401
});
399402

400-
receiver_ty.subst(self, substs)
403+
let result = receiver_ty.subst(self, substs);
404+
debug!("receiver_for_self_ty({:?}, {:?}, {:?}) = {:?}",
405+
receiver_ty, self_ty, method_def_id, result);
406+
result
401407
}
402408

403409
/// creates the object type for the current trait. For example,
@@ -413,18 +419,26 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
413419
);
414420

415421
let mut associated_types = traits::supertraits(self, ty::Binder::dummy(trait_ref))
416-
.flat_map(|trait_ref| self.associated_items(trait_ref.def_id()))
417-
.filter(|item| item.kind == ty::AssociatedKind::Type)
422+
.flat_map(|super_trait_ref| {
423+
self.associated_items(super_trait_ref.def_id())
424+
.map(move |item| (super_trait_ref, item))
425+
})
426+
.filter(|(_, item)| item.kind == ty::AssociatedKind::Type)
418427
.collect::<Vec<_>>();
419428

420429
// existential predicates need to be in a specific order
421-
associated_types.sort_by_cached_key(|item| self.def_path_hash(item.def_id));
422-
423-
let projection_predicates = associated_types.into_iter().map(|item| {
430+
associated_types.sort_by_cached_key(|(_, item)| self.def_path_hash(item.def_id));
431+
432+
let projection_predicates = associated_types.into_iter().map(|(super_trait_ref, item)| {
433+
// We *can* get bound lifetimes here in cases like
434+
// `trait MyTrait: for<'s> OtherTrait<&'s T, Output=bool>`.
435+
//
436+
// binder moved to (*)...
437+
let super_trait_ref = super_trait_ref.skip_binder();
424438
ty::ExistentialPredicate::Projection(ty::ExistentialProjection {
425-
ty: self.mk_projection(item.def_id, trait_ref.substs),
439+
ty: self.mk_projection(item.def_id, super_trait_ref.substs),
426440
item_def_id: item.def_id,
427-
substs: trait_ref.substs,
441+
substs: super_trait_ref.substs,
428442
})
429443
});
430444

@@ -433,7 +447,8 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
433447
);
434448

435449
let object_ty = self.mk_dynamic(
436-
ty::Binder::dummy(existential_predicates),
450+
// (*) ... binder re-introduced here
451+
ty::Binder::bind(existential_predicates),
437452
lifetime,
438453
);
439454

src/test/ui/issues/issue-57156.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// compile-pass
2+
3+
trait Foo<Args> {
4+
type Output;
5+
}
6+
7+
trait Bar<'a, T>: for<'s> Foo<&'s T, Output=bool> {
8+
fn cb(&self) -> Box<dyn Bar<'a, T, Output=bool>>;
9+
}
10+
11+
impl<'s> Foo<&'s ()> for () {
12+
type Output = bool;
13+
}
14+
15+
impl<'a> Bar<'a, ()> for () {
16+
fn cb(&self) -> Box<dyn Bar<'a, (), Output=bool>> {
17+
Box::new(*self)
18+
}
19+
}
20+
21+
fn main() {
22+
let _t = ().cb();
23+
}

0 commit comments

Comments
 (0)