Parametrized error types causes type evaluation overflows when recursive

Aperta
#317 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
30/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
tooling

Direzione di ricerca

Riproduci l’errore generico ricorsivo in src/main.rs e parti dall’implementazione di Error generata dalla macro derive. Determina se il tipo ricorsivo parametrizzato può essere compilato senza causare un overflow nella valutazione dei trait e aggiungi la copertura dei test se il progetto dispone di una posizione adatta per i test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

thiserror works well with recursive error types, and this snippet compiles with no error:

use std::error::Error;

use thiserror::Error; // 1.0.63

#[derive(Debug, Error)]
pub enum MyError {
    #[error("Recurse")]
    Recurse(#[source] Box<MyError>),
    #[error("Payload")]
    Payload(&'static str),
}


fn main() {
    let _: &dyn Error = &MyError::Payload("Hello");
}

But when one uses an error that is parametrized over his content, it causes an overflow in type evaluation:

use std::error::Error;

use thiserror::Error; // 1.0.63

#[derive(Debug, Error)]
pub enum MyError<Payload> {
    #[error("Recurse")]
    Recurse(#[source] Box<MyError<Payload>>),
    #[error("Payload")]
    Payload(Payload),
}


fn main() {
    let _: &dyn Error = &MyError::Payload("Hello");
}
   Compiling playground v0.0.1 (/playground)
error[E0275]: overflow evaluating the requirement `Box<MyError<&str>>: std::error::Error`
  --> src/main.rs:15:25
   |
15 |     let _: &dyn Error = &MyError::Payload("Hello");
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: required for `MyError<&str>` to implement `std::error::Error`
  --> src/main.rs:5:17
   |
5  | #[derive(Debug, Error)]
   |                 ^^^^^ unsatisfied trait bound introduced in this `derive` macro
6  | enum MyError<Payload> {
   |      ^^^^^^^^^^^^^^^^
   = note: required for the cast from `&MyError<&str>` to `&dyn std::error::Error`
   = note: this error originates in the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0275`.
error: could not compile `playground` (bin "playground") due to 1 previous error

I can easily implement by myself, but often this is not possible due to too many variants

use std::error::Error;
use std::fmt::{Display, Debug};

#[derive(Debug)]
pub enum MyError<Payload> {
    Recurse(Box<MyError<Payload>>),
    Payload(Payload),
}

impl<Payload> Display for MyError<Payload> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>)->std::fmt::Result {
        match self {
            Self::Recurse(_) => write!(f, "Recurse"),
            Self::Payload(_) => write!(f, "Payload"),
        }
    }
}

impl<Payload> Error for MyError<Payload>
where 
    MyError<Payload>: Debug + 'static,
{
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Recurse(source) => Some(source),
            Self::Payload(_) => None,
        }
    }
}

fn main() {
    let _: &dyn Error = &MyError::Payload("Hello");
}

Is this an hard limitation?

Lingua principale
Rust
Stelle
5.6k
Fork
220
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dtolnay/thiserror

Tutte le issue di dtolnay/thiserror

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.