On Wednesday, March 11 2015 at 1:14 pm Zeev Suraski wrote:

> I don't believe strict STH would bring any meaningful gains for static
> analysis, no. Unlike the JIT/AOT advantages (on the same code) which I
> believe I've proven cannot be gained, I can't prove this one - because you
> can infer slightly different data from strict type hints. But again, in
> edge cases.

The benefits of strict typing for static analysis are hardly relegated to
edge cases. Here's a simple example:

function getUserPermissions(int $userId): array
function getUsername(int $userId): string

$userId = 10;
$user = getUsername($userId);
$permissions = getUserPermissions($user);

In strict mode static analysis can tell you that this will fail, since a
string is being passed to a function expecting an int. With coercive STH,
all you know is that it may or may not work.

>> Strict types can absolutely help - not only for static analysis, but also
>> for potential future AOT/JIT development. Why? Because only types need to
>> be checked, rather than values.
> 
> I'm sorry, but that is simply wrong. Why? Because types ARE in fact a
> part of the value in PHP. They're an element of PHP's value structure
> (zval). You do NOT know the value with certainty until runtime, because
> again, the type IS in fact a part of the value. The very same type
> inference you can do with strict types you can do with coercive or even
> with weak types. Again, this has been discussed in length in the threads
> that took place several weeks ago. I believe even Anthony concluded that
> for a given piece of code - assuming it's unchanged - there are no AOT/JIT
> advantages. The theory is that there can be stronger static analysis,
> which will provide more insightful suggestions to the developer to change
> their code which will in turn result in code that can be JIT'd or AOT'd
> better. Here too, we don't believe that's the case and believe you can
> provide the same value with coercive type hints.

Of course you can do the same type inference. The difference is that with
coercive STH knowing the type isn't enough - you also have to know the
value. A string passed to a function with an int type declaration MAY
succeed or MAY fail. You won't know until the value is checked at runtime.
However, in strict mode a user would be required to pass a value of the
correct type. There are many cases where PHP can check the types at compile
time, but not the values. Therefore, with strict STH it is possible to
eliminate many runtime value checks and type conversions. For example:

function getFooByVal(string $val): string
function getBarByFoo(int $foo)
getBarByFoo(getFooByVal("value"));

With coercive STH a runtime value check is necessary. With strict types, a
failure could be detected at compile time, the user would fix their code,
and at runtime no value check is necessary. This is a huge advantage for
AOT/JIT.

Theodore Brown
                                          
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to