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 error message when passing trait object to generic function #57744

Closed
wesleywiser opened this issue Jan 18, 2019 · 1 comment · Fixed by #68377
Closed

Confusing error message when passing trait object to generic function #57744

wesleywiser opened this issue Jan 18, 2019 · 1 comment · Fixed by #68377
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-traits Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@wesleywiser
Copy link
Member

wesleywiser commented Jan 18, 2019

This could be the same issue as #23286 but I'm not sure.

The following code gives this error (playground):

use std::io::Write;

fn trait_obj(w: &dyn Write) {
    generic(w);
}

fn generic<W: Write>(_w: &W) {
}
error[E0277]: the size for values of type `dyn std::io::Write` cannot be known at compilation time
 --> src/lib.rs:4:5
  |
4 |     generic(w);
  |     ^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `dyn std::io::Write`
  = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
note: required by `generic`
 --> src/lib.rs:7:1
  |
7 | fn generic<W: Write>(_w: &W) {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

There's a couple reasons this is very confusing:


4 |     generic(w);
  |     ^^^^^^^ doesn't have a size known at compile-time

points to the function name not the parameter w.


the size for values of type `dyn std::io::Write` cannot be known at compilation time

dyn Write only appears behind a reference which means it should be sized.

We should show a better error message when you pass a &dyn T value to a function expecting a generic &T value.

cc @munik

@estebank
Copy link
Contributor

estebank commented Oct 10, 2019

Current output:

error[E0277]: the size for values of type `dyn std::io::Write` cannot be known at compilation time
 --> src/lib.rs:4:13
  |
4 |     generic(w);
  |             ^ doesn't have a size known at compile-time
...
7 | fn generic<W: Write>(_w: &W) {
  |    ------- - required by this bound in `generic`
  |
  = help: the trait `std::marker::Sized` is not implemented for `dyn std::io::Write`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

A possible solution would be for the diagnostic to suggest ?Sized:

error[E0277]: the size for values of type `dyn std::io::Write` cannot be known at compilation time
 --> src/lib.rs:4:13
  |
4 |     generic(w);
  |             ^ doesn't have a size known at compile-time
...
7 | fn generic<W: Write>(_w: &W) {
  |    ------- -       - help: consider relaxing the implicit `Sized` constraint: `+ ?Sized`
  |            |
  |            required by this bound in `generic`
  |            
  |
  = help: the trait `std::marker::Sized` is not implemented for `dyn std::io::Write`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

CC #27964

@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 10, 2019
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Nov 5, 2019
@bors bors closed this as completed in 5b0caef Feb 4, 2020
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-traits Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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