Skip to content

Commit d39d91f

Browse files
committed
Make enum variant paths induce trait object lifetime defaults in the enum path segment
1 parent ed60e2a commit d39d91f

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
16911691
// which requires object lifetime defaults.
16921692
let type_def_id = match res {
16931693
Res::Def(DefKind::AssocTy, def_id) if depth == 1 => Some(self.tcx.parent(def_id)),
1694-
Res::Def(DefKind::Variant, def_id) if depth == 0 => Some(self.tcx.parent(def_id)),
1694+
Res::Def(DefKind::Variant, def_id) if depth == 0 || depth == 1 => {
1695+
Some(self.tcx.parent(def_id))
1696+
}
16951697
Res::Def(
16961698
DefKind::Struct
16971699
| DefKind::Union
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ check-pass
2+
3+
enum Enum<'a, T: 'a + AbideBy<'a> + ?Sized> {
4+
Variant,
5+
6+
Carry(&'a T),
7+
}
8+
9+
fn scope<'any>() {
10+
// We elaborate `dyn Trait` to `dyn Trait + 'any` due to bound `'a` on `T`.
11+
let _ = Enum::Variant::<'any, dyn Trait> {};
12+
13+
// Similarly here.
14+
let _ = Enum::<'any, dyn Trait>::Variant {};
15+
}
16+
17+
trait Trait {}
18+
19+
// We use this to test that a given trait object lifetime bound is
20+
// *exactly equal* to a given lifetime (not longer, not shorter).
21+
trait AbideBy<'a> {}
22+
impl<'a> AbideBy<'a> for dyn Trait + 'a {}
23+
24+
fn main() {}

0 commit comments

Comments
 (0)