Heya Anthony,

> -----Ursprüngliche Nachricht-----
> Von: Anthony Ferrara [mailto:ircmax...@gmail.com]
> Gesendet: Montag, 23. Februar 2015 14:53
> An: Robert Stoll
> Cc: PHP internals
> Betreff: Re: [PHP-DEV] JIT (was RE: [PHP-DEV] Coercive Scalar Type Hints RFC)
> 
> Robert,
> 
> On Mon, Feb 23, 2015 at 3:27 AM, Robert Stoll <p...@tutteli.ch> wrote:
> > Hey all,
> >
> > tl;dr
> >
> > Just one point which JIT/AOT people should consider when dealing with PHP. 
> > PHP is highly dynamic and there are enough
> use cases which makes it impossible for a static analyser to infer types 
> accurately without using a top type like mixed.
> > How would you deal with variable function calls, variable variables, 
> > reflection, dynamic includes etc.
> >
> > Your inferred types would simply be wrong without using mixed.
> > Consider the following
> >
> > function foo(int $a){}
> > $a = 1;  //can be int for sure right?
> > $b = "a";
> > $$b = "h"; //oh no, your generated code would crash foo($a);
> >
> > Maybe I am wrong and there is a possibility, if so, please let me know, 
> > would be interesting to know.
> 
> This very specific example is easy to type. The reason is that we can use 
> constant propagation to know that $$b is really $a
> at compile time. Hence we can reduce it to:
> 
> $a = "h";
> foo($a);

[Robert Stoll] 
Sure, "a" was just an example to illustrate the problem. I figured it would not 
be necessary to say that the value of $b can be completely unknown by the 
static analyser -> could come from user input, from a database, from 
unserialising code etc. (but probably that is what you meant with "this isn't 
the general case" below).

Assuming statically that $a is int or $b is string is erroneous in this context.

Another problem to illustrate that a top type or at least some form of union 
type is required:

function foo($x, $y, $z){
  $a = 1;
  if($x){
    $a = "1";
  }
  If($y > 10){
    $a = [];
  }
  If($z->foo() < 100){
    $a = new Exception();
  }
  echo $a;
  return $a;
}

How do you want to type $a without using a union type?

> 
> And hence know **at compile time** that's an error.
> 
> This isn't the general case, but we can error in that case (from a static 
> analysis perspective at least) and say "this code is too
> dynamic". In strict mode at least.

[Robert Stoll] 
If you go and implement a more conservative type system than the actual dynamic 
type system of PHP well then... you can do whatever you want of course.
But if you do not just want to support just a limited set of PHP then you will 
need to include dynamic checks in many places. Or do you think that is not true?


> Anthony





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

Reply via email to