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

Surprising diagnostics for E0623 lifetime mismatch #97392

Open
djc opened this issue May 25, 2022 · 2 comments
Open

Surprising diagnostics for E0623 lifetime mismatch #97392

djc opened this issue May 25, 2022 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@djc
Copy link
Contributor

djc commented May 25, 2022

Given the following code:

use std::collections::HashMap;
use std::marker::PhantomData;

fn diff(
    keys: &[Option<&str>],
    maps: &(
        HashMap<Option<&str>, Vec<&mut Record>>,
        HashMap<Option<&str>, Vec<&mut Record>>,
    ),
) {
    for key in keys {
        let info = (maps.0.get(key), maps.1.get(key));
        if info.0 == info.1 {
            continue;
        }
    }
}

#[derive(Debug, PartialEq)]
struct Record<'a>(PhantomData<&'a ()>);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
enum Enum {
    Foo,
    Bar,
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f47c0f93783ccca97fa1a040ce78f668

The current output is:

error[[E0623]](https://doc.rust-lang.org/stable/error-index.html#E0623): lifetime mismatch
  --> src/lib.rs:13:22
   |
7  |         HashMap<Option<&str>, Vec<&mut Record>>,
   |                                        ------
8  |         HashMap<Option<&str>, Vec<&mut Record>>,
   |                                        ------ these two types are declared with different lifetimes...
...
13 |         if info.0 == info.1 {
   |                      ^^^^^^ ...but data from `maps` flows into `maps` here

error[[E0623]](https://doc.rust-lang.org/stable/error-index.html#E0623): lifetime mismatch
  --> src/lib.rs:13:22
   |
7  |         HashMap<Option<&str>, Vec<&mut Record>>,
   |                                        ------ these two types are declared with different lifetimes...
8  |         HashMap<Option<&str>, Vec<&mut Record>>,
   |                                        ------
...
13 |         if info.0 == info.1 {
   |                      ^^^^^^ ...but data from `maps` flows into `maps` here

For more information about this error, try `rustc --explain E0623`.

I'm not sure what the ideal output for this, but here are some improvement points:

  • Showing what appears to be the same error twice
  • The explanation of "data [..] flows into [..]" seems really weird considering this is just a comparison
  • Why does the borrow checker care about the exact lifetimes for a comparison anyway?
  • Maybe this should mention something about the inner Record lifetime
  • Why is the error pointing at the info.1 value, which is derived from a maps value, but also at the keys of maps?

Another case where I got quite confused:

use anyhow::Error;

#[tokio::main]
async fn main() {
    let store = Store { p1: Pool1 };
    let t = Pool2.begin();
    let mut store_mut = store.get_mut(&mut t);
    store_mut.bar().await.unwrap();
    ()
}

struct Store {
    p1: Pool1,
}

impl Store {
    fn get_mut<'t, 's: 't>(&'s self, t: &'t mut T<'t>) -> StoreMut<'s> {
        StoreMut {
            p1: &self.p1,
            t,
        }
    }
}

struct StoreMut<'a> {
    p1: &'a Pool1,
    t: &'a mut T<'a>,
}

impl<'a> StoreMut<'a> {
    async fn bar(&mut self) -> Result<(), Error> {
        Ok(())
    }
}

struct Pool1;

struct Pool2;

impl Pool2 {
    fn begin(&self) -> T<'static> {
        T { s: "foo" }
    }
}

struct T<'a> {
    s: &'a str,
}

impl<'a> T<'a> {
    fn finalize(self) {
        
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f50a36b183385f8247f688ca7dd5cb41

Which gave me the error:

error[[E0623]](https://doc.rust-lang.org/stable/error-index.html#E0623): lifetime mismatch
  --> src/main.rs:18:9
   |
17 |       fn get_mut<'t, 's: 't>(&'s self, t: &'t mut T<'t>) -> StoreMut<'s> {
   |                              --------     ------------- these two types are declared with different lifetimes...
18 | /         StoreMut {
19 | |             p1: &self.p1,
20 | |             t,
21 | |         }
   | |_________^ ...but data from `t` flows into `self` here

Seems really weird to say that data from t is flowing into self in this case.

@djc djc 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 May 25, 2022
@JohnTitor JohnTitor added D-confusing Diagnostics: Confusing error or lint that should be reworked. A-lifetimes Area: lifetime related labels May 25, 2022
@cmtm
Copy link

cmtm commented Jul 7, 2022

First example:

The explanation of "data [..] flows into [..]" seems really weird considering this is just a comparison

I agree that the error message is confusing here.

Why does the borrow checker care about the exact lifetimes for a comparison anyway?

It doesn't. From what I can tell, that isn't what's going on. #[derive(PartialEq)] only causes the PartialEq trait to be implemented if both the lhs and rhs are of the same type. That inludes the lifetype parameter. So when comparing two types with different lifetime parameters, there's no PartialEq implemented for it. I don't know why the error message doesn't just say that. Something like: '==' isn't defined for these types, and then a hint saying something like it would exist if the lifetimes were the same.

Second example

That error message makes no sense to me. I know why it's not valid, because 's outlives 't, and it can't because 's is the lifetime of of the returned StoreMut, so 't would at least outlive 's.

@estebank
Copy link
Contributor

Current output:

error: lifetime may not live long enough
  --> src/lib.rs:12:21
   |
6  |     maps: &(
   |     ----
   |     |
   |     has type `&(HashMap<Option<&str>, Vec<&mut Record<'1>>>, HashMap<Option<&str>, Vec<&mut Record<'_>>>)`
   |     has type `&(HashMap<Option<&str>, Vec<&mut Record<'_>>>, HashMap<Option<&str>, Vec<&mut Record<'2>>>)`
...
12 |         let info = (maps.0.get(key), maps.1.get(key));
   |                     ^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`
   |
   = note: requirement occurs because of a mutable reference to `Record<'_>`
   = note: mutable references are invariant over their type parameter
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

error: lifetime may not live long enough
  --> src/lib.rs:12:38
   |
6  |     maps: &(
   |     ----
   |     |
   |     has type `&(HashMap<Option<&str>, Vec<&mut Record<'_>>>, HashMap<Option<&str>, Vec<&mut Record<'2>>>)`
   |     has type `&(HashMap<Option<&str>, Vec<&mut Record<'1>>>, HashMap<Option<&str>, Vec<&mut Record<'_>>>)`
...
12 |         let info = (maps.0.get(key), maps.1.get(key));
   |                                      ^^^^^^^^^^^^^^^ argument requires that `'2` must outlive `'1`
   |
   = note: requirement occurs because of a mutable reference to `Record<'_>`
   = note: mutable references are invariant over their type parameter
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
error: lifetime may not live long enough
  --> src/main.rs:18:9
   |
17 |       fn get_mut<'t, 's: 't>(&'s self, t: &'t mut T<'t>) -> StoreMut<'s> {
   |                  --  -- lifetime `'s` defined here
   |                  |
   |                  lifetime `'t` defined here
18 | /         StoreMut {
19 | |             p1: &self.p1,
20 | |             t,
21 | |         }
   | |_________^ method was supposed to return data with lifetime `'s` but it is returning data with lifetime `'t`
   |
   = help: consider adding the following bound: `'t: 's`
   = note: requirement occurs because of the type `StoreMut<'_>`, which makes the generic argument `'_` invariant
   = note: the struct `StoreMut<'a>` is invariant over the parameter `'a`
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

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-lifetimes Area: lifetime related D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

4 participants