Skip to content

Conversion of Box<dyn Error + Send + Sync> to Box<dyn Error> confusingly complains about Sized #88001

@kornelski

Description

@kornelski

Playground

use std::error::Error;

fn test() -> Result<(), Box<dyn Error>> {
    err()?;
    Ok(())
}

fn err() -> Result<(), Box<dyn Error + Send + Sync>> {
    Err("".into())
}

The current output is:

error[E0277]: the size for values of type `dyn std::error::Error + Send + Sync` cannot be known at compilation time
 --> src/lib.rs:4:10
  |
4 |     err()?;
  |          ^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `dyn std::error::Error + Send + Sync`
  = note: required because of the requirements on the impl of `std::error::Error` for `Box<dyn std::error::Error + Send + Sync>`
  = note: required because of the requirements on the impl of `From<Box<dyn std::error::Error + Send + Sync>>` for `Box<dyn std::error::Error>`
  = note: required because of the requirements on the impl of `FromResidual<Result<Infallible, Box<dyn std::error::Error + Send + Sync>>>` for `Result<(), Box<dyn std::error::Error>>`
  = note: required by `from_residual`

Note that the type coercion of these types is allowed, because it's going from Send to non-Send type.

The error also happens if you use err.into() directly, but the in practice problem typically appears when using the ? operator.

The error is confusing, because From is an internal detail of ?, and blanket impl of From for Box<T> to Box<U> is another internal detail, and then inability of calling from(dyn T) -> dyn U is three internal details deep.

For usage with ? the output should look like:

`Box<dyn Error + Send + Sync>` cannot be converted to `Box<dyn Error>` using the `From` trait used by the `?` operator.
  = help: use `match` instead or change the `dyn` types to be the same.

Alternatively, it could focus only on usage with From/.into() and be like:

`Box<dyn Error + Send + Sync>` cannot be converted to `Box<dyn Error>` using the `From` trait
  = help: use type coercion to convert between these types

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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