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

Bad suggestion for &mut in combination with Default (maybe also other traits) #100289

Closed
piegamesde opened this issue Aug 8, 2022 · 0 comments · Fixed by #100769
Closed

Bad suggestion for &mut in combination with Default (maybe also other traits) #100289

piegamesde opened this issue Aug 8, 2022 · 0 comments · Fixed by #100769
Assignees
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

@piegamesde
Copy link
Contributor

Given the following code:

fn foo(bar: &mut usize) {
    todo!()
}

fn main() {
    foo(&mut Default::default()); // Works
    foo(Default::default()); // Doesn't
}

The current output is:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `&mut usize: Default` is not satisfied
 --> src/main.rs:7:9
  |
7 |     foo(Default::default()); // Doesn't
  |     --- ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `&mut usize`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the following other types implement trait `Default`:
            f32
            f64
            i128
            i16
            i32
            i64
            i8
            isize
          and 6 others

For more information about this error, try `rustc --explain E0277`.

This output is confusing, because changing the type to e.g. i64 won't solve the problem and instead cause a lot of new ones. For a HashMap, I currently get the suggestion to use a HashSet. The correct solution would be to recognize that usize: Default and suggest to add a &mut. This may generalize to other similar situations (&) or traits.

@piegamesde piegamesde 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 Aug 8, 2022
@TaKO8Ki TaKO8Ki self-assigned this Aug 15, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 20, 2022
…o-trait-assoc-item, r=cjgillot

Suggest adding a reference to a trait assoc item

fixes rust-lang#100289
@bors bors closed this as completed in 7f02896 Aug 20, 2022
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

Successfully merging a pull request may close this issue.

2 participants