Skip to content

Conversation

@ruudk
Copy link
Contributor

@ruudk ruudk commented Aug 3, 2023

Q A
Branch? 6.3
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

The LazyClosure was introduced in #49639 and generates PHP code using reflection.

It works, but when the callable has a void return type, it would produce code like this:

return $container->services['closure2'] = (new class(fn() => new \Symfony\Component\DependencyInjection\Tests\Compiler\FooVoid()) extends \Symfony\Component\DependencyInjection\Argument\LazyClosure {
    public function __invoke(string $name) : void
    {
        // the `return` below causes the error
        return $this->service->__invoke(...\func_get_args());
    }
})->__invoke(...);

That return statement before calling the $this->service is not allowed and causes an error:

Compile Error: A void function must not return a value

/cc @nicolas-grekas

@carsonbot carsonbot added this to the 6.3 milestone Aug 3, 2023
@ruudk ruudk force-pushed the lazy-closure-void branch 2 times, most recently from c06e574 to 2cab4b7 Compare August 3, 2023 11:07
@carsonbot carsonbot changed the title Do not add return in LazyClosure when return type of closure is void [DependencyInjection] Do not add return in LazyClosure when return type of closure is void Aug 3, 2023
@nicolas-grekas
Copy link
Member

Thank you @ruudk.

@nicolas-grekas nicolas-grekas merged commit bbb5f5c into symfony:6.3 Aug 3, 2023
@ruudk ruudk deleted the lazy-closure-void branch August 3, 2023 11:41
@fabpot fabpot mentioned this pull request Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants