Skip to content

Unclear introduction of 'static with APIT #88682

Open
@CAD97

Description

@CAD97

Given the code:

struct Event;
struct Span;

impl Event {
    fn span(&self) -> &Span {
        &Span
    }
}

impl Span {
    fn fields(&self) -> impl '_ + Iterator<Item = (&'static str, &'_ str)> {
        None.into_iter()
    }
}

fn show_event<'a>(event: &'a Event) {
    show_fields(event.span().fields());
}

fn show_fields<'a, 'data>(fields: impl 'a + Iterator<Item = (&'data str, &'data str)>) {
    for (_name, _value) in fields {
        todo!();
    }
}

The current output is:

error[E0759]: `event` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
  --> src/lib.rs:17:23
   |
16 | fn show_event<'a>(event: &'a Event) {
   |                          --------- this data with lifetime `'a`...
17 |     show_fields(event.span().fields());
   |                 ----- ^^^^
   |                 |
   |                 ...is captured here...
   |
note: ...and is required to live as long as `'static` here
  --> src/lib.rs:17:5
   |
17 |     show_fields(event.span().fields());
   |     ^^^^^^^^^^^

It's not immediately clear from this error where the 'static bound is introduced, especially when not as reduced. I only noticed that the 'static was probably being introduced by the APIT during reduction to file this issue.

It would be helpful to have a note/hint on the argument of the function that introduces the 'static bound. (And, as a side note, I'm not even sure if the 'static bound can be eliminated.)

This was tested to be the output on 1.56.0-nightly (2021-09-01 50171c3).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-varianceArea: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)T-compilerRelevant to the compiler 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