Relax "This requires the late-bound type to be exactly [classname]" when class is variant or methods are final
- Dominant language
- C++
- Stars
- 18.7k
- Forks
- 3.1k
- Avg merge
- 1h 47m
- Merged PRs (30d)
- 2
Description
### HHVM Version
```
$ hhvm --version
HipHop VM 3.16.0-dev (rel)
Compiler: heads/master-0-g07c7a02cd850b51c85ab64692298a91eb1c3e0c5
Repo schema: a0d9f666dd3c0dc8a940e03788cabf7bed52809d
```
### Standalone code, or other way to reproduce the problem
This requests relates to [a StackOverflow question](http://stackoverflow.com/questions/41310649/hacklang-does-this-type-error-involving-this-suggest-that-the-underlying-obj) asking about the rationale behind the "This requires the late-bound type to be exactly [classname]" error. I think I've identified the fundamental violation it is meant to prevent, and I've repeated it below:
```php
act($this);
}
}
class B extends A {
public ?int $b_prop;
<<__Override>>
public function act(this $v): void {
$v->b_prop;
}
}
function initiate(): void {
violate(new B());
}
function violate(A $v): void {
(new A())->act($v);
}
```
Without the error, `B::act` would try to access `$b_prop` from an `A`. This violation can be prevented, however, just by asserting _all methods with `this` arguments_ are final, instead of requiring the class to be final. Tweaking the previous snippet for example:
```php
act($this);
}
}
class B extends A {
public ?int $b_prop;
}
function initiate(): void {
violate(new B());
}
function violate(A $v): void {
(new A())->act($v);
}
```
### Expected result
```
No errors!
```
### Actual result
```
This is an object of type A (known to be exactly the class 'A') [...]
It is incompatible with an object of type A
[line 14] requires the late-bound type to be exactly A
Since A is not final this might be an instance of a child class
```
I'm using argument `this` to build and extend tree structures, but putting a hard `final` on the first mutable class is cutting into the flexibility of the methods working with them. I appreciate this is probably a pretty low-pri and difficult enhancement, but I thought I would make the suggestion, and maybe learn if another violation is lurking under even these proposed rules?
Contributor guide
Research direction
The issue names no HHVM source file or test entry point. Start by reproducing the two Hack snippets with HHVM 3.16.0-dev and tracing the late-bound-type diagnostic; done means the final-method or variant-class cases pass without errors while the demonstrated override violation remains rejected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100