Skip to content

bug: when/is blocks share a single scope instead of having independent scopes #1560

@SchoolyB

Description

@SchoolyB

Description

Each is branch inside a when block shares the same scope. Declaring a variable in one is branch prevents declaring the same variable name in another branch, which is incorrect — each branch is mutually exclusive and should have its own scope, like if/otherwise blocks do.

Repro

do classify(x int) -> (label string) {
    when x {
        is 1 {
            mut label string = "one"
            return label
        }
        is 2 {
            mut label string = "two"
            return label
        }
        default {
            mut label string = "other"
            return label
        }
    }
}

do main() {
    println(classify(1))
    println(classify(2))
    println(classify(99))
}

Expected: Compiles and prints one, two, other. Each is block has its own scope.

Actual:

error[E4003]: variable 'label' already declared in this scope (line 4)
  --> test.ez:8:13

Where to look

ezc/src/typechecker/typechecker.c — the NODE_WHEN_STMT case. Each is branch body needs scope_create()/restore around it, same as if/otherwise branches already have. The default branch needs the same treatment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtypecheckerRelated to type checking and validation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions