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

[ Typechecker ] Try blocks with a switch in them incorrectly infers no exceptions could be thrown #8764

Closed
lexidor opened this issue Nov 2, 2020 · 1 comment

Comments

@lexidor
Copy link
Collaborator

lexidor commented Nov 2, 2020

Describe the bug
Hack will type exceptions an nothing for try {} blocks if there is no way for them to throw. HHVM now throws exceptions on non exhaustive switches. Hack is not aware of this fact and will therefore assume the catch block is unreachable, when it can be reached.

Standalone code, or other way to reproduce the problem

<<__EntryPoint>>
function main(): void {
    try {
        switch(0) {
            case 1:
        }
    } catch (\RuntimeException $e) {
        takes_nothing($e);
    }
}

function takes_nothing(nothing $nothing): void {}

Steps to reproduce the behavior:

  1. Runtime hh_client (No errors!)
  2. Invoke script (\TypeError thrown, since \RuntimeException is not a subtype of nothing.)

Expected behavior
The type of $e should be \RuntimeException for the typechecker.

Actual behavior

hh_client 
No errors!

===

Fatal error: Uncaught TypeError:
Argument 1 passed to takes_nothing() must be an instance of nothing,
RuntimeException given in /path/to/main.hack:13

Stack trace:
#0 /path/to/main.hack(8): takes_nothing()
#1 (): main()
#2 {main}

Environment

  • Operating system

Ubuntu 20.04

  • Installation method

apt-get with dl.hhvm.com repository

  • HHVM Version
hackc-f96b1ec9eaa436d00ddd39e08e2231478d47e50e-4.80.0
HipHop VM 4.80.0 (rel)
Compiler: 1603141480_003241999
Repo schema: d1ae8e21bf3419a65f12a010527485564e719d07
@lexidor
Copy link
Collaborator Author

lexidor commented May 16, 2022

I am going over old issues on this repository, to see which ones apply to the current versions of hhvm.

Hack now correctly infers the the switch may throw, so $e is not nothing anymore.

Typing[4110] Invalid argument [1]
-> Expected nothing [2]
-> But got RuntimeException [3]

src/file.hack:8:19
     5 |       case 1:
     6 |     }
[3]  7 |   } catch (\RuntimeException $e) {
[1]  8 |     takes_nothing($e);
     9 |   }
    10 | }
    11 | 
[2] 12 | function takes_nothing(nothing $nothing): void {}

1 error found.

@lexidor lexidor closed this as completed May 16, 2022
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

1 participant