Backtrace api and #[from]

Đang mở
#157 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rust
Lĩnh vực
api

Hướng nghiên cứu

Chạy Rust playground được liên kết và so sánh hai biến thể enum bằng các điểm vào backtrace() và source() được hiển thị trong ví dụ. Theo dõi cách hành vi được derive của thiserror xử lý #[from] và một Backtrace tường minh, sau đó xác định và ghi lại thứ tự ưu tiên dự kiến; được xem là hoàn tất khi hành vi được giải quyết một cách nhất quán hoặc được xác nhận rõ ràng là đúng như mong đợi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
}
Ngôn ngữ chính
Rust
Star
5.6k
Fork
220
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dtolnay/thiserror

Tất cả issue của dtolnay/thiserror

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.