Skip to content

Commit e3f88cb

Browse files
arielb1pietroalbini
authored andcommitted
use the correct supertrait substitution in object_ty_for_trait
Fixes #57156.
1 parent 2bde39b commit e3f88cb

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/librustc/traits/object_safety.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
262262
method: &ty::AssociatedItem)
263263
-> Option<MethodViolationCode>
264264
{
265+
debug!("object_safety_violation_for_method({:?}, {:?})", trait_def_id, method);
265266
// Any method that has a `Self : Sized` requisite is otherwise
266267
// exempt from the regulations.
267268
if self.generics_require_sized_self(method.def_id) {
@@ -280,6 +281,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
280281
method: &ty::AssociatedItem)
281282
-> bool
282283
{
284+
debug!("is_vtable_safe_method({:?}, {:?})", trait_def_id, method);
283285
// Any method that has a `Self : Sized` requisite can't be called.
284286
if self.generics_require_sized_self(method.def_id) {
285287
return false;
@@ -399,6 +401,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
399401
fn receiver_for_self_ty(
400402
self, receiver_ty: Ty<'tcx>, self_ty: Ty<'tcx>, method_def_id: DefId
401403
) -> Ty<'tcx> {
404+
debug!("receiver_for_self_ty({:?}, {:?}, {:?})", receiver_ty, self_ty, method_def_id);
402405
let substs = Substs::for_item(self, method_def_id, |param, _| {
403406
if param.index == 0 {
404407
self_ty.into()
@@ -407,7 +410,10 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
407410
}
408411
});
409412

410-
receiver_ty.subst(self, substs)
413+
let result = receiver_ty.subst(self, substs);
414+
debug!("receiver_for_self_ty({:?}, {:?}, {:?}) = {:?}",
415+
receiver_ty, self_ty, method_def_id, result);
416+
result
411417
}
412418

413419
/// creates the object type for the current trait. For example,
@@ -423,18 +429,26 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
423429
);
424430

425431
let mut associated_types = traits::supertraits(self, ty::Binder::dummy(trait_ref))
426-
.flat_map(|trait_ref| self.associated_items(trait_ref.def_id()))
427-
.filter(|item| item.kind == ty::AssociatedKind::Type)
432+
.flat_map(|super_trait_ref| {
433+
self.associated_items(super_trait_ref.def_id())
434+
.map(move |item| (super_trait_ref, item))
435+
})
436+
.filter(|(_, item)| item.kind == ty::AssociatedKind::Type)
428437
.collect::<Vec<_>>();
429438

430439
// existential predicates need to be in a specific order
431-
associated_types.sort_by_cached_key(|item| self.def_path_hash(item.def_id));
432-
433-
let projection_predicates = associated_types.into_iter().map(|item| {
440+
associated_types.sort_by_cached_key(|(_, item)| self.def_path_hash(item.def_id));
441+
442+
let projection_predicates = associated_types.into_iter().map(|(super_trait_ref, item)| {
443+
// We *can* get bound lifetimes here in cases like
444+
// `trait MyTrait: for<'s> OtherTrait<&'s T, Output=bool>`.
445+
//
446+
// binder moved to (*)...
447+
let super_trait_ref = super_trait_ref.skip_binder();
434448
ty::ExistentialPredicate::Projection(ty::ExistentialProjection {
435-
ty: self.mk_projection(item.def_id, trait_ref.substs),
449+
ty: self.mk_projection(item.def_id, super_trait_ref.substs),
436450
item_def_id: item.def_id,
437-
substs: trait_ref.substs,
451+
substs: super_trait_ref.substs,
438452
})
439453
});
440454

@@ -443,7 +457,8 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
443457
);
444458

445459
let object_ty = self.mk_dynamic(
446-
ty::Binder::dummy(existential_predicates),
460+
// (*) ... binder re-introduced here
461+
ty::Binder::bind(existential_predicates),
447462
lifetime,
448463
);
449464

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

Lines changed: 23 additions & 0 deletions
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)