Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear introduction of 'static with APIT #88682

Open
CAD97 opened this issue Sep 6, 2021 · 2 comments
Open

Unclear introduction of 'static with APIT #88682

CAD97 opened this issue Sep 6, 2021 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Sep 6, 2021

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).

@CAD97 CAD97 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 6, 2021
@CAD97
Copy link
Contributor Author

CAD97 commented Sep 6, 2021

Thanks to users on urlo, we've both figured out exactly where the 'static obligation comes from as well as how to avoid it.

This is a tricky situation to figure out. In summary:

  • Span::fields is returning impl 'a + Iterator<Item = (&'static str, &'a str)>, and
  • show_fields wants impl Iterator<Item = (&'d str, &'d str)>.

Because return position impl Trait is invariant (because it could potentially be fulfilled by a type which is invariant over the lifetime(s)), we fail to unify the two types, because we can't have 'd = 'static and 'd = 'a at the same time.

rustc then chooses to explain the mismatch as "has lifetime 'a but needs to satisfy 'static, which is... not all that illuminating to where the 'static came from, thus the diagnostic issue.

I do not know how best to expose this logical chain to the user, but I think it's necessary to give more information to pin in onto variance. The error message "blames" show_fields for introducing the 'static obligation, where it's not really at fault, since it doesn't require the argument is 'static; the 'static comes from trying to unify an invariant short lifetime with an invariant long lifetime.

@BGR360
Copy link
Contributor

BGR360 commented Dec 27, 2021

@rustbot label +A-variance

@rustbot rustbot added the A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) label Dec 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants