[ TypeChecker ] Enable `disallow_discarded_nullable_awaitables` by default
- Dominant language
- C++
- Stars
- 18.7k
- Forks
- 3.1k
- Avg merge
- 1h 47m
- Merged PRs (30d)
- 2
Description
**Describe the bug**
When you drop an Awaitable, the typechecker informs you that this is unsafe.
_This expression is of type Awaitable, but it's either being discarded or used in a dangerous way before being awaited_
However, the same error is not generated for a nullable awaitable.
**Standalone code, or other way to reproduce the problem**
```HACK
<<__EntryPoint>>
async function main_async(): Awaitable {
// drop awaitables
awaitable_async();
nullable_awaitable_async();
}
async function awaitable_async(): Awaitable {
return 0;
}
function nullable_awaitable_async(): ?Awaitable {
return awaitable_async();
}
```
Steps to reproduce the behavior:
1. Invoke typechecker
**Expected behavior**
```
Typing[4015] This expression is of type Awaitable, but it's either being discarded or used in a dangerous way before being awaited
--> dropping_awaitables.hack
4 | awaitable_async();
| ^^^^^^^^^^^^^^^^^
8 | async function awaitable_async(): Awaitable {
| ^^^^^^^^^^^^^^ This is why I think it is Awaitable
Typing[4015] This expression is of type ?Awaitable, but it's either being discarded or used in a dangerous way before being awaited
--> dropping_awaitables.hack
5 | nullable_awaitable_async();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12 | async function nullable_awaitable_async(): ?Awaitable {
| ^^^^^^^^^^^^^^^ This is why I think it is Awaitable
2 errors found.
```
**Actual behavior**
```
Typing[4015] This expression is of type Awaitable, but it's either being discarded or used in a dangerous way before being awaited
--> dropping_awaitables.hack
4 | awaitable_async();
| ^^^^^^^^^^^^^^^^^
8 | async function awaitable_async(): Awaitable {
| ^^^^^^^^^^^^^^ This is why I think it is Awaitable
1 error found.
```
**Environment**
- Operating system: Ubuntu 20.04
- Installation method: dl.hhvm.com
- HHVM Version 4.56.0
```
HipHop VM 4.56.0 (rel)
Compiler: 1588613660_104441370
Repo schema: d1ae8e21bf3419a65f12a010527485564e719d07
hackc-0b10dd000fc9b454637d8dffc67fb542d231572c-4.56.0
```
**Additional context**
It is relatively uncommon to explicitly return a nullable Awaitable, however, these are often produced in method chains that include the `?->` operator.
```HACK
$maybe_null = \rand() ? new SomeClass() : null;
$maybe_null?->somethingAsync(); // This is not a typeerror, but it could be.
```
Contributor guide
Assessment
This issue has not been assessed yet.