Skip to content

Make trait aliases eligible generic containers wrt. ambient object lifetime defaults #140710

Open
@fmease

Description

@fmease

Arguably, trait alias refs should bring along ambient 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      

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 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)]`S-blockedStatus: Blocked on something else such as an RFC or other implementation work.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