Rust-analyzer gives error on syntax that compiles

Hi,
In multiple editors (Kakoune and Helix) I've tried the following code:

#[cfg(test)]
mod tests {
    const distance_tests : Vec<(((i32, i32), (i32, i32)), i32)> = vec![
        (((0, 0), (0, 0)), 0);
        (((0, 0), (1, 0)), 1);
        (((0, 0), (0, 1)), 1);
        (((0, 0), (-1, 0)), 1);
        (((0, 0), (0, -1)), 1);
        (((14, 13), (14, 13)), 0);
        (((10, -3), (-16, 2)), 31);
        (((-16, 2), (10, -3)), 31);
        (((1, 10), (1, -10)), 20);
        (((1, 10), (-1, 10)), 2);
        (((1, 10), (-1, -10)), 22)
    ];

    #[test]
    fn test_distance() {
    }
}

fn main() {
    println!("Hello, world!");
}

rust-analyzer gives me the following diagnostics:

src/main.rs:3:11: warning: Constant `distance_tests` should have UPPER_SNAKE_CASE name, e.g. `DISTANCE_TESTS`
src/main.rs:5:30: error: no rules expected the token `;`
no rules expected this token in macro call
/home/alice/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/macros.rs:46:18: while trying to match meta-variable `$n:expr`
/home/alice/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/macros.rs:46:18: hint: while trying to match meta-variable `$n:expr`
src/main.rs:5:30: original diagnostic

But the code compiles fine, and nothing looks wrong (specifically re: the token :). Is there a known reason it's doing this?

I get a compiler error from cargo. The vec! syntax is a list of comma separated values, or two things (type and length, or fill value and length) separated by a semicolon -- this is just like the array syntax.

error: no rules expected the token `;`
  --> src/lib.rs:16:34
   |
16 |             (((0, 0), (1, 0)), 1);
   |                                  ^ no rules expected this token in macro call
   |
note: while trying to match meta-variable `$n:expr`
  --> /home/mark/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/macros.rs:46:18
   |
46 |     ($elem:expr; $n:expr) => (
   |                  ^^^^^^^

I'm not sure why you're not getting a compiler error, maybe building in the wrong directory? Or maybe you didn't try to run or build the tests?

2 Likes

Oh, duh. Makes a lot of sense - not a rust-analyzer problem after all lol.
Yes, cargo build works but cargo test doesn't. Thank you!

1 Like

You're welcome!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.