Parametrized error types causes type evaluation overflows when recursive

Đang mở
#317 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
30/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
rust
Lĩnh vực
tooling

Hướng nghiên cứu

Tái hiện lỗi generic đệ quy trong src/main.rs và bắt đầu từ triển khai Error được tạo bởi macro derive. Xác định xem kiểu đệ quy được tham số hóa có thể biên dịch mà không làm tràn quá trình đánh giá trait hay không, và bổ sung coverage nếu dự án có vị trí kiểm thử phù hợp.

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

Mô tả

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?

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.