Skip to content

Commit 9e1c961

Browse files
committed
Fix intersection types being nullable via implicit forced nullability
1 parent 3cfcfac commit 9e1c961

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Intersection types cannot be implicitly nullable
3+
--FILE--
4+
<?php
5+
6+
function foo(X&Y $foo = null) {}
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use null as default value for parameter $foo of type X&Y in %s on line %d

Zend/zend_compile.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6694,6 +6694,15 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fall
66946694
zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
66956695
}
66966696

6697+
if (force_nullable && ZEND_TYPE_IS_INTERSECTION(arg_info->type)) {
6698+
zend_string *type_str = zend_type_to_string(arg_info->type);
6699+
zend_error_noreturn(E_COMPILE_ERROR,
6700+
"Cannot use null as default value for parameter $%s of type %s",
6701+
/* We move type_str pointer one char forward to skip the '?' generated by
6702+
* the call to zend_compile_typename() */
6703+
ZSTR_VAL(name), ZSTR_VAL(type_str)+1);
6704+
}
6705+
66976706
if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
66986707
&& !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
66996708
zend_string *type_str = zend_type_to_string(arg_info->type);

0 commit comments

Comments
 (0)