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

Switch statement starts generating "Not all code paths return a value" when wrapped in try/finally #19423

Closed
mattmccutchen opened this issue Oct 23, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@mattmccutchen
Copy link
Contributor

I was confused when my function suddenly started to generate a "Not all code paths return a value" error when I put a try/finally statement around an enum switch statement. After reading #17358 (comment) and #9163 (comment), I understand it's a limitation of the current implementation that the exhaustiveness check only works for a switch statement that's the last thing in a function. This information wasn't trivial for me to find, so I'm filing an issue that the next person will hopefully be able to find more quickly, as well as to provide feedback that I found the inconsistency confusing.

In my opinion, ideally the exhaustiveness check should apply whenever the switch statement has a control-flow successor that has another antecedent. The important thing is that it should not apply when the switch statement is immediately followed by an unconditional statement, suggesting developer intent that the switch statement not be considered exhaustive. But this can be a naive check just based on the kind of the immediately enclosing control structure.

TypeScript Version: reproduced with master as of this writing (ceba507)

Code

// Compile with --noImplicitReturns

enum Test {
  ONE = 1,
  TWO = 2
}
function mySwitch(t: Test) {
  switch (t) {
    case Test.ONE:
      return 42;
    case Test.TWO:
      return 42;
  }
}
function mySwitchWrapped(t: Test, b: boolean) {
  try {
    switch (t) {
      case Test.ONE:
        return 42;
      case Test.TWO:
        return 42;
    }
  } finally {}
}

Expected behavior:
Consistent exhaustiveness checking for mySwitchWrapped and mySwitch.

Actual behavior:
"error TS7030: Not all code paths return a value." on mySwitchWrapped, but not on mySwitch.

@ghost
Copy link

ghost commented Oct 23, 2017

Looks like a duplicate of #11572

@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 23, 2017
@mattmccutchen
Copy link
Contributor Author

I guess I didn't try enough different search keywords. Anyway, this issue has more of them.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants