Skip to content

Confusing diagnostics where the error mentions Sized but not that a trait bound is not implemented (where it would be for a Sized type). #69719

Open
@najamelan

Description

@najamelan

This comes from the user forum, playground, moved from #27964:

use std::error::Error;

fn library_function<F1, E1>(f1: F1) -> Result<(), E1>
where
    F1: FnOnce() -> Result<(), E1>,
    E1: Error, // <-remove this line -> compile
{
    f1()
}

fn main() -> Result<(), Box<dyn Error>> 
{
    library_function( || Ok(()) )
}

Error:

error[E0277]: the size for values of type `dyn std::error::Error` cannot be known at compilation time
  --> src/main.rs:13:5
   |
3  | fn library_function<F1, E1>(f1: F1) -> Result<(), E1>
   |    ----------------
...
6  |     E1: Error, // <-remove this line -> compile
   |         ----- required by this bound in `library_function`
...
13 |     library_function( || Ok(()) )
   |     ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::error::Error`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required because of the requirements on the impl of `std::error::Error` for `std::boxed::Box<dyn std::error::Error>`

Go figure why "library_function doesn't have a size known at compile time".

Analysis:
There is a blanket impl for Error on Box<T>, so quickly looking it up in the docs makes you think that it should implement Error, but it's not on Box<T: ?Sized>. credit to @yandros

I feel the main issue here is that we have a trait bound on Error that isn't satisfied, so probably it would be nice to replace library_function not being sized by: Box<dyn Error> does not implement Error. The following Impls where found: impl<T: Error> Error for Box<T>, but that requires T: Sized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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