Skip to content

impl<T: Error> Error for Box<T> does not delegate its provide method implementation #117432

@BugenZhao

Description

@BugenZhao

I tried this code:

#![feature(error_generic_member_access)]

#[derive(Debug)]
struct Foo;

#[derive(Debug)]
struct MyError {
    foo: Foo,
}

impl std::fmt::Display for MyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MyError")
    }
}

impl std::error::Error for MyError {
    fn provide<'a>(&'a self, request: &mut std::error::Request<'a>) {
        request.provide_ref::<Foo>(&Foo);
    }
}

fn foo_provided<T: std::error::Error>(e: &T) -> bool {
    std::error::request_ref::<Foo>(e).is_some()
}

fn main() {
    let e = MyError { foo: Foo };

    assert!(foo_provided::<MyError>(&e));                // ok
    assert!(foo_provided::<&MyError>(&&e));              // ok
    assert!(foo_provided::<Box<MyError>>(&Box::new(e))); // fails
}

I expected to see this happen:

<Box<MyError> as std::error::Error>::provide should delegate its implementation to <MyError as std::error::Error>::provide, just like what impl<'a, T: Error> Error for &'a T does.

Instead, this happened:

It does not and uses the default trait implementation instead.

#[stable(feature = "box_error", since = "1.8.0")]
impl<T: core::error::Error> core::error::Error for Box<T> {
#[allow(deprecated, deprecated_in_future)]
fn description(&self) -> &str {
core::error::Error::description(&**self)
}
#[allow(deprecated)]
fn cause(&self) -> Option<&dyn core::error::Error> {
core::error::Error::cause(&**self)
}
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
core::error::Error::source(&**self)
}
}

Meta

rustc --version --verbose:

rustc 1.75.0-nightly (249624b50 2023-10-20)
binary: rustc
commit-hash: 249624b5043013d18c00f0401ca431c1a6baa8cd
commit-date: 2023-10-20
host: aarch64-apple-darwin
release: 1.75.0-nightly
LLVM version: 17.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions