Skip to content

A null passed to a non-nullable int parameter is coerced to 0 instead of throwing TypeError. #172

Closed

Description

@blacktrs

Summary

A null passed to a non-nullable int parameter is coerced to 0 instead of throwing TypeError.

Repro

<?php
declare(strict_types=1);

function f(int $x): int { return $x + 1; }

final class K
{
    public function has(int $id): bool { return $id > 0; }
}

$v = [null, 5][0];

try { var_dump(f($v)); } catch (\TypeError $e) { echo "TypeError fn\n"; }
try { var_dump((new K)->has($v)); } catch (\TypeError $e) { echo "TypeError method\n"; }

$g = static fn (?int $t) => f($t);
try { var_dump($g(null)); } catch (\TypeError $e) { echo "TypeError via ?int\n"; }

Expected (php)

TypeError fn
TypeError method
TypeError via ?int

Actual (manticore e40634e6, Darwin arm64)

int(1)
bool(false)
int(1)

Tracking

Known-bug repro: tests/aot/repro/wrong/null_passed_to_int_param_is_coerced.php — bash tests/aot/xfail.sh -k null_passed_to_int_param_is_coerced reports XFAIL while this is open and XPASS once it is fixed; then move the repro into tests/aot/cases/ and close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugwrong-answer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions