Skip to content

A couple of confusing diagnostics with &str slices #26319

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

Closed
durka opened this issue Jun 15, 2015 · 3 comments
Closed

A couple of confusing diagnostics with &str slices #26319

durka opened this issue Jun 15, 2015 · 3 comments
Labels
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. WG-diagnostics Working group: Diagnostics

Comments

@durka
Copy link
Contributor

durka commented Jun 15, 2015

https://play.rust-lang.org/?gist=089ca2d7214f6761051c&version=stable

let s = "abc";
let t = if true { s[..2] } else { s };       // confusing error message
let u: &str = if true { s[..2] } else { s }; // clear error message
let v = s[..2];                              // strange error message
let w: &str = s[..2];                        // clear error message

These are four attempts to take a slice of a &str, in two pairs. They are all wrong because indexing a &str returns a str, so you need an extra &. That's not the point. The point is without type annotations, the error diagnostics seem like they could use some improvement.

Especially the first one -- it sounds like it is saying my expression ought to be a str, but it already is. I think it's actually saying the associated type Index::Output ought to be a str, but the way the diagnostic is written, it sounds backwards.

Can either of these diagnostics be improved?

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 23, 2015
@steveklabnik
Copy link
Member

Three of these have the same errors now: mismatched type between str and &str. The third has an error about Sized not being implemented.

@durka
Copy link
Contributor Author

durka commented Jan 7, 2017

Maybe there could be a suggestion, if a variable is inferred to type T: !Sized, to add a & and make the variable &T instead?

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Nov 15, 2017
@estebank
Copy link
Contributor

Suggested output:

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
 --> src/main.rs:3:5
  |
3 | let v = s[..2];                              // strange error message
  |     ^   ------ help: considering borrowing the slice: `&s[..2]`
  |     |
  |     `str` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `str`
  = note: all local variables must have a statically known size
help: considering borrowing the slice

kennytm added a commit to kennytm/rust that referenced this issue Nov 27, 2017
Suggest using slice when encountering `let x = ""[..];`

Fix rust-lang#26319.
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

4 participants