Skip to content

Commit b373a1f

Browse files
committed
Do not emit ZEND_VERIFY_RETURN_TYPE if we return $this and return value is static
1 parent 2f4a214 commit b373a1f

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

Zend/zend_compile.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,6 +2696,17 @@ static void zend_emit_return_type_check(
26962696
/* we don't need run-time check */
26972697
return;
26982698
}
2699+
2700+
/* If return type contains static and we are returning $this
2701+
* (determined by checking if the previous opcode is ZEND_FETCH_THIS)
2702+
* then we don't need to check the return type */
2703+
if (expr && ZEND_TYPE_CONTAINS_CODE(type, IS_STATIC)) {
2704+
const zend_op_array *op_array = CG(active_op_array);
2705+
zend_op previous = op_array->opcodes[op_array->last-1];
2706+
if (previous.opcode == ZEND_FETCH_THIS) {
2707+
return;
2708+
}
2709+
}
26992710

27002711
opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
27012712
if (expr && expr->op_type == IS_CONST) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Return type check elision
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.optimization_level=-1
7+
opcache.opt_debug_level=0x20000
8+
opcache.preload=
9+
--EXTENSIONS--
10+
opcache
11+
--FILE--
12+
<?php
13+
14+
class C {
15+
public function returnStatic(): static {
16+
return $this;
17+
}
18+
}
19+
20+
?>
21+
--EXPECTF--
22+
$_main:
23+
; (lines=1, args=0, vars=0, tmps=0)
24+
; (after optimizer)
25+
; %s:1-10
26+
0000 RETURN int(1)
27+
28+
C::returnStatic:
29+
; (lines=2, args=0, vars=0, tmps=1)
30+
; (after optimizer)
31+
; %s:4-6
32+
0000 T0 = FETCH_THIS
33+
0001 RETURN T0

0 commit comments

Comments
 (0)