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

Nested Conditional blocks may be incorrectly lifted #768

@swernli

Description

@swernli

Describe the bug

When a conditional includes nested conditionals that modify variables defined outside of the outer conditional scope, the outer conditional may incorrectly be lifted into a generated callable by the ConditionallyControlled rewrite step. See discussion with concrete examples in comments here #767 (comment).

To Reproduce

The easiest way to reproduce this is to use ClassicallyControlledSupportTests.qs from the qsharp-runtime repo. Add the following operation to the file:

    operation MutableWithNesting() : Unit {
        mutable x = 0;
        if (x == 0) {
            if (x < 1) {
                set x = 1;
            }
        }
        Message($"{x}");
    }

And then recompile the repo. The compilation will succeed and produce generated C# files in the obj/qsharp/src folder. Investigate ClassicallyControlledSupportTests.g.cs, and observe that the outer if-statement is lifted into a callabe that is passed the x variable as a read-only argument even though the generated body performs an update of that variable. The C# will compile, but the behavior at runtime will be wrong, as the variable x will have the value zero after execution instead of the expected value of 1.

Expected behavior

Both the inner if-statement and the outer one should be recognized as not valid for lifting, so no lifted callables should be generated.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions