Skip to content

CFA: control flow for switch is defeated by outer if-else #11572

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

Closed
ghost opened this issue Oct 12, 2016 · 1 comment · Fixed by #32695
Closed

CFA: control flow for switch is defeated by outer if-else #11572

ghost opened this issue Oct 12, 2016 · 1 comment · Fixed by #32695
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@ghost
Copy link

ghost commented Oct 12, 2016

TypeScript Version: nightly (2.1.0-dev.20161012)

Code

enum E { A, B }

function f(e: E): number {
    switch (e) {
        case E.A: return 0
        case E.B: return 1
    }
}

function g(e: E): number {
    if (!true)
        return -1
    else
        switch (e) {
            case E.A: return 0
            case E.B: return 1
        }
}

Expected behavior:

Both functions succeed.

Actual behavior:

a.ts(10,19): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.

If I remove the else keyword (which makes an equivalent program), it compiles without errors.

If I put an e access under the switch, I see (parameter) e: never on hover, so that part is working.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Oct 12, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 14, 2016

This is working as intended. the two checks, all-code-paths-result-in-explicit-return and exhaustive-switch-with-respect-to-type happen in two different phases, once in the binder and the other in the checker. we special case one, and only one case where the switch statement is the last statement in the function body and skip the error if it is exhaustive, see functionHasImplicitReturn.

@mhegazy mhegazy added Design Limitation Constraints of the existing architecture prevent this from being fixed and removed Bug A bug in TypeScript labels Dec 14, 2016
@ghost ghost closed this as completed Dec 14, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants