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 when using unsized types without indirection #28653

Closed
anmej opened this issue Sep 25, 2015 · 5 comments · Fixed by #74228
Closed

Confusing error when using unsized types without indirection #28653

anmej opened this issue Sep 25, 2015 · 5 comments · Fixed by #74228
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@anmej
Copy link

anmej commented Sep 25, 2015

fn foo(arg: Foo) {} //Foo is a trait
fn bar (arg: [i32]) {}
fn quz (arg: str) {}

All those functions return the following error: the trait core::marker::Sized is not implemented for the type...
This is confusing for a beginner, because it makes you think you need to implement a trait to make it work. The message could suggest using &Foo/&[i32]/&str instead.

@arielb1 arielb1 added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 25, 2015
@arielb1
Copy link
Contributor

arielb1 commented Sep 25, 2015

Should we add an on_unimplemented_help?

@steveklabnik
Copy link
Member

steveklabnik commented Mar 8, 2017

Triage: no change. Here's the actual diagnostic today

rustc 1.15.1 (021bd294c 2017-02-08)
error[E0277]: the trait bound `Foo + 'static: std::marker::Sized` is not satisfied
 --> <anon>:3:8
  |
3 | fn foo(arg: Foo) {} //Foo is a trait
  |        ^^^ the trait `std::marker::Sized` is not implemented for `Foo + 'static`
  |
  = note: `Foo + 'static` does not have a constant size known at compile-time
  = note: all local variables must have a statically known size

error[E0277]: the trait bound `[i32]: std::marker::Sized` is not satisfied
 --> <anon>:4:9
  |
4 | fn bar (arg: [i32]) {}
  |         ^^^ the trait `std::marker::Sized` is not implemented for `[i32]`
  |
  = note: `[i32]` does not have a constant size known at compile-time
  = note: all local variables must have a statically known size

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
 --> <anon>:5:9
  |
5 | fn quz (arg: str) {}
  |         ^^^ the trait `std::marker::Sized` is not implemented for `str`
  |
  = note: `str` does not have a constant size known at compile-time
  = note: all local variables must have a statically known size

@steveklabnik steveklabnik added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 8, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 24, 2017
@cyplo
Copy link
Contributor

cyplo commented Nov 1, 2017

Same message on rustc 1.23.0-nightly (8b22e70 2017-10-31)

@estebank
Copy link
Contributor

CC #23286

@estebank
Copy link
Contributor

Current output:

error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
 --> src/lib.rs:2:8
  |
2 | fn foo(arg: Foo) {} //Foo is a trait
  |        ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `(dyn Foo + 'static)`
  = 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: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error[E0277]: the size for values of type `[i32]` cannot be known at compilation time
 --> src/lib.rs:3:9
  |
3 | fn bar (arg: [i32]) {}
  |         ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[i32]`
  = 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: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:4:9
  |
4 | fn quz (arg: str) {}
  |         ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `str`
  = 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: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

@estebank estebank added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 22, 2020
Manishearth added a commit to Manishearth/rust that referenced this issue Jul 13, 2020
Provide structured suggestion on unsized fields and fn params

* Suggest borrowing or boxing unsized fields
* Suggest borrowing fn parameters
* Remove some verbosity of unsized errors
* Remove `on_unimplemented` note from `trait Sized`

Fix rust-lang#23286, fix rust-lang#28653.

r? @davidtwco
@bors bors closed this as completed in a364c0a Jul 14, 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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

6 participants