Parametrized error types causes type evaluation overflows when recursive
还没有人认领这个 Issue。
评估
调研方向
在 src/main.rs 中重现递归泛型错误,并从 derive 宏生成的 Error 实现开始。确定参数化递归类型是否可以在不导致 trait 求值溢出的情况下编译,并在项目有合适的测试位置时添加覆盖测试。
由索引模型根据 Issue 内容生成。
描述
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?
- 主要语言
- Rust
- 星标
- 5.6k
- 派生
- 220
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dtolnay/thiserror 的其他 Issue
-
Improve #[from] documentation example.可能已有人在做 @SatvikMishra08 于 50 天前认领。 未关闭
难度 1/5 1 小时以内 新手友好度 62/100
-
难度 5/5 一周以上 新手友好度 25/100
-
难度 5/5 一周以上 新手友好度 35/100
-
No `unused field` warnings are produced on enums deriving thiserror::Error可能已有人在做 @usmanovbf 于 240 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 45/100
-
难度 5/5 一周以上 新手友好度 30/100
查看 dtolnay/thiserror 的全部 Issue
相似的 Issue
-
Selected chart highlights only its values, not its series names and category labels可能已有人在做 @Higgs32584 今天认领。 未关闭bug priority: low ui
难度 2/5 1-3 小时 新手友好度 82/100
storytold/gridcraft#4390 · 2 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
bug bugfix priority: medium
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
quarto-dev/q2#1219 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100