Skip to content

void: document that returning a value is a compile-time error - #5920

Merged
lacatoire merged 2 commits into
php:masterfrom
SatvikMishra08:void-return-compile-error
Oct 8, 2026
Merged

lacatoire merged 2 commits into
php:masterfrom
SatvikMishra08:void-return-compile-error

Conversation

@SatvikMishra08

Copy link
Copy Markdown
Contributor

Closes #5389

This adds the point from the issue that @DanielEScherzer agreed should be documented: a return with an expression in a void function is a compile-time error, even when the return can't be reached. I also mentioned return null;, since that's the case people usually hit, and PHP's error message points them to a bare return;.

The other points in the issue got pushback in the discussion (exceptions, TypeErrors, "return-only type" is already in the first sentence), so I left them out. This mirrors what #5906 did for never.

Comment thread language/types/void.xml Outdated
<type>void</type> function is a compile-time error, even if the
<literal>return</literal> would not be reached during execution.
This includes <literal>return null;</literal>; use a bare
<literal>return;</literal> to exit the function early.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<literal>return;</literal> to exit the function early.
<literal>return;</literal> to exit the function early. The
<literal>null</literal> mentioned below is only the implicit value of
the call.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, applied your suggestion in 3cf5d28.

Co-authored-by: Louis-Arnaud <lacatoire@users.noreply.github.com>
@lacatoire

Copy link
Copy Markdown
Member

Thanks @SatvikMishra08 !

@lacatoire
lacatoire merged commit fff1bba into php:master Oct 8, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A "void" function is only checked at compile time, and may still throw

2 participants