Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
thebanjomatic opened this issue Oct 26, 2017 · 9 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@thebanjomatic
Copy link

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.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 26, 2017

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.

@mhegazy mhegazy added the Bug A bug in TypeScript label Oct 26, 2017
@mhegazy mhegazy added this to the Future milestone Oct 26, 2017
@ajafff
Copy link
Contributor

ajafff commented Mar 18, 2018

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

@IlanFrumer
Copy link

IlanFrumer commented Feb 22, 2021

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

@mjbvz
Copy link
Contributor

mjbvz commented Dec 15, 2021

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

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Dec 15, 2021

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

RyanCavanaugh added a commit to RyanCavanaugh/TypeScript that referenced this issue Dec 15, 2021
@RyanCavanaugh
Copy link
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...

@DanielRosenwasser
Copy link
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);
}

@RyanCavanaugh
Copy link
Member

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

@DanielRosenwasser
Copy link
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
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants