Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
BugenZhao opened this issue Oct 31, 2023 · 0 comments · Fixed by #117434
Closed
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@BugenZhao
Copy link
Contributor

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
@BugenZhao BugenZhao added the C-bug Category: This is a bug. label Oct 31, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 31, 2023
BugenZhao added a commit to BugenZhao/rust-playground that referenced this issue Oct 31, 2023
@saethlin saethlin added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 31, 2023
@bors bors closed this as completed in 958a6af Nov 3, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 3, 2023
Rollup merge of rust-lang#117434 - BugenZhao:box-error-provide, r=cuviper

delegate `<Box<E> as Error>::provide` to `<E as Error>::provide`

Fix rust-lang#117432.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants