-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Closed
Copy link
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.