0

I have a struct with a non 'static lifetime, this sruct is then used by enum that represents my error:

#[derive(Debug, Fail)]
pub enum ParserError<'a> {
    #[fail(display = "Unexpected [{}] got {}", _1, _0)]
    ExpectedAGotB(Token<'a>, TokenKindExpectedOptions<'a>),
}

Because of failure's requirement of 'static lifetime for #[derive(Failre)] I'm getting this error:

error[E0478]: lifetime bound not satisfied
  --> src/lib/ra_parser/src/errors.rs:18:17
   |
18 | #[derive(Debug, Fail)]
   |                 ^^^^
   |
note: lifetime parameter instantiated with the lifetime `'a` as defined on the impl at 19:22
  --> src/lib/ra_parser/src/errors.rs:19:22
   |
19 | pub enum ParserError<'a> {
   |                      ^^
   = note: but lifetime parameter must outlive the static lifetime
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

What's my strategy to solve this? Is there way to make Token<'static>, perhaps not. Should I impl something myself?

anvlkv
  • 147
  • 2
  • 11
  • The strategy to solve this is to avoid including the `Token` itself in the error. Instead, include the identifying information you need in owned form. If you really need the token itself, you need to create an owned version of `Token`, but generally that shouldn't be necessary. You can provide a constructor for `ParseError` taking a `Token` and `TokenKindExpectedOptions` and extracting the needed information to store in the error. – Sven Marnach Jul 03 '20 at 19:45
  • @SvenMarnach You right, that does it and I can conveniently destructure when creating errors. Great! – anvlkv Jul 04 '20 at 08:16

0 Answers0