serde icon indicating copy to clipboard operation
serde copied to clipboard

Please improve the error message for borrowed derived types

Open oriongonza opened this issue 2 years ago • 1 comments

use serde::*;

#[derive(Debug, Deserialize)]
pub struct COMPOSITEREF <'a> {
    pub ID: &'a str,
    pub TAG: &'a str,
}

#[derive(Debug, Deserialize)]
pub struct COMPOSITES <'de>{
    pub compositeref: Vec<COMPOSITEREF<'de>>,
}

gives this error:

error: lifetime may not live long enough
  --> src/main.rs:99:5
   |
97 | #[derive(Debug, Deserialize)]
   |                 ----------- lifetime `'de` defined here
98 | pub struct CODESETS<'a> {
   |                     -- lifetime `'a` defined here
99 |     pub codeset: Vec<CODESET<'a>>,
   |     ^^^ requires that `'de` must outlive `'a`
   |
   = help: consider adding the following bound: `'de: 'a`

error: lifetime may not live long enough
   --> src/main.rs:121:5
    |
119 | #[derive(Debug, Deserialize)]
    |                 ----------- lifetime `'de` defined here
120 | pub struct COMPOSITES<'a> {
    |                       -- lifetime `'a` defined here
121 |     pub compositeref: Vec<COMPOSITEREF<'a>>,
    |     ^^^ requires that `'de` must outlive `'a`
    |
    = help: consider adding the following bound: `'de: 'a`
   
which is not very helpful. A hint to `#[serde(borrow)]` could help new users (like me)

oriongonza avatar Sep 14 '23 20:09 oriongonza

I don't think we're able to change anything here without support from the Rust compile for producing custom diagnostics for our types and traits

oli-obk avatar Sep 15 '23 10:09 oli-obk