hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

Hack: Allow providing additional information to the typechecker

Open HRMsimon opened this issue 11 years ago • 5 comments

I'm writing a lot of tests in Hack at the moment, using my phpunit-hhi package to tell Hack about PHPUnit.

It would be useful to be able to get Hack to know about the semantics of some of the methods, rather than doing the assert, then adding in an invariant directly afterwards.

For example, I'm doing:

    $this->assertNotNull($foo);
    invariant($foo !== null, 'For Hack');

Would be nice if there was someway to annotate the methods so that Hack knew that if assertNotNull doesn't throw, then what's passed in is not null. Likewise for assertNull and assertInstanceOf.

HRMsimon avatar Aug 26 '15 00:08 HRMsimon

An alternative is that phpunit is popular enough we might want to special-case this.

fredemmott avatar Aug 26 '15 03:08 fredemmott

As a side note, should assertInstanceOf take a classname instead of string in your HHI?

fredemmott avatar Aug 26 '15 03:08 fredemmott

It should now that 3.9 is out. Will be doing that as part of updating the HHI to PHPUnit 4.7.

HRMsimon avatar Aug 26 '15 03:08 HRMsimon

Hack explicitly does not want any function calls to refine. Even \is_string() and friends lost that ability in 3.30. If something like you are requesting were to be introduced, it would most likely only apply for free functions. If methods were able to refine something, you'd need to know the type of the receiver (the object the method is being called on). Discovering more types would then force Hack to redo control flow analysis, which could then in turn change the type of a receiver. This would result in very difficult to debug type errors for end users, where failure to refine such a special object would result in a type error a mile away.

I agree that the repro you gave was a pointed example. The modern day equivalent would be:

use function Facebook\FBExpect\expect;
// ...
expect($some_value)->toNotBeNull();

lexidor avatar Jul 01 '21 22:07 lexidor

toNotBeNull sort-of addresses this specific case: it returns T where the input is ?T

fredemmott avatar Jul 01 '21 23:07 fredemmott