Skip to content

const_prop_lint can't tell when code guarded with && is dead #113905

Open
@SvenKeimpema

Description

@SvenKeimpema

I tried this code:

macro_rules! set_bit {
    ($bb:expr, $sq:expr) => {
        {
            let mut bb = $bb as u64;
            let sq = $sq as i32;
            if sq >= 0 && sq < 64 {
                bb |= (1u64 << $sq);
            }
            bb
        }
    };
}

I expected to see this happen: i expected the code to run if i simply passed sq=64 to the macro as an parameter, however it threw an:

error: this arithmetic operation will overflow
  --> main.rs:10:23
   |
10 |                 bb |= (1u64 << $sq);
   |                       ^^^^^^^^^^^^^ attempt to shift left by `64_i32`, which would overflow
...
63 |     println!("{}", set_bit!(0, 64));
   |                    --------------- in this macro invocation
   |
   = note: `#[deny(arithmetic_overflow)]` on by default
   = note: this error originates in the macro `set_bit` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error; 4 warnings emitted

which is pretty strange since it will never be able to actually perform the bitwise if sq >= 64.

It's also to note if i change the bitwise operation to bb |= (1u64 << sq); (removing the dollar sign) it won't throw this error.
I thinks this is due to an oversight where if the sq operator is copied to an different variable it won't release a if-statement is done to prevent a arithmetic_overflow.

Meta

rustc --version --verbose:

rustc 1.66.1 (90743e729 2023-01-10) (built from a source tarball)
binary: rustc
commit-hash: 90743e7298aca107ddaa0c202a4d3604e29bfeb6
commit-date: 2023-01-10
host: x86_64-unknown-linux-gnu
release: 1.66.1
LLVM version: 15.0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-propArea: Constant propagationA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions