facebook / facebook/hhvm

[ Typechecker | Bug ] Constructor calls allowed on unconstrained \HH\classname<T> (unsound)

Open
#8,720 0 comments 0 reactions 0 assignees View on GitHub
hack
Dominant language
C++
Stars
18.7k
Forks
3.1k
Avg merge
1h 47m
Merged PRs (30d)
2

Description

**Describe the bug**

Calling a constructor of `\HH\classname` is allowed when you don't restrain `T`.
The constructor signature is unknown and this call is therefore unsound.

**Standalone code, or other way to reproduce the problem**

```HACK
function call_constructor_wo_arguments(
classname $may_have_required_args,
): T {
return new $may_have_required_args();
}
```

Steps to reproduce the behavior:
1. Create a class with required arguments in the constructor.
2. Create the following callsite, where DateInterval is your class.
```call_constructor_wo_arguments(DateInterval::class);```
3. Observe no typechecker error on the declaration of call_constructor_wo_arguments.
4. Invoke the function and observe the `\RuntimeException`: `DateInterval::__construct() expects exactly 1 parameter, 0 given`.

**Expected behavior**

Declaring `call_constructor_wo_arguments(): T` should be an error, because `T` is essentially `mixed`.

**Actual behavior**

`No errors!`

**Environment**
- Operating system: Ubuntu 20.04
- Installation method: dl.hhvm.com with apt
- HHVM Version:
```
HipHop VM 4.56.0 (rel)
Compiler: 1588614387_226336535
Repo schema: d1ae8e21bf3419a65f12a010527485564e719d07
hackc-0b10dd000fc9b454637d8dffc67fb542d231572c-4.56.0
```

**Additional context**
I tried making a factory function for a class with `<<__ConsistentConstruct>>`, but forgot to restrain `T` to that class hierarchy. Was surprised that it compiled.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.