Hi Daniil

On Fri, Nov 3, 2023 at 12:00 AM <dan...@daniil.it> wrote:
> The much better approach, one that I intend to maybe give a shot at this 
> Christmas, is to add static analysis functionality to PHP itself (i.e. turn 
> it into a truly statically typed language).
> I have a hunch it may be easy enough to do by hooking into the type inference 
> functionality provided by opcache, and throw compile-time exceptions instead 
> of silently inserting runtime typechecks.

The optimizer, including type inference, is limited to the scope of
the current file (along with internal functions/classes). Each file is
considered a "single compilation unit". When classes from different
files reference each other, modifying one file does not require
recompiling the other. However, this does mean that we cannot rely on
information from other files as they may change at any point.
Preloading is the exception, where all preloaded files can assume not
to be changed after PHP has booted.

We can obviously not limit type checking to preloaded files. We could
make type checking a CLI step but how is that really better than
PHPStan or Psalm at that point, other than having the official PHP
stamp? PHPStan and Psalm are arguably successful *because* they are
written in PHP, making them much easier to maintain and contribute to.

I'd also like to add that tools like PHPStan and Psalm have much more
accurate type representations. PHP does not accurately represent
arrays in the optimizer and has no notion of array shapes. The
optimizers types are biased towards speed rather than accuracy.

Another issue, specifically pertaining to generics, is that PHP has
type coercion. In both weak and strict typing mode, a float function
parameter will coerce an integer value. However, if generic types are
erased at runtime then the VM cannot do coercion for foo<float>($int)
(where function foo<T>(T $var)). This will require either accepting
inaccurate runtime types, or establishing stricter static rules that
do not match the existing behavior.

Ilija

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

Reply via email to