Skip to content

object lifetime bound defaults in trait bindings are incompletely implemented #63618

Open
@nikomatsakis

Description

@nikomatsakis

In #63376, we fixed the behavior of default object lifetime bounds in "binding arguments", as described in this gist. For example the following type;

dyn Foo<Bar = dyn Baz>

will now default dyn Baz to dyn Baz + 'static.

However, the rules were incompletely implemented. If the trait has a lifetime parameter, we currently require an explicit lifetime bound. The expected behavior is to use the bounds declared on the associated type to set the default. So, given Foo like so:

trait Foo<'a> {
  type Bar: 'a + ?Sized;
}

then dyn Foo<'x, Bar = dyn Baz> would default to dyn Baz + 'a. Presently, however, it gives an error.

NB. There is one "design decision" to be made; does a where clause like where Self::Bar: 'x adjust the default? And how does that work when extended to GATs? My personal opinion is that we should limit ourselves to bounds declared on the associated type itself (and hence where Self::Bar: 'x would not count towards the default object lifetime bound), but this merits a bit of further discussion. This is largely because the complexity to specify and implement the behavior around where clauses and with GATs is kind feels out of proportion with the benefit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-dyn-traitArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemA-type-systemArea: Type systemT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamT-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