-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Type inference requires type of LHS to be known for binary operators #8280
Comments
Either - Some(&x) => map.insert(char, x + 1),
+ Some(&x) => map.insert(char, 1 + x), or - Some(&x) => map.insert(char, x + 1),
- None => map.insert(char, 1)
+ None => map.insert(char, 1),
+ Some(&x) => map.insert(char, x + 1), makes the inference work:
(There are various ("correct") type errors and mutability issues that stop it compiling from after this.) It seems like the original code should be legal, given the trivial adjustments (especially reordering the arms). (cc @nikomatsakis) |
Triage: I've updated the code for language changes; the inference is still suboptimal (i.e. it fails), the fixes in the comment above still address the inference issue, but also still hit the borrowing errors. The following code works fine: use std::hashmap;
fn main() {
let mut map = hashmap::HashMap::new();
let str = "Hello, world!";
for &char in str.to_ascii().iter() {
map.insert_or_update_with(char, 1, |_, v| *v += 1);
}
println!("{:?}", map);
} |
Reading this example more closely, I actually don't think inference is expected to succeed here. You haven't told it the type of the values in the map, but you attempting to do |
Update title. |
Multidispatch should have fixed this, correct? |
@Aatch It did not. The distinction has to do with needing to decide between "builtin" operators like |
This does compile (rather, type-check - of course it won't borrow-check) at 1.0 and above. |
The following does not work, however: fn main() {
0u8.into() == 0u8;
} It gives: "error: unable to infer enough type information about I'd been assuming that was this issue since it works if you switch the order of the operands, but if this one is solved perhaps it is a new one? |
My own attempt to concoct a modern reproduction no longer exhibits this bug: let mut v = Vec::new();
match v.get(0) {
Some(&x) => v.push(x + 1), // A-OK
None => v.push(1),
} In fact, our inference is so good these days that even this works: let mut v = Vec::new();
match v.get(0) {
Some(&x) => v.push(x + x),
None => v.push(1),
} Given that this bug was filed so long before 1.0 that the test case no longer works and that nobody seems to have come up with a reproduction of the original behavior, I'm closing this. |
So autoref for operators will make this stop working for the normal reasons. I suppose that would be a regression. I hope it won't be too terribly annoying. |
Add `msrv` config for `map_clone` Just a small PR to have some fun with Clippy and to clear my head a bit 😅 --- changelog: [`map_clone`]: The suggestion takes `msrv` into account changelog: Track `msrv` attribute for `manual_bits` and `borrow_as_prt` fixes: rust-lang#8276
This is the code:
Error message:
The text was updated successfully, but these errors were encountered: