Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83519 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 10136 invoked from network); 23 Feb 2015 01:02:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Feb 2015 01:02:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.171 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.217.171 mail-lb0-f171.google.com Received: from [209.85.217.171] ([209.85.217.171:42070] helo=mail-lb0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2C/22-33016-71C7AE45 for ; Sun, 22 Feb 2015 20:02:16 -0500 Received: by lbiw7 with SMTP id w7so15675338lbi.9 for ; Sun, 22 Feb 2015 17:02:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=BcQOaOKV90rg3phKr5U8Gg5UOiHy9fGjRFOQWuF30Vw=; b=Szns1hmeea99Z+eD17PvuCknqZ603gdjRK+sIcSWk4hNeFkKy4GJO9RkZUBC1i/x8X 5X+Yw42EWPMvaxPRFIxsWbbZoA73Hu2+IVgjtkPBuKPMb1wkWRPS24jd9N7tj7HtttjM wY4py5Jd9YNUBPemYVtp+RCXk7KIczQ/gWEiRw4geH7mkda0tKvEPfrGSWtG0/9aHuyB vgGHeJm6og1K38geFLsqiwuUCZfz/XKYGWnGDdNfPm7VAy57EuY1f2q88iXEfqRRQfCO oxlVFR/wKfqzPPdBpvgRb61ckvkFga3G7rE4rh5w7RWJ1r/n8qWRnp76wItkLM6gTCF+ zcxQ== MIME-Version: 1.0 X-Received: by 10.112.136.201 with SMTP id qc9mr7667212lbb.57.1424653330907; Sun, 22 Feb 2015 17:02:10 -0800 (PST) Received: by 10.25.43.9 with HTTP; Sun, 22 Feb 2015 17:02:10 -0800 (PST) In-Reply-To: <6d317b6cce0cc1aaded1dae11218234d@mail.gmail.com> References: <2e4694f9805ee81ea0b2c79eab06c2d6@mail.gmail.com> <83921f861c3378dfc6ea34b6681f2edd@mail.gmail.com> <26a5bb62bd37a3f610b2a6c10f84d855@mail.gmail.com> <6d317b6cce0cc1aaded1dae11218234d@mail.gmail.com> Date: Sun, 22 Feb 2015 20:02:10 -0500 Message-ID: To: Zeev Suraski Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] JIT (was RE: [PHP-DEV] Coercive Scalar Type Hints RFC) From: ircmaxell@gmail.com (Anthony Ferrara) 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