Skip to content

&{integer} is incorrectly inferenced to i32 #68345

Closed
@nonzerofloat

Description

@nonzerofloat
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}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions