qsharp-compiler icon indicating copy to clipboard operation
qsharp-compiler copied to clipboard

Unexpected behavior when defining variables with the same name in enclosing scopes

Open tcNickolas opened this issue 3 years ago • 0 comments

Describe the bug

I'm observing several odd behaviors, I'm not sure which of them are bugs and which are expected, so I'll report them all at once without unentangling them.

  1. The following code throws an error CS0136 A local or parameter named 'angle' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter, even though the angle variable in the outer scope is defined after the loop, so no shadowing occurs.
    operation Tmp() : Unit {
        for i in 0 .. 1 {
            let angle = i * 2;
        }
        let angle = 0;
    }

I'm also not sure how to interpret a local or parameter phrase - is it something like "a local variable or parameter"?

  1. With an extra conjugation in place, the same code passes compilation, though if the variable angle in the loop was shadowing the variable outside of it, it is still doing it with an extra block inside.
    operation Tmp() : Unit {
        for i in 0 .. 1 {
             within {
                let angle = i * 2;
             } apply {
                let a = 1;
             }
        }
        let angle = 0;
    }

Additionally, it gives a warning The variable '__qsVar1__a__' is assigned but its value is never used, which looks like something from QIR, since the user-facing variable name is a. The unused variable warning for angle looks as expected.

System information

  • QDK 0.24.210930

tcNickolas avatar Jun 08 '22 23:06 tcNickolas