Skip to content

Box<dyn std::error::Error> fails to satisfy TryFrom::Error: std::error::Error #149086

@ultimaweapon

Description

@ultimaweapon

I tried this code:

fn main() {
    foo::<Bar>();
}

fn foo<T>() -> Result<T, T::Error>
where
    T: TryFrom<i64, Error: std::error::Error>,
{
    todo!();
}

struct Bar;

impl TryFrom<i64> for Bar {
    type Error = Box<dyn std::error::Error>;

    fn try_from(_: i64) -> Result<Self, Self::Error> {
        Ok(Self)
    }
}

I expected to see this happen: It compile success.

Instead, this happened:

error[E0277]: the size for values of type `(dyn std::error::Error + 'static)` cannot be known at compilation time
 --> src/main.rs:2:11
  |
2 |     foo::<Bar>();
  |           ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn std::error::Error + 'static)`
  = help: the trait `std::error::Error` is implemented for `Box<E>`
  = note: required for `Box<(dyn std::error::Error + 'static)>` to implement `std::error::Error`
note: required by a bound in `foo`
 --> src/main.rs:7:28
  |
5 | fn foo<T>() -> Result<T, T::Error>
  |    --- required by a bound in this function
6 | where
7 |     T: TryFrom<i64, Error: std::error::Error>,
  |                            ^^^^^^^^^^^^^^^^^ required by this bound in `foo`

For more information about this error, try `rustc --explain E0277`.

Meta

rustc --version --verbose:

rustc 1.91.0 (f8297e351 2025-10-28)
binary: rustc
commit-hash: f8297e351a40c1439a467bbbb6879088047f50b3
commit-date: 2025-10-28
host: x86_64-unknown-linux-gnu
release: 1.91.0
LLVM version: 21.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutA-error-handlingArea: Error handlingD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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