Skip to content

iterable<T> seems to be declared incorrectly #8880

@SirNickolas

Description

@SirNickolas

Bug report

I came to a conclusion that iterable<T> in PHPStan is declared to be contravariant in its parameter. This is wrong; it should be covariant in it instead.

Code snippet that reproduces the problem

https://phpstan.org/r/112368bd-0528-4d62-b3de-97e068c0e005

Expected output

This code fails at runtime (we attempt to call putStrLn with an int) but produces no errors during type-checking. The reason is that IProcessor must be contravariant in its parameter T, but PHPStan believes it is covariant in it. It seems that builtin type iterable<T> is mistakingly declared in PHPStan to be contravariant in T. This is wrong: its method current() returns T, and only covariant types can occur in that position. If you change iterable to \Iterator, \Generator, or array in the code, you’ll get a type error, which is the correct behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions