Skip to content

Fix GH-8623: Better error message for first-class callable of undefined function - #25647

Open
GuilhermeAltmann wants to merge 1 commit into
php:masterfrom
GuilhermeAltmann:fix-gh8623-fcc-undefined-function
Open

GuilhermeAltmann wants to merge 1 commit into
php:masterfrom
GuilhermeAltmann:fix-gh8623-fcc-undefined-function

Conversation

@GuilhermeAltmann

Copy link
Copy Markdown

Fixes GH-8623.

First-class callables compile to an INIT_* opcode immediately followed by ZEND_CALLABLE_CONVERT. When the function does not exist, the INIT_* opcode fails with "Call to undefined function", which is misleading because no call is taking place — the call frame is about to be converted into a closure.

This PR checks, on the error paths for undefined functions, whether the next opcode is ZEND_CALLABLE_CONVERT and emits a closure-specific message instead:

Failed to create closure from undefined function missing()

The wording is aligned with the existing Closure::fromCallable() TypeError ("Failed to create closure from callable: function "missing" not found or invalid function name").

Covered code paths:

  • ZEND_INIT_FCALL_BY_NAME / ZEND_INIT_NS_FCALL_BY_NAME via zend_undefined_function_helper (missing(...), 'missing'(...), Foo\missing(...))
  • ZEND_INIT_DYNAMIC_CALL for string callables ($name(...)), by passing the first-class callable context to zend_init_dynamic_call_string()
  • constant-expression FCC evaluation (zend_ast.c), where the AST is already known to be a first-class callable

Out of scope, per the discussion in GH-8623 and #7019:

  • Methods: the "Call to undefined method" error originates in the get_method object handler, which does not know about the call kind.
  • Partial applications (foo(?)): ZEND_CALLABLE_CONVERT_PARTIAL is not adjacent to the INIT opcode (the argument sends sit in between), so runtime PFAs keep the current message.

Common calls are unaffected: missing() still reports "Call to undefined function missing()".

Tests: new Zend/tests/first_class_callable/gh8623.phpt, and updated expectations of five existing tests that asserted the old message in FCC context (four under Zend/tests/first_class_callable/, one under ext/opcache).

Validation: full test suite on an NTS debug build (17151 tests, no new failures), the Zend suite on a ZTS debug build, and sanity runs with opcache enabled (the INIT → CALLABLE_CONVERT sequence is preserved by the optimizer).

Parts of this PR description were drafted with the help of an LLM coding agent.

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.

Awkward error using first-class callable syntax with missing function

1 participant