Skip to content

Commit 6575988

Browse files
committed
handle trait objects formed from traits with Self::Foo: 'a clauses
1 parent 0d744ec commit 6575988

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/librustc_typeck/outlives/implicit_infer.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc::hir::def_id::DefId;
1414
use rustc::hir::itemlikevisit::ItemLikeVisitor;
1515
use rustc::ty::subst::{Kind, Subst, UnpackedKind};
1616
use rustc::ty::{self, Ty, TyCtxt};
17+
use rustc::ty::fold::TypeFoldable;
1718
use rustc::util::nodemap::FxHashMap;
1819

1920
use super::explicit::ExplicitPredicatesMap;
@@ -311,13 +312,23 @@ pub fn check_explicit_predicates<'tcx>(
311312
//
312313
// Note that we do this check for self **before** applying `substs`. In the
313314
// case that `substs` come from a `dyn Trait` type, our caller will have
314-
// included `Self = dyn Trait<'x, X>` as the value for `Self`. If we were
315+
// included `Self = usize` as the value for `Self`. If we were
315316
// to apply the substs, and not filter this predicate, we might then falsely
316317
// conclude that e.g. `X: 'x` was a reasonable inferred requirement.
317-
if let UnpackedKind::Type(ty) = outlives_predicate.0.unpack() {
318-
if ty.is_self() && ignore_self_ty.0 {
319-
debug!("skipping self ty = {:?}", &ty);
320-
continue;
318+
//
319+
// Another similar case is where we have a inferred
320+
// requirement like `<Self as Trait>::Foo: 'b`. We presently
321+
// ignore such requirements as well (cc #54467)-- though
322+
// conceivably it might be better if we could extract the `Foo
323+
// = X` binding from the object type (there must be such a
324+
// binding) and thus infer an outlives requirement that `X:
325+
// 'b`.
326+
if ignore_self_ty.0 {
327+
if let UnpackedKind::Type(ty) = outlives_predicate.0.unpack() {
328+
if ty.has_self_ty() {
329+
debug!("skipping self ty = {:?}", &ty);
330+
continue;
331+
}
321332
}
322333
}
323334

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Regression test for #54467:
2+
//
3+
// Here, the trait object has an "inferred outlives" requirement that
4+
// `<Self as MyIterator<'a>>::Item: 'a`; but since we don't know what
5+
// `Self` is, we were (incorrectly) messing things up, leading to
6+
// strange errors. This test ensures that we do not give compilation
7+
// errors.
8+
//
9+
// compile-pass
10+
11+
trait MyIterator<'a>: Iterator where Self::Item: 'a { }
12+
13+
struct MyStruct<'a, A> {
14+
item: Box<dyn MyIterator<'a, Item = A>>
15+
}
16+
17+
fn main() { }

0 commit comments

Comments
 (0)