-
Notifications
You must be signed in to change notification settings - Fork 3k
Allow classname<T> in instanceof test #7632
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
Allow classname<T> in instanceof test #7632
Conversation
This reverts commit e5e54bb.
@aorenste has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
The problem with this construct is that it leads to unsoundness. Consider, for example:
Now if I write |
And yet this code passes but it has the exact same problem: class List<T> {
public function add(T $x): void {}
}
function main(): void {
$y = new List();
$y->add(1);
foo($y);
}
function foo(mixed $x): void {
if ($x instanceof List) {
expectListOfString($x);
}
}
function expectListOfString(List<string> $x): void {
// ..
} |
With
set in the .hhconfig file, or by running
But I had forgotten that this was still marked as an "experimental" feature, whereas the It's probably time to turn on the "safe instanceof" feature permanently. (See 4c2a609 and related commits.) In general, we're trying to close all known strict-mode type soundness issues. |
@andrewjkennedy Awesome, I didn't know about I agree Also I think the message
should be more explanatory, like
or more simply
If you agree, I'll open a new PR to update the message. |
Thanks @jesseschalken that's a great suggestion for improving the error message. Perhaps "may itself be an instantiated generic type". The word "generic" is overloaded - I often see people talk about "the generic" when they mean a type parameter such as |
@jesseschalken if I'm understanding 603cbd7 correctly, this can be closed: while the safe behavior is still experimental, if the experimental flag isn't set, the old unsafe behavior is back - the latest build of the typechecker is now allowing https://github.com/facebook/fbshipit/blob/master/src/shipit/repo/ShipItRepo.php#L96-L98 to be removed without the experimental option |
whoops, that should be on the issue, not here |
As far as I can tell, the commit e5e54bb is in error.
If
$y
isclassname<T>
(name of a class that is compatible withT
) and$x instanceof $y
istrue
,$x
is necessarily compatible withT
. I don't see the relevance to type erasure mentioned in the error message.Fixes #7617