Skip to content

Incorrect compiler suggestion for adding lifetime bounds to derive macros #107694

Open
@Mingun

Description

@Mingun

Code

use serde::Deserialize; // 1.0.152

#[derive(Deserialize)]
struct Nested {
    float: f64,
}

#[derive(Deserialize)]
#[serde(tag = "tag")]
enum InternallyTagged {
    Flatten {
        #[serde(flatten)]
        nested: Nested,
        string: &'static str,
    },
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0261]: use of undeclared lifetime name `'de`
 --> src/lib.rs:8:10
  |
8 | #[derive(Deserialize)]
  |          ^^^^^^^^^^^
  |          |
  |          undeclared lifetime
  |          lifetime `'de` is missing in item created through this procedural macro
  |
  = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
  = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider making the bound lifetime-generic with a new `'de` lifetime
  |
8 | #[derive(for<'de> Deserialize)]
  |          ++++++++
help: consider making the bound lifetime-generic with a new `'de` lifetime
  |
8 | #[derive(for<'de> Deserialize)]
  |          ++++++++
help: consider introducing lifetime `'de` here
  |
8 | #[derive(Deserialize<'de>)]
  |                     +++++

For more information about this error, try `rustc --explain E0261`.
error: could not compile `playground` due to previous error

Desired output

No response

Rationale and extra context

The error on playground.

The compiler suggest incorrect corrections to add lifetimes where they cannot appear. The reproduction of this bug depends on the serde bug serde-rs/serde#2371, which should not be fixed.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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