Surprising item shadowing


#1

Today I found that the following program prints 92

fn main() {
    let x: i32 = 92;
    {
        const x: i32  = 42;
        println!("{}", x);
    }
}

This happens because const is an item and the reference says “it is otherwise identical in meaning to declaring the item outside the statement block.”.

Is this behavior intentional?


#2

Collisions are rare because there are different naming rules: snake case for vars and upper case for globals. It looks you are the first who found it. Compiler gives warning, but semantics looks really fickle :fearful:
Maybe it’s a reason for issue.


#3

There’s a reason why the compiler warns on using the wrong naming convention.

Similarly,

let x = None;
match x {
 Nonee => (),
 _ => ()
}

will compile, even though Option doesn’t have a Nonee variant, because in this case Nonee will be a variable binding. However we will get unused variable and variable naming warnings which let us know this happened.


#4

And today (rustc 1.8.0) it prints 42! (an issue)