vimeo/psalm

Psalm infers float|int when using template for positive-int|0

Open

#7,235 opened on Dec 28, 2021

View on GitHub
 (3 comments) (0 reactions) (0 assignees)PHP (668 forks)batch import
Help wantedbuggood first issuetemplates

Repository metrics

Stars
 (5,369 stars)
PR merge metrics
 (Avg merge 3d 12h) (5 merged PRs in 30d)

Description

Given the following snippet https://psalm.dev/r/28b89bee3b

<?php

/**
 * @psalm-pure
 * @psalm-template T as positive-int|0
 * @psalm-param T $depth
 */
function recursiveFn(int $depth = 1): void {
    if ($depth > 1) {
        recursiveFn($depth - 1);
    }
}

Psalm reports:

ERROR: ArgumentTypeCoercion - 10:21 - Argument 1 of recursiveFn expects 0|positive-int, parent type float|int provided

Given the exactly the same snippet, except not using the template https://psalm.dev/r/412e21cd88

<?php

/**
 * @psalm-pure
 * @psalm-param positive-int|0 $depth
 */
function recursiveFn(int $depth = 1): void {
    if ($depth > 1) {
        recursiveFn($depth - 1);
    }
}

There are no (related) errors; presumably $depth is correctly identified as positive-int|0 here.

Contributor guide