Skip to content

#[\NoDiscard] with PHP function version of call_user_func() does not produce any warning. #25028

Closed as not planned

Description

@Girgias

Description

The following code:

<?php
#[\NoDiscard]
function test(): int {
	return 0;
}

if (random_int(1,1)) {
    $fn = 'call_user_func';
} else {
    $fn = 'nope';
}

$fn('test');

See: https://3v4l.org/V8TSN#v

Resulted in this output:

But I expected this output instead:

Warning: The return value of function test() should either be used or intentionally ignored by casting it as (void) in %s on line %d

The root cause is that there is no handling of a potential #[\NoDiscard] attribute in the PHP_FUNCTION(call_user_func) version. I'm guessing this also not handled in PHP_FUNCTION(call_user_func_array), PHP_FUNCTION(forward_static_call), and PHP_FUNCTION(forward_static_call_array).

PHP Version

PHP 8.5 and above

Operating System

No response

Activity

  1. TimWolla commented on Oct 29, 2026

    @TimWolla
    Member

    This is working as designed: Internally call_user_func() is consuming the return value of test() - it's just that the return value of call_user_func() is unused and we cannot make the attribute conditional.

  2. ndossche commented on Oct 29, 2026

    @ndossche
    Member

    I agree with your reasoning, but note that due to the optimizer, the behaviour changes: https://3v4l.org/oJ3IK#v

  3. TimWolla commented on Oct 29, 2026

    @TimWolla
    Member

    6. but note that due to the optimizer, the behaviour changes:

    Yes, I'm aware. I would consider that to be one of the acceptable behavioral differences introduced by optimization. Technically the optimizer could emit a (void) cast there, but that is probably not worth it, particularly since call_user_func() is likely rarely used in modern code (due to more ergonomic alternatives).

  4. Girgias commented on Oct 29, 2026

    @Girgias
    MemberAuthor

    Sure, I don't have strong feelings about it. Will close as won't fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions