Backtrace api and #[from]

未关闭
#157 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
rust
领域
api

调研方向

运行链接的 Rust playground,并使用示例中展示的 backtrace() 和 source() 入口点比较两个 enum 变体。跟踪派生的 thiserror 行为如何处理 #[from] 和显式的 Backtrace,然后确定并记录预期的优先级;当该行为得到一致解决,或明确确认符合预期时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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
}
主要语言
Rust
星标
5.6k
派生
220
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dtolnay/thiserror 的其他 Issue

查看 dtolnay/thiserror 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。