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

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 opened this issue Mar 4, 2020 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@najamelan
Copy link
Contributor

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.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 4, 2020
@estebank estebank added the D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. label Mar 6, 2020
@brain0
Copy link

brain0 commented Mar 11, 2020

The issue can also be reproduced with the following code snippet:

trait Tr1 {}

trait Tr2: Tr1 {}

impl<T: Tr2 + Sized> Tr1 for T {}

//impl Tr1 for [u8] {}

impl Tr2 for () {}

impl Tr2 for [u8] {}

Uncommenting the commented line makes the snippet compile. Without it, the error message is as follows:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/lib.rs:11:6
   |
11 | impl Tr2 for [u8] {}
   |      ^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `[u8]`
   = 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 `Tr1` for `[u8]`

@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

Current output:

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the size for values of type `dyn std::error::Error` cannot be known at compilation time
  --> src/main.rs:17:5
   |
17 |     library_function( || Ok(()) )
   |     ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `dyn std::error::Error`
   = help: the trait `std::error::Error` is implemented for `Box<T>`
   = note: required for `Box<dyn std::error::Error>` to implement `std::error::Error`
note: required by a bound in `library_function`
  --> src/main.rs:8:9
   |
5  | fn library_function<F1, E1>(f1: F1) -> Result<(), E1>
   |    ---------------- required by a bound in this function
...
8  |     E1: Error,
   |         ^^^^^ required by this bound in `library_function`
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/lib.rs:11:14
   |
11 | impl Tr2 for [u8] {}
   |              ^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `[u8]`
note: required for `[u8]` to implement `Tr1`
  --> src/lib.rs:5:22
   |
5  | impl<T: Tr2 + Sized> Tr1 for T {}
   |      -               ^^^     ^
   |      |
   |      unsatisfied trait bound introduced here
note: required by a bound in `Tr2`
  --> src/lib.rs:3:12
   |
3  | trait Tr2: Tr1 {}
   |            ^^^ required by this bound in `Tr2`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants