Skip to content

Use before declaration errors when using let statements within a switch case  #19503

Open

Description

TypeScript Version: 2.5.3

Code

function test(value) {
    switch (value) {
        case 1:
            let foo = 10;
            break;
        case 2:
            foo = 20; // Runtime ReferenceError: foo is not defined
            break;
        default:
    }
}

test(2);

tsc --target es2015 (or higher)

Expected behavior:
This should probably produce a compile time error: Block-scoped variable 'pen' used before its declaration. on reference to foo in case 2.

Actual behavior:

The code type-checks and compiles fine, however, when running the es2015 transpiled output, you will receive a ReferenceError error at runtime. Ideally this type of error could be caught at compile time.

Activity

  1. mhegazy commented on Oct 26, 2017

    @mhegazy
    Contributor

    The current algorithm we use to detect use before def checks locations of declarations in the source file. this works in most cases, but not in cases where control flow is involved.

  2. added this to the milestone on Oct 26, 2017
  3. ajafff commented on Mar 18, 2018

    @ajafff
    Contributor

    In case anyone has the same issue: this bug also affects classes and enums declared in a preceding case clause.

  4. IlanFrumer commented on Feb 22, 2021

    @IlanFrumer

    I've found a case that is related to this bug, but TS only catch it when strictNullChecks is true
    I get a runtime error ReferenceError: Cannot access 'message' before initialization

    code on playground

    detected when "strictNullChecks": true,

    image

    not detected when "strictNullChecks": false,

    image

    "strictNullChecks": false should only allow cases where the variable is initialized but it's value is undefined like in this example

    image

  5. mjbvz commented on Dec 15, 2021

    @mjbvz
    Contributor

    Real world example of this causing a bug (the commit is the fix): microsoft/vscode@7e266b2

    entry was declared in the first case but then incorrectly used in the second case even though fall through is impossible

  6. RyanCavanaugh commented on Dec 15, 2021

    @RyanCavanaugh
    Member

    I'm trying to think of a reasonably-efficient algorithm to detect this and not coming up with many good ideas. switch is goto 😕

    Update: Have something reasonable

  7. added a commit that references this issue on Dec 15, 2021
    9611488
  8. RyanCavanaugh commented on Dec 15, 2021

    @RyanCavanaugh
    Member

    So the only caveat here is that we'll have unavoidable false positives:

    let n = 0;
    switch (n) {
        case 0:
            let s = "ok";
        case 1:
            if (n === 0) {
                s; // ok by construction
            }
            break;
    }

    not clear what to do about that...

  9. DanielRosenwasser commented on Dec 15, 2021

    @DanielRosenwasser
    Member

    If I understand correctly, for a var-scoped variable, maybe that's a false-positive; however, s is in a temporal dead zone in case 1, and that's a runtime error in the following case.

    switch (100) {
        case 99:
          let x;
        case 100:
          x = 0;
          console.log(x);
    }
  10. RyanCavanaugh commented on Dec 15, 2021

    @RyanCavanaugh
    Member

    Daniel Rosenwasser (@DanielRosenwasser) the problem is that the code runs without error in the 0 case, but we'd flag it

  11. DanielRosenwasser commented on Dec 15, 2021

    @DanielRosenwasser
    Member

    Okay, I misread. Here's one that would've been more obvious to me.

    let sHasBeenSet = false;
    switch (n) {
        case 0:
            let s = "ok";
            sHasBeenSet = true;
        case 1:
            if (sHasBeenSet) {
                s; // ok by construction
            }
            break;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: BinderGenerally crashes in, or caused by, the binder not doing something rightEffort: DifficultGood luck.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions