Skip to content

Rust Analyzer detects a syntax error when the code compiles and runs just fine #10784

@itsKhalidHossain

Description

@itsKhalidHossain

Here is the sample code:

fn collatz(n: u64) -> Option<u64> {
    const OVERFLOW_BARRIER: u64 = u64::MAX / 3;
    let mut n = n;
    for i in 0.. {
        n = match (n, n % 2) {
            (0, _) => break,
            (1, _) => return Some(i),
            (OVERFLOW_BARRIER.., 1) => 0, // <- Errors out here.
            (_, 0) => n / 2,
            _ => n * 3 + 1,
        }
    }
    None
}

Changing OVERFLOW_BARRIER.. to OVERFLOW_BARRIER..=u64::MAX seems to work.

But changing placement of match tuple works just fine:

pub fn collatz(n: u64) -> Option<u64> {
    const OVERFLOW_BARRIER: u64 = u64::MAX / 3;
    let mut n = n;
    for i in 0.. {
        n = match (n % 2, n) {
            (_, 0) => break,
            (_, 1) => return Some(i),
            (1, OVERFLOW_BARRIER..) => 0, // Detects no error.
            (0, _) => n / 2,
            _ => n * 3 + 1,
        }
    }
    None
}

But Rust compiler doesn't detect any error. It compiles just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserparser issuesC-bugCategory: bugS-actionableSomeone could pick this issue up and work on it right now

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions