Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,11 @@ protected function getOpCodeTypeFromUnaryOp(Op\Expr $expr): int {
}

protected function compileExpr(Op\Expr $expr, Block $block): array {
if ($expr instanceof Op\Expr\BinaryOp\Coalesce) {
$this->compileCoalesce($expr, $block);

return [];
}
if ($expr instanceof Op\Expr\BinaryOp) {
$opcode = new OpCode(
$this->getOpCodeTypeFromBinaryOp($expr),
Expand Down
15 changes: 15 additions & 0 deletions patches/php-types-generic-null-tail.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git vendor/ircmaxell/php-types/lib/PHPTypes/Type.php vendor/ircmaxell/php-types/lib/PHPTypes/Type.php
--- vendor/ircmaxell/php-types/lib/PHPTypes/Type.php
+++ vendor/ircmaxell/php-types/lib/PHPTypes/Type.php
@@ -372,6 +372,11 @@
}
$decl = self::stripTrailingDocText($decl);
$trimmedDecl = trim($decl);
+ // list<T|null> union splits may pass a trailing "null>" fragment (#2276).
+ if (str_ends_with($trimmedDecl, '>') && !str_contains($trimmedDecl, '<')) {
+ $trimmedDecl = rtrim(substr($trimmedDecl, 0, -1));
+ $decl = $trimmedDecl;
+ }
if ('' === $trimmedDecl || '*' === $trimmedDecl || '*/' === $trimmedDecl
|| str_starts_with($trimmedDecl, '*/')) {
return self::mixed();
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ public static function fromDecl($decl): self
}
$decl = self::stripTrailingDocText($decl);
$trimmedDecl = trim($decl);
// list<T|null> union splits may pass a trailing "null>" fragment (#2276).
if (str_ends_with($trimmedDecl, '>') && !str_contains($trimmedDecl, '<')) {
$trimmedDecl = rtrim(substr($trimmedDecl, 0, -1));
$decl = $trimmedDecl;
}
if ('' === $trimmedDecl || '*' === $trimmedDecl || '*/' === $trimmedDecl
|| str_starts_with($trimmedDecl, '*/')) {
return self::mixed();
Expand Down
4 changes: 4 additions & 0 deletions script/apply-patches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ patch_already_applied() {
php-types-generics-list-array.patch)
grep -qF "preg_match('/^(list|array)" "$ROOT/vendor/ircmaxell/php-types/lib/PHPTypes/Type.php" 2>/dev/null
;;
php-types-generic-null-tail.patch)
grep -q 'list<T|null> union splits' "$ROOT/vendor/ircmaxell/php-types/lib/PHPTypes/Type.php" 2>/dev/null
;;
php-types-docblock-trailing-text.patch)
grep -q "stripTrailingDocText" "$ROOT/vendor/ircmaxell/php-types/lib/PHPTypes/Type.php" 2>/dev/null
;;
Expand Down Expand Up @@ -1922,6 +1925,7 @@ if [[ -d "$ROOT/vendor/ircmaxell/php-types" ]]; then
apply_patch "$PATCH_DIR/php-types-array-shape.patch"
apply_patch "$PATCH_DIR/php-types-generics-fallback.patch"
apply_patch "$PATCH_DIR/php-types-generics-list-array.patch"
apply_patch "$PATCH_DIR/php-types-generic-null-tail.patch"
apply_patch "$PATCH_DIR/php-types-docblock-trailing-text.patch"
apply_patch "$PATCH_DIR/php-types-fromdecl-junk-fragments.patch"
apply_patch "$PATCH_DIR/php-types-ns-func-call.patch"
Expand Down
12 changes: 12 additions & 0 deletions test/unit/CoalesceVmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ public function testNullCoalesceAssignEchoInline(): void
}

/** Issue #3462: ($x ?? throw new Ex()) — RHS only when LHS is null. */
public function testNullCoalesceNestedInCast(): void
{
$this->assertVmOutput(
'<?php
$stat = ["mode" => 0644];
echo (int) ($stat["mode"] ?? 0), "\n";
echo (int) ($stat["missing"] ?? 0), "\n";
',
"420\n0\n"
);
}

public function testNullCoalesceThrow(): void
{
$this->assertVmOutput(
Expand Down
8 changes: 8 additions & 0 deletions test/unit/PhpcLintBootstrapInventoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function testBootstrapInventoryFixturePathsLintClean(): void
}
}

/** Issue #2276 — nested ?? in casts must not report Expr_BinaryOp_Coalesce. */
public function testBootstrapSpineVmFsLintClean(): void
{
$repoRoot = dirname(__DIR__, 2);
$exit = $this->runLint([$repoRoot.'/ext/standard/VmFs.php']);
$this->assertSame(0, $exit['code'], $exit['stderr']."\n".$exit['stdout']);
}

public function testPhpcDelegatesBootstrapInventory(): void
{
$repoRoot = dirname(__DIR__, 2);
Expand Down