Skip to content

Built-in NoRewindIterator stub does not work #6387

@vudaltsov

Description

@vudaltsov
Contributor

Activity

psalm-github-bot

psalm-github-bot commented on Sep 2, 2021

@psalm-github-bot

I found these snippets:

https://psalm.dev/r/a7edd80625
<?php

/**
 * @return Generator<int, string>
 */
function generator(): Generator
{
    yield 'test';
}

$iterator = new NoRewindIterator(generator());

/** @psalm-trace $iterator */
Psalm output (using commit eb973ab):

INFO: Trace - 13:30 - $iterator: NoRewindIterator<mixed, mixed>
orklah

orklah commented on Sep 2, 2021

@orklah
Collaborator

It seems to work when we push an Iterator instead of a Generator: https://psalm.dev/r/b8a3eaff57

psalm-github-bot

psalm-github-bot commented on Sep 2, 2021

@psalm-github-bot

I found these snippets:

https://psalm.dev/r/b8a3eaff57
<?php

/**
 * @return Iterator<int, string>
 */
function generator()
{
    
}

$iterator = new NoRewindIterator(generator());

/** @psalm-trace $iterator */
Psalm output (using commit eb973ab):

ERROR: InvalidReturnType - 4:12 - No return statements were found for method generator but return type 'Iterator<int, string>' was expected

INFO: Trace - 13:30 - $iterator: NoRewindIterator<int, string>
orklah

orklah commented on Sep 2, 2021

@orklah
Collaborator

The stub declare it accepts an Iterator so Generator should be accepted: https://3v4l.org/lqr5D

weirdan

weirdan commented on Sep 2, 2021

@weirdan
Collaborator

Reproducible with user-defined classes as well: https://psalm.dev/r/36a0f142e0

psalm-github-bot

psalm-github-bot commented on Sep 2, 2021

@psalm-github-bot

I found these snippets:

https://psalm.dev/r/36a0f142e0
<?php

/**
 * @template TK
 * @template TV
 */
class C {
    /**
     * @param Iterator<TK,TV> $it
     */    
    public function __construct($it) {}
}

/** @return Generator<int, string> */
function generator(): Generator {
    yield "a";
}

$_t = new C(generator());
/** @psalm-trace $_t */;
Psalm output (using commit eb973ab):

INFO: Trace - 20:24 - $_t: C<mixed, mixed>
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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @weirdan@vudaltsov@orklah

      Issue actions

        Built-in NoRewindIterator stub does not work · Issue #6387 · vimeo/psalm