Skip to content

Add tests to check ZPP handles intersection types for internal functions #9100

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ public function toArginfoType(): ArginfoType {
$classTypes[] = $type;
}
}
return new ArginfoType($classTypes, $builtinTypes);
return new ArginfoType($classTypes, $builtinTypes, $this->isIntersection);
}

public function toOptimizerTypeMask(): string {
Expand Down Expand Up @@ -784,22 +784,25 @@ class ArginfoType {

/** @var SimpleType[] $builtinTypes */
private $builtinTypes;
private bool $isIntersection;

/**
* @param SimpleType[] $classTypes
* @param SimpleType[] $builtinTypes
*/
public function __construct(array $classTypes, array $builtinTypes) {
public function __construct(array $classTypes, array $builtinTypes, bool $isIntersection) {
$this->classTypes = $classTypes;
$this->builtinTypes = $builtinTypes;
$this->isIntersection = $isIntersection;
}

public function hasClassType(): bool {
return !empty($this->classTypes);
}

public function toClassTypeString(): string {
return implode('|', array_map(function(SimpleType $type) {
$separator = $this->isIntersection ? '&' : '|';
return implode($separator, array_map(function(SimpleType $type) {
return $type->toEscapedName();
}, $this->classTypes));
}
Expand All @@ -808,6 +811,7 @@ public function toTypeMask(): string {
if (empty($this->builtinTypes)) {
return '0';
}
/* Intersection types cannot happen with built-in types */
return implode('|', array_map(function(SimpleType $type) {
return $type->toTypeMask();
}, $this->builtinTypes));
Expand Down
64 changes: 45 additions & 19 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,51 @@ static ZEND_FUNCTION(zend_string_or_stdclass)
}
}

/* Tests Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL */
static ZEND_FUNCTION(zend_string_or_stdclass_or_null)
{
zend_string *str;
zend_object *object;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(object, zend_standard_class_def, str)
ZEND_PARSE_PARAMETERS_END();

if (str) {
RETURN_STR_COPY(str);
} else if (object) {
RETURN_OBJ_COPY(object);
} else {
RETURN_NULL();
}
}

static ZEND_FUNCTION(zend_intersection_type)
{
zend_object *object;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ(object)
ZEND_PARSE_PARAMETERS_END();

if (!(instanceof_function(object->ce, zend_ce_countable) && instanceof_function(object->ce, zend_ce_traversable))) {
zend_argument_type_error(1, "must be of type Traversable&Countable, %s given", ZSTR_VAL(object->ce->name));
}

RETURN_OBJ_COPY(object);
}

static ZEND_FUNCTION(zend_intersection_type_return)
{
zend_object *object;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ(object)
ZEND_PARSE_PARAMETERS_END();

RETURN_OBJ_COPY(object);
}

static ZEND_FUNCTION(zend_test_compile_string)
{
zend_string *source_string = NULL;
Expand Down Expand Up @@ -242,25 +287,6 @@ static ZEND_FUNCTION(zend_test_compile_string)
return;
}

/* Tests Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL */
static ZEND_FUNCTION(zend_string_or_stdclass_or_null)
{
zend_string *str;
zend_object *object;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(object, zend_standard_class_def, str)
ZEND_PARSE_PARAMETERS_END();

if (str) {
RETURN_STR_COPY(str);
} else if (object) {
RETURN_OBJ_COPY(object);
} else {
RETURN_NULL();
}
}

static ZEND_FUNCTION(zend_weakmap_attach)
{
zval *value;
Expand Down
4 changes: 4 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ function zend_string_or_stdclass($param): stdClass|string {}
/** @param stdClass|string|null $param */
function zend_string_or_stdclass_or_null($param): stdClass|string|null {}

function zend_intersection_type(Traversable&Countable $v): Traversable&Countable {}

function zend_intersection_type_return(object $v): Traversable&Countable {}

function zend_iterable(iterable $arg1, ?iterable $arg2 = null): void {}

function zend_weakmap_attach(object $object, mixed $value): bool {}
Expand Down
14 changes: 13 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions ext/zend_test/tests/zend_internal_arg_intersection.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Test that internal functions can accept intersection types via ZPP
--EXTENSIONS--
zend_test
spl
--FILE--
<?php

class C implements Countable {
public function count(): int {
return 1;
}
}

class I extends EmptyIterator implements Countable {
public function count(): int {
return 1;
}
}


try {
zend_intersection_type(new EmptyIterator());
} catch (TypeError $e) {
echo $e->getMessage(), PHP_EOL;
}
try {
zend_intersection_type(new C());
} catch (TypeError $e) {
echo $e->getMessage(), PHP_EOL;
}

zend_intersection_type(new I());

?>
==DONE==
--EXPECT--
zend_intersection_type(): Argument #1 ($v) must be of type Traversable&Countable, EmptyIterator given
zend_intersection_type(): Argument #1 ($v) must be of type Traversable&Countable, C given
==DONE==
40 changes: 40 additions & 0 deletions ext/zend_test/tests/zend_internal_return_intersection.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Test that internal functions warn on improper intersection types return
--EXTENSIONS--
zend_test
spl
--FILE--
<?php

class C implements Countable {
public function count(): int {
return 1;
}
}

class I extends EmptyIterator implements Countable {
public function count(): int {
return 1;
}
}

try {
var_dump(zend_intersection_type(new EmptyIterator()));
} catch (TypeError $e) {
echo $e->getMessage(), PHP_EOL;
}
try {
var_dump(zend_intersection_type(new C()));
} catch (TypeError $e) {
echo $e->getMessage(), PHP_EOL;
}
var_dump(zend_intersection_type(new I()));

?>
==DONE==
--EXPECT--
zend_intersection_type(): Argument #1 ($v) must be of type Traversable&Countable, EmptyIterator given
zend_intersection_type(): Argument #1 ($v) must be of type Traversable&Countable, C given
object(I)#2 (0) {
}
==DONE==