Backtrace api and #[from]

Abierto
#157 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
rust
Área
api

Línea de trabajo

Ejecuta el Rust playground enlazado y compara las dos variantes del enum usando los puntos de entrada backtrace() y source() que se muestran en el ejemplo. Sigue cómo el comportamiento derivado de thiserror trata #[from] y un Backtrace explícito, y determina y documenta la precedencia prevista; se considera terminado cuando el comportamiento se haya resuelto de forma coherente o se haya confirmado explícitamente como esperado.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Hi,

I discovered a little strange behavior when using the experimental backtrace api together with thiserror.

I have prepared an example gist and the playground

You get different behavior when trying to access the backtrace of an error behind an enum.

When the enum value is derived with from , without specifying an own backtrace member (as tuple)
the backtrace() api call from the Error trait will return None

When the enum value has an own backtrace member, the backtrace() api call will return the backtrace of the underlying error.
Accessing the backtrace value of the enum value is not possible anymore.

From my POV the best way would be that the backtrace() api call should always return the uppermost backtrace if it is specified. Because it would be still possible to use the source() api call to get to the inner error.

Is the current behavior a bug or expected ?

Thanks,
Peter

Putting the code example here as reference also:

#![feature(backtrace)]
#![feature(backtrace_frames)]

use std::{backtrace::Backtrace, error::Error};

use thiserror::Error as ThisError;

#[derive(ThisError, Debug)]
#[error("MyError")]
struct MyError {
    backtrace: std::backtrace::Backtrace,
}

#[derive(ThisError, Debug)]
#[error("OtherError")]
struct OtherError {
    backtrace: std::backtrace::Backtrace,
}

#[derive(ThisError, Debug)]
enum AllErrors {
    #[error("E")]
    E(#[from] MyError),

    #[error("O")]
    O(#[from] OtherError, Backtrace),
}

fn main() {
   let my = MyError {
        backtrace: std::backtrace::Backtrace::capture(), // Backtrace location A
    };
    assert!(my.backtrace().is_some());

    let other = OtherError {
        backtrace: std::backtrace::Backtrace::capture(), // Backtrace location B
    };
    assert!(other.backtrace().is_some());

    let all_my = AllErrors::from(my);
    let all_other = AllErrors::from(other); // Implicit Backtrace C

    println!("My my {:#?}", all_my.backtrace()); // -> None
    println!("My my source {:#?}", all_my.source().unwrap().backtrace()); // -> Backtrace A

    println!("Other my {:#?}", all_other.backtrace()); // -> Backtrace B
    println!(
        "Other my source {:#?}",
        all_other.source().unwrap().backtrace() // Backtrace B
    );
    
    // Inconsistency:
    // Option 1 : Always return inner backtrace
    // all_my.backtrace() should return Backtrace A
    // all_other.backtrace() should return Backtrace B // as
    
    // Option 2: Always deliver own backtrace
    // all_my.backtrace() should return None
    // all_other.backtrace() should return Backtrace C ( where all_other was created )
    
    // The prefered option is Option 2 as currently there is no way to access backtrace C
    // using the backtrace interface
}
Lenguaje dominante
Rust
Estrellas
5.6k
Forks
220
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de dtolnay/thiserror

Todos los issues de dtolnay/thiserror

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.