vimeo/psalm

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

Open

#7,235 opened on 2021年12月28日

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
 (平均マージ 3d 12h) (30d で 5 merged PRs)

説明

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.

コントリビューターガイド