Zeev, > I think we are indeed getting somewhere, I hope. > If I understand correctly, effectively the flow you're talking about in your > example is this: > > 1. The developers tries to run the program. > 2. It fails because the static analyzer detects float being fed to an int. > 3. The user changes the code to convert the input to int. > 4. You can now optimize the whole flow better, since you know for a fact > it's an int. > > Did I describe that correctly?
Partially. The static analysis and compilation would be pure AOT. So the errors would be told to the user when they try to analyze the program, not run it. Similar to HHVM's hh_client. However, there could be a "runtime compiler" which compiles in PHP's compile flow (leveraging opcache, etc). In that case, if the type assertion isn't stable, the function wouldn't be compiled (the external analyzer would error, here it just doesn't compile). Then the code would be run under the Zend engine (and error when called). >> With strict typing at the foo() call site, it tells you that $input has to >> be an int >> or float (respectively between the snippets). > > I'm not following. > Are you saying that because foo() expects an int or float respectively, > $input has to be int or float? What if $input is really a string? Or a > MySQL connection? So think of it as a graph. When you start the type analysis, there's one edge between $input and foo() with type mixed. Looking at foo's argument, you can say that the type of that graph edge must be int. Therefore it becomes an int. Then, when you look at $input, you see that it can be other things, and therefore there are unstable states which can error at runtime. > Or are you saying that there was a strict type hint in the function that > contains the call to foo(), so we know it's an int/float respectively? If > so, how would it be any different with a coercive type hint? Not all data gets into a function from a parameter: function bar() { $x = $_POST['data']; foo($x); } in that case, we know $x can only be a string or an array (unless we find where that variable was written to in the program). So we know for a fact that there's a type error, even though it wasn't a parameter. Going deeper, we can look at other cases: function x() { if (time() % 360 > 0) { return 123; } } function bar() { $x = x(); foo($x); } In this case, we know that x() has two possible types: int/null. That doesn't satisfy the valid possibilities for foo (int), hence there's a possible type error. The key difference is this: Forward analysis (typing $x by assignment) can tell you valid modes for your program. Backward analysis (determining $x's type by its usages) can tell you invalid modes for your program. Combining them gives you more flexibility in hard-to-infer/reconstruct situations. Anthony -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php