Skip to content

Silent demotion of lifetime in analysis causes confusing message #21508

@dhardy

Description

@dhardy

See StackOverflow question.

Summary: the compiler accepts a trait impl function returning a &'static str when the trait requires only &'a str, 'a being the life of the object, but then assumes the lifetime of the returned value is only &'a str despite direct call (not via trait).

Possible solutions I see:

  1. Do not demote the lifetime of a function when called via the object (not the trait), but keep both versions of the function (i.e. lifetime &'static when called via the object and lifetime &'a when called via the trait). This is likely confusing, so (2) may be better.
  2. Make it an error to implement a trait function with too long a lifetime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsP-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions