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

Inconsistent unreachable code detection #49950

Closed
leetwinski opened this issue Jul 19, 2022 · 5 comments
Closed

Inconsistent unreachable code detection #49950

leetwinski opened this issue Jul 19, 2022 · 5 comments

Comments

@leetwinski
Copy link

Bug Report

Unreachable code detection seems to be inconsistent in case of assigning the never returning function call result to a var.
In this case the unreachable code error isn't signaled, while it gets signaled then there's no such assignment.

🔎 Search Terms

unreachable code

🕗 Version & Regression Information

4.7.4

⏯ Playground Link

Playground link with relevant code

💻 Code

function f(): never {
    f()
}


function f1(x: number): number {
    f();
    return x;  // Unreachable code error
}

function f2(x: number): number {
    const zee = f();
    return x;  // Successful compilation
}

🙁 Actual behavior

the last line of f2 function doesn't fire an unreachable code compilation error

🙂 Expected behavior

the same as that of f1

@fatcerberus
Copy link

This is a known limitation. See #32695; in particular:

A function call is analyzed as an assertion call or never-returning call when

  • the call occurs as a top-level expression statement, and
  • the call specifies a single identifier or a dotted sequence of identifiers for the function name, and
  • each identifier in the function name references an entity with an explicit type, and
  • the function name resolves to a function type with an asserts return type or an explicit never return type annotation.

@leetwinski
Copy link
Author

@fatcerberus, thank you for your answer!
Wouldn't it be nice to have at least warning about never on the right side in assignment?

@fatcerberus
Copy link

never is intentionally assignable to every other type - somewhat nerdy theoretical explanation here:
#49862 (comment)

Also see extensive discussion about assignability from never in #28982.

@leetwinski
Copy link
Author

leetwinski commented Jul 19, 2022

#49862 (comment)

Wow that makes it clear from the set theory's point of view. Nevertheless it is still rather confusing for the working programmer )
Well, i guess i'll have to live with that
Once again, thanks for the explanation!

@fatcerberus
Copy link

@leetwinski Also, from a type theoretical point of view, a function that normally returns a number but might throw could be seen as returning number | never. Generally you want the result of this function to be assignable to number, and if never is assignable to everything, the union can be treated as just number, making it so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants