Skip to content

Commit

Permalink
Fix intersection types being nullable via implicit forced nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Jul 17, 2021
1 parent 3cfcfac commit 9e1c961
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Intersection types cannot be implicitly nullable
--FILE--
<?php

function foo(X&Y $foo = null) {}

?>
--EXPECTF--
Fatal error: Cannot use null as default value for parameter $foo of type X&Y in %s on line %d
9 changes: 9 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6694,6 +6694,15 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fall
zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
}

if (force_nullable && ZEND_TYPE_IS_INTERSECTION(arg_info->type)) {
zend_string *type_str = zend_type_to_string(arg_info->type);
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot use null as default value for parameter $%s of type %s",
/* We move type_str pointer one char forward to skip the '?' generated by
* the call to zend_compile_typename() */
ZSTR_VAL(name), ZSTR_VAL(type_str)+1);
}

if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
&& !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
zend_string *type_str = zend_type_to_string(arg_info->type);
Expand Down

0 comments on commit 9e1c961

Please sign in to comment.