Skip to content

Trait alias refs don't induce trait object lifetime defaults #140710

@fmease

Description

@fmease

Arguably, trait alias refs should bring along trait object lifetime defaults (I would say "just like trait refs" but those are currently unintentionally broken (or have been since their introduction) which would get fixed in #129543).

TL;DR: This code should compile and doesn't currently:

#![feature(trait_alias)]

trait Container<'a, T: 'a + ?Sized> =;
trait Bound {}

fn f<'r, T>() where T: Container<'r, dyn Bound + 'r> { g::<'r, T>() }
fn g<'r, T>() where T: Container<'r, dyn Bound> {}
//                                   ^^^^^^^^^ we currently deduce `dyn Bound + 'static` since trait alias
//                                             `Container` is not considered an eligible generic container.
//                                             However, we should make it one and deduce `dyn Bound + 'r`.
error: lifetime may not live long enough
 --> src/lib.rs:7:56
  |
7 | fn f<'r, T>() where T: Container<'r, dyn Bound + 'r> { g::<'r, T>() }
  |      -- lifetime `'r` defined here      

Trait object lifetime defaults aren't a hot topic esp. due to the existence of inferred outlives-bounds which don't participate in their resolution, so this is generally P-low but I'm not marking this issue as such as it should still block the stabilization of trait_alias (or whatever features end up replacing it) since modifying the resolution of trait object lifetime default afterwards would be a breaking change.

Temporarily blocking this on my PR #129543 which touches a lot of code in that area.

Metadata

Metadata

Assignees

Labels

A-dyn-traitArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.F-trait_alias`#![feature(trait_alias)]`T-typesRelevant to the types team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions