Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

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

Open
tcNickolas opened this issue Jun 8, 2022 · 0 comments
Labels
area: code gen (C#) C# code generation bug Something isn't working

Comments

@tcNickolas
Copy link
Member

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 tcNickolas added bug Something isn't working needs triage An initial review by a maintainer is needed labels Jun 8, 2022
@bettinaheim bettinaheim added area: code gen (C#) C# code generation and removed needs triage An initial review by a maintainer is needed labels Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: code gen (C#) C# code generation bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants