kphp icon indicating copy to clipboard operation
kphp copied to clipboard

Smart cast fails in AND-chain

Open vkaverin opened this issue 4 years ago • 3 comments

Consider following example:

<?php
main();

function main() {
  $x = null;
  if (true) {
    $x = 1;
  }

  $y = $x && foo($x);
}

function foo(int $x): bool {
  return $x > 0;
}

Code translation fails with the following:

+----------------------+
| TYPE INFERENCE ERROR |
+----------------------+
Incorrect type of the arg #0 ($x) at function: foo
Expected type:	int
Actual type:	int|null
+-------------+
| STACKTRACE: |
+-------------+
arg #0 ($x) : int|null   at function: foo
$x : int|null            at function: foo
arg #0 ($x) : int|null   at function: foo
$x : int|null            at test.php: main:10
null                     at test.php: main:5

while this snippet works just fine:

<?php

main();

function main() {
  $x = null;
  if (true) {
    $x = 1;
  }

  $y = false;
  if ($x) {
    $y = foo($x);
  }
}

function foo(int $x): bool {
  return $x > 0;
}

vkaverin avatar Dec 18 '20 15:12 vkaverin