Skip to content

Supplying an &&str when an &str is expected missing a suggestion #66023

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

Open
gilescope opened this issue Nov 1, 2019 · 1 comment
Open

Supplying an &&str when an &str is expected missing a suggestion #66023

gilescope opened this issue Nov 1, 2019 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

@gilescope
Copy link
Contributor

    let a = std::collections::HashMap::<String,String>::new();
    let s = "hello";
    let _b = a[&s];
               ^ & not needed, this is already a reference

playground

But the error we currently get is:

error[E0277]: the trait bound `std::string::String: std::borrow::Borrow<&str>` is not satisfied
 --> src/main.rs:4:14
  |
4 |     let _b = a[&s];
  |              ^^^^^ the trait `std::borrow::Borrow<&str>` is not implemented for `std::string::String`
  |
  = help: the following implementations were found:
            <std::string::String as std::borrow::Borrow<str>>
  = note: required because of the requirements on the impl of `std::ops::Index<&&str>` for `std::collections::HashMap<std::string::String, std::string::String>`

Adding one too many layers of indirection is fairly common in rust especially with String / &str so it would be excellent if Rust could hint to the user how to fix this.

@gilescope
Copy link
Contributor Author

If the index was just a normal method, the below code compiles with no errors:

fn fn_with_ref_arg(_s: &str) {}

fn main() {
    let s = "hello";
    let _b = fn_with_ref_arg(&s);
}

I'm assuming that's because the compiler will dereference any number of times till it gets a type match.

Rather than improve the error message, should we be dereferencing indexes as many times as necessary for consistency?

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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 Nov 4, 2019
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Dec 8, 2019
estebank added a commit to estebank/rust that referenced this issue Nov 13, 2023
Use more targetted span for index obligation failures by rewriting the
obligation cause span.

CC rust-lang#66023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 14, 2023
…rrors

Always point at index span on index obligation failure

Use more targetted span for index obligation failures by rewriting the obligation cause span.

CC rust-lang#66023
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 14, 2023
Always point at index span on index obligation failure

Use more targetted span for index obligation failures by rewriting the obligation cause span.

CC rust-lang#66023
estebank added a commit to estebank/rust that referenced this issue Nov 14, 2023
When encountering

```rust
    let a = std::collections::HashMap::<String,String>::new();
    let s = "hello";
    let _b = &a[&s];
```
suggest `let _b = &a[s];`.

Fix rust-lang#66023.
estebank added a commit to estebank/rust that referenced this issue Nov 14, 2023
When encountering

```rust
    let a = std::collections::HashMap::<String,String>::new();
    let s = "hello";
    let _b = &a[&s];
```
suggest `let _b = &a[s];`.

Fix rust-lang#66023.
estebank added a commit to estebank/rust that referenced this issue Nov 14, 2023
When encountering

```rust
    let a = std::collections::HashMap::<String,String>::new();
    let s = "hello";
    let _b = &a[&s];
```
suggest `let _b = &a[s];`.

Fix rust-lang#66023.
estebank added a commit to estebank/rust that referenced this issue Dec 2, 2023
When encountering

```rust
    let a = std::collections::HashMap::<String,String>::new();
    let s = "hello";
    let _b = &a[&s];
```
suggest `let _b = &a[s];`.

Fix rust-lang#66023.
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` C-enhancement Category: An issue proposing an enhancement or a PR with one. 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.

3 participants