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

Improve diagnostics when accidentally making str instead of &str #88800

Open
jruderman opened this issue Sep 9, 2021 · 0 comments
Open

Improve diagnostics when accidentally making str instead of &str #88800

jruderman opened this issue Sep 9, 2021 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jruderman
Copy link
Contributor

Given the following code:

fn first_three(s: &str) -> String {
    String::from(s[0..3])
}

fn main() {
    println!("{}", first_three("hello"));
}

The current output is:

error[E0277]: the size for values of type `str` cannot be known at compilation time
   --> src/main.rs:2:18
    |
2   |     String::from(s[0..3])
    |                  ^^^^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `str`
note: required by `from`

error[E0277]: the trait bound `String: From<str>` is not satisfied
   --> src/main.rs:2:5
    |
2   |     String::from(s[0..3])
    |     ^^^^^^^^^^^^ the trait `From<str>` is not implemented for `String`
    |
    = help: the following implementations were found:
              <String as From<&String>>
              <String as From<&mut str>>
              <String as From<&str>>
              <String as From<Box<str>>>
            and 2 others
note: required by `from`

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/main.rs:2:5
  |
2 |     String::from(s[0..3])
  |     ^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`
  = note: all function arguments must have a statically known size
  = help: unsized fn params are gated as an unstable feature

Ideally the output should look like:

help: borrow as a &str slice: &s[0..3]

(and perhaps a bit less verbosity on the errors; #56607 will help)

@jruderman jruderman added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 9, 2021
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 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

1 participant