Cannot use lifetime 'de but must impose a lifetime bound on it

I have an enum Msg that basically looks like this:

#[derive(serde::Serialize, serde::Deserialize)]
pub enum Msg<'uz> {
   StartMsg(Start<'uz>)
   // ... other variants
}

Compilation fails:

error: lifetime may not live long enough
  --> src/types.rs:47:20
   |
44 | #[derive(serde::Serialize, serde::Deserialize)]
   |                            ------------------ lifetime `'de` defined here
45 | pub enum Msg<'uz>
   |              --- lifetime `'uz` defined here
46 | {
47 |     StartMsg(Start<'uz>),
   |                    ^^^^^^^^^^^^^ requires that `'de` must outlive `'uz`
   |
   = help: consider adding the following bound: `'de: 'uz`

However, following the compiler hint produces:

  --> src/types.rs:46:16
   |
45 | pub enum Msg<'uz>
   |              - help: consider introducing lifetime `'de` here: `'de,`
46 |     where 'uz: 'de
   |                ^^^ undeclared lifetime


Does the lifetime exist or does it not? This is really frustrating because basically Msgs only exist inside User structs and what I'm basically trying to express is that Users must outlive their deserialization source, which does not seem like it should be so diabolically hard.

Try:

#[derive(serde::Serialize, serde::Deserialize)]
pub enum Msg<'uz> {
    #[serde(borrow)]
    StartMsg(Start<'uz>),
}

See also.


I've seen that sort of diagnostic before; it is indeed a broken suggestion.

Thank you enormously. I'm curious why the compiler hint is so broken. Is there an open issue to make it more informative? If the macro expands to a function with a lifetime parameter 'de why didn't the compiler know about its existence.

Ah, my bad, the broken hint I was thinking of is something else (where there's no valid place to add the bound). Looking at the expansion of this macro, the hint is actually accurate,[1] albeit not friendly to the macro user. There's no good way to get from the error to knowing about serde's custom derive macro markers, and that's a hard nut to crack. The macro would need some way to tell the compiler "if this area of code produces this kind of error, supply this hint".


  1. FYI there's an Expand Macros option in the playground under Tools, top right, which is where I got the code from. ↩︎