Skip to content

&{integer} is incorrectly inferenced to i32 #68345

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
nonzerofloat opened this issue Jan 18, 2020 · 1 comment
Closed

&{integer} is incorrectly inferenced to i32 #68345

nonzerofloat opened this issue Jan 18, 2020 · 1 comment

Comments

@nonzerofloat
Copy link

nonzerofloat commented Jan 18, 2020

fn main() {
    let v = gen(1000);
    println!("{:?}", v);
    assert_eq!(gen(1_000_000_000).len(), 1023);
}

fn gen(n: u64) -> Vec<u64> {
    let mut v = vec![0];
    let mut i = 0;
    let mut len = 1;
    let mut w = 1;
    loop {
        for k in [4, 7].into_iter() {
            let mut x = i;
            let y = i + len;
            while x < y {
                let z = k * w + v[x];
                if n < z {
                    return v;
                }
                v.push(z);
                x += 1;
            }
        }
        w *= 10;
        i += len;
        len *= 2;
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=81011b06836fc24bd4649a9358207ba9

Expected output:

[0, 4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777]

Instead, I get:

   Compiling playground v0.0.1 (/playground)
error[E0277]: cannot add `u64` to `i32`
  --> src/main.rs:17:31
   |
17 |                 let z = k * w + v[x];
   |                               ^ no implementation for `i32 + u64`
   |
   = help: the trait `std::ops::Add<u64>` is not implemented for `i32`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

When I change the for line for k in [4, 7].into_iter() { to for k in [4, 7].into_iter().copied {, there's no compile error. I think there's a bug in type inference for &{integer}.

@nonzerofloat
Copy link
Author

#57447

I checked the duplicate issue. I close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant