Skip to content

#[derive(Error)] fails on enum variant with a lifetime. #511

@KSXGitHub

Description

@KSXGitHub

Reproduction code

#![allow(unused)]

use derive_more::{Display, Error};

#[derive(Debug, Display, Error)]
pub struct Owned {
    #[error(not(source))]
    message: String,
}

#[derive(Debug, Display, Error)]
pub struct Borrowed<'a> {
    #[error(not(source))]
    message: &'a str,
}

#[derive(Debug, Display, Error)]
pub enum MyError<'a> {
    Owned(Owned),
    Borrowed(Borrowed<'a>),
}

Compilation error

error: lifetime may not live long enough
  --> src/tmp_err_report.rs:17:26
   |
17 | #[derive(Debug, Display, Error)]
   |                          ^^^^^ returning this value requires that `'a` must outlive `'static`
18 | pub enum MyError<'a> {
   |                  -- lifetime `'a` defined here
   |
   = note: this error originates in the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)

Current workaround

Implement Error manually:

#[derive(Debug, Display)]
pub enum MyError<'a> {
    Owned(Owned),
    Borrowed(Borrowed<'a>),
}

impl<'a> core::error::Error for MyError<'a> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            MyError::Owned(error) => error.source(),
            MyError::Borrowed(error) => error.source(),
        }
    }
}

Metadata

Metadata

Assignees

Labels

bugk::apiRelated to API (application interface)

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions