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

Suggest ?Sized on generics where applicable #38936

Closed
J-F-Liu opened this issue Jan 9, 2017 · 4 comments · Fixed by #68377
Closed

Suggest ?Sized on generics where applicable #38936

J-F-Liu opened this issue Jan 9, 2017 · 4 comments · Fixed by #68377
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system 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

@J-F-Liu
Copy link

J-F-Liu commented Jan 9, 2017

I meet a compile error can be demonstrated by the following code, and think it's irrational:

trait Train {
    fn knots(&self) -> usize;
}

impl Train for str {
    fn knots(&self) -> usize {
        self.chars().count()
    }
}

fn train_length<T:Train>(text: &'static T) -> usize {
    text.knots()
}

fn str_length(text: &'static str) -> usize {
    text.knots()
}

fn main() {
    println!("{:?}", str_length("asdfas"));   // works
    println!("{:?}", train_length("asdfas")); // compile error
}
@kevinmehall
Copy link
Contributor

Use fn train_length<T:Train + ?Sized>(text: &'static T) -> usize.

Bare str is an unsized type and generics assume that types are sized by default. Here's another post explaining unsized types.

@J-F-Liu
Copy link
Author

J-F-Liu commented Jan 9, 2017

Yes, add +?Sized can solve the problem, the compile message could be more helpful.

@Mark-Simulacrum Mark-Simulacrum added the A-diagnostics Area: Messages for errors, warnings, and lints label May 19, 2017
@Mark-Simulacrum Mark-Simulacrum changed the title irrational compile error Suggest ?Sized on generics where applicable May 19, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

Current output:

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> src/main.rs:23:35
   |
13 | fn train_length<T:Train>(text: &'static T) -> usize {
   |    ------------ - required by this bound in `train_length`
...
23 |     println!("{:?}", train_length("asdfas")); // compile error
   |                                   ^^^^^^^^ 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/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

It should be

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> src/main.rs:23:35
   |
13 | fn train_length<T:Train>(text: &'static T) -> usize {
   |    ------------ -      - help: relax the implicit restriction:  `+ ?Sized`
   |                 |
   |                 `Sized` implicitly required in this bound in `train_length`
...
23 |     println!("{:?}", train_length("asdfas")); // compile error
   |                                   ^^^^^^^^ 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/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

@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. A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 11, 2019
@estebank
Copy link
Contributor

estebank commented Feb 5, 2020

Current output:

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> file4.rs:21:35
   |
11 | fn train_length<T:Train>(text: &'static T) -> usize {
   |    ------------ -      - help: consider relaxing the implicit `Sized` restriction: `+  ?Sized`
   |                 |
   |                 required by this bound in `train_length`
...
21 |     println!("{:?}", train_length("asdfas")); // compile error
   |                                   ^^^^^^^^ 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/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

Fixed in #68377.

@estebank estebank closed this as completed Feb 5, 2020
@fmease fmease added A-trait-system Area: Trait system and removed A-trait-system Area: Trait system labels Dec 21, 2024
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-trait-system Area: Trait system 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.

5 participants