Skip to content

Add #[\NoDiscard] attribute #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Zend/Optimizer/dce.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ static inline bool may_have_side_effects(
case ZEND_IS_SMALLER_OR_EQUAL:
case ZEND_CASE:
case ZEND_CASE_STRICT:
case ZEND_CAST:
case ZEND_ROPE_INIT:
case ZEND_ROPE_ADD:
case ZEND_INIT_ARRAY:
Expand All @@ -127,6 +126,8 @@ static inline bool may_have_side_effects(
case ZEND_ARRAY_KEY_EXISTS:
/* No side effects */
return 0;
case ZEND_CAST:
return opline->extended_value == IS_UNDEF;
case ZEND_ADD_ARRAY_ELEMENT:
/* TODO: We can't free two vars. Keep instruction alive. <?php [0, "$a" => "$b"]; */
if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
Expand Down
2 changes: 1 addition & 1 deletion Zend/Optimizer/optimize_func_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void zend_delete_call_instructions(zend_op_array *op_array, zend_op *opli
static void zend_try_inline_call(zend_op_array *op_array, zend_op *fcall, zend_op *opline, zend_function *func)
{
if (func->type == ZEND_USER_FUNCTION
&& !(func->op_array.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_DEPRECATED))
&& !(func->op_array.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_DEPRECATED|ZEND_ACC_NODISCARD))
/* TODO: function copied from trait may be inconsistent ??? */
&& !(func->op_array.fn_flags & (ZEND_ACC_TRAIT_CLONE))
&& fcall->extended_value >= func->op_array.required_num_args
Expand Down
2 changes: 2 additions & 0 deletions Zend/Optimizer/zend_inference.c
Original file line number Diff line number Diff line change
Expand Up @@ -5287,6 +5287,8 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
return (t1 & MAY_BE_OBJECT);
case IS_OBJECT:
return 0;
case IS_UNDEF:
return 0;
EMPTY_SWITCH_DEFAULT_CASE()
}
/* GCC is getting confused here for the Wimplicit-fallthrough warning with
Expand Down
86 changes: 86 additions & 0 deletions Zend/tests/attributes/nodiscard/001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--TEST--
#[\NoDiscard]: Basic test.
--FILE--
<?php

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

#[\NoDiscard("this is important")]
function test2(): int {
return 0;
}

#[\NoDiscard]
function test3(...$args): int {
return 0;
}

class Clazz {
#[\NoDiscard]
function test(): int {
return 0;
}

#[\NoDiscard("this is important")]
function test2(): int {
return 0;
}

#[\NoDiscard]
static function test3(): int {
return 0;
}
}

$closure = #[\NoDiscard] function(): int {
return 0;
};

$closure2 = #[\NoDiscard] function(): int {
return 0;
};

test();
test2();
test3(1, 2, named: 3);
call_user_func("test");
$fcc = test(...);
$fcc();

$cls = new Clazz();
$cls->test();
$cls->test2();
Clazz::test3();

call_user_func([$cls, "test"]);

$closure();

$closure2();

?>
--EXPECTF--
Warning: (B)The return value of function test() is expected to be consumed in %s on line %d

Warning: (B)The return value of function test2() is expected to be consumed, this is important in %s on line %d

Warning: (B)The return value of function test3() is expected to be consumed in %s on line %d

Warning: (A)The return value of function test() is expected to be consumed in %s on line %d

Warning: (A)The return value of function test() is expected to be consumed in %s on line %d

Warning: (A)The return value of method Clazz::test() is expected to be consumed in %s on line %d

Warning: (A)The return value of method Clazz::test2() is expected to be consumed, this is important in %s on line %d

Warning: (B)The return value of method Clazz::test3() is expected to be consumed in %s on line %d

Warning: (A)The return value of method Clazz::test() is expected to be consumed in %s on line %d

Warning: (A)The return value of function {closure:%s:%d}() is expected to be consumed in %s on line %d

Warning: (A)The return value of function {closure:%s:%d}() is expected to be consumed in %s on line %d
44 changes: 44 additions & 0 deletions Zend/tests/attributes/nodiscard/002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
#[\NoDiscard]: __call(), __callStatic(), and __invoke().
--FILE--
<?php

class Clazz {
#[\NoDiscard]
public function __call(string $name, array $args): int {
echo "__call({$name})", PHP_EOL;

return strlen($name);
}

#[\NoDiscard]
public static function __callStatic(string $name, array $args): int {
echo "__callStatic({$name})", PHP_EOL;

return strlen($name);
}

#[\NoDiscard]
public function __invoke(string $param): int {
echo "__invoke({$param})", PHP_EOL;

return strlen($param);
}
}

$cls = new Clazz();
$cls->test();
Clazz::test();
$cls('foo');

?>
--EXPECTF--
Warning: (A)The return value of method Clazz::test() is expected to be consumed in %s on line %d
__call(test)

Warning: (A)The return value of method Clazz::test() is expected to be consumed in %s on line %d
__callStatic(test)

Warning: (A)The return value of method Clazz::__invoke() is expected to be consumed in %s on line %d
__invoke(foo)

22 changes: 22 additions & 0 deletions Zend/tests/attributes/nodiscard/003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
#[\NoDiscard]: Taken from trait.
--FILE--
<?php

trait T {
#[\NoDiscard]
function test(): int {
return 0;
}
}

class Clazz {
use T;
}

$cls = new Clazz();
$cls->test();

?>
--EXPECTF--
Warning: (A)The return value of method Clazz::test() is expected to be consumed in %s on line %d
17 changes: 17 additions & 0 deletions Zend/tests/attributes/nodiscard/005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
#[\NoDiscard]: Native function and method.
--FILE--
<?php

$f = tmpfile();
flock($f, LOCK_SH | LOCK_NB);
fclose($f);

$date = new DateTimeImmutable('now');
$date->setTimestamp(0);

?>
--EXPECTF--
Warning: (B)The return value of function flock() is expected to be consumed, as locking the stream might have failed in %s on line %d

Warning: (A)The return value of method DateTimeImmutable::setTimestamp() is expected to be consumed, as DateTimeImmutable::setTimestamp() does not modify the object itself in %s on line %d
20 changes: 20 additions & 0 deletions Zend/tests/attributes/nodiscard/006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
#[\NoDiscard]: execute_ex overwritten
--EXTENSIONS--
zend_test
--INI--
zend_test.replace_zend_execute_ex=1
opcache.jit=disable
--FILE--
<?php

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

test();

?>
--EXPECTF--
Warning: (A)The return value of function test() is expected to be consumed in %s on line %d
21 changes: 21 additions & 0 deletions Zend/tests/attributes/nodiscard/007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
#[\NoDiscard]: execute_internal overwritten
--EXTENSIONS--
zend_test
--INI--
zend_test.observer.execute_internal=1
--FILE--
<?php

$f = tmpfile();
flock($f, LOCK_SH | LOCK_NB);
fclose($f);

?>
--EXPECTF--
<!-- internal enter tmpfile() -->
<!-- internal enter NoDiscard::__construct() -->

Warning: (A)The return value of function flock() is expected to be consumed, as locking the stream might have failed in %s on line %d
<!-- internal enter flock() -->
<!-- internal enter fclose() -->
18 changes: 18 additions & 0 deletions Zend/tests/attributes/nodiscard/008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
#[\NoDiscard]: Combining with #[\Deprecated].
--FILE--
<?php

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

test();

?>
--EXPECTF--
Deprecated: Function test() is deprecated in %s on line %d

Warning: (B)The return value of function test() is expected to be consumed in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/attributes/nodiscard/009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
#[\NoDiscard]: Combining with #[\Deprecated] (Internal).
--EXTENSIONS--
zend_test
--FILE--
<?php

zend_test_deprecated_nodiscard();

?>
--EXPECTF--
Deprecated: Function zend_test_deprecated_nodiscard() is deprecated, custom message in %s on line %d

Warning: (B)The return value of function zend_test_deprecated_nodiscard() is expected to be consumed, custom message 2 in %s on line %d
21 changes: 21 additions & 0 deletions Zend/tests/attributes/nodiscard/error_code_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
#[\NoDiscard]: Code is E_USER_WARNING.
--FILE--
<?php

set_error_handler(function (int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) {
var_dump($errno, E_USER_WARNING, $errno === E_USER_WARNING);
});

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

test();

?>
--EXPECT--
int(512)
int(512)
bool(true)
14 changes: 14 additions & 0 deletions Zend/tests/attributes/nodiscard/property_readonly_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
#[\NoDiscard]: NoDiscard::$message is readonly.
--FILE--
<?php

$d = new \NoDiscard("foo");
$d->message = 'bar';

?>
--EXPECTF--
Fatal error: Uncaught Error: Cannot modify readonly property NoDiscard::$message in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
15 changes: 15 additions & 0 deletions Zend/tests/attributes/nodiscard/property_readonly_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
#[\NoDiscard]: __construct() respects that properties are readonly.
--FILE--
<?php

$d = new \NoDiscard("foo");
$d->__construct("bar");

?>
--EXPECTF--
Fatal error: Uncaught Error: Cannot modify readonly property NoDiscard::$message in %s:%d
Stack trace:
#0 %s(%d): NoDiscard->__construct('bar')
#1 {main}
thrown in %s on line %d
Loading
Loading