Skip to content

Commit b40ae80

Browse files
authored
Convert iterable into an internal alias for Traversable|array (#7309)
This does a compile time transformation of ``iterable`` into ``Traversable|array`` which simplifies some of the LSP variance handling. The arginfo generation script from stubs is updated to produce a union type when it encounters the type ``iterable`` Extension functions which do not regenerate the arginfo, or write them manually are still supported by mimicking the compile time transformation while registering the function. Type Reflection is preserved for single ``iterable`` (and ``?iterable``) to produce a ReflectionNamedType with name ``iterable``, however usage of ``iterable`` in union types will be converted to ``array|Traversable``
1 parent 94183d3 commit b40ae80

36 files changed

+341
-198
lines changed

Zend/Optimizer/zend_inference.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,9 +2114,6 @@ static uint32_t zend_convert_type_declaration_mask(uint32_t type_mask) {
21142114
if (type_mask & MAY_BE_CALLABLE) {
21152115
result_mask |= MAY_BE_STRING|MAY_BE_OBJECT|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
21162116
}
2117-
if (type_mask & MAY_BE_ITERABLE) {
2118-
result_mask |= MAY_BE_OBJECT|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2119-
}
21202117
if (type_mask & MAY_BE_STATIC) {
21212118
result_mask |= MAY_BE_OBJECT;
21222119
}

Zend/tests/return_types/generators001.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ function test6() : object|callable {
2626
yield 6;
2727
}
2828

29+
function test7() : iterable {
30+
yield 7;
31+
}
32+
2933
var_dump(
3034
test1(),
3135
test2(),
3236
test3(),
3337
test4(),
3438
test5(),
3539
test6(),
40+
test7(),
3641
);
3742
?>
3843
--EXPECTF--
@@ -48,3 +53,5 @@ object(Generator)#%d (%d) {
4853
}
4954
object(Generator)#%d (%d) {
5055
}
56+
object(Generator)#%d (%d) {
57+
}

Zend/tests/type_declarations/intersection_types/invalid_types/invalid_iterable_type.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ function foo(): iterable&Iterator {}
77

88
?>
99
--EXPECTF--
10-
Fatal error: Type iterable cannot be part of an intersection type in %s on line %d
10+
Fatal error: Type Traversable|array cannot be part of an intersection type in %s on line %d

Zend/tests/type_declarations/intersection_types/variance/invalid5.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ class Test2 extends Test {
1515

1616
?>
1717
--EXPECTF--
18-
Fatal error: Declaration of Test2::method(): X&Y must be compatible with Test::method(): iterable in %s on line %d
18+
Fatal error: Declaration of Test2::method(): X&Y must be compatible with Test::method(): Traversable|array in %s on line %d

Zend/tests/type_declarations/iterable_001.phpt renamed to Zend/tests/type_declarations/iterable/iterable_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ object(ArrayIterator)#1 (1) {
4545
int(3)
4646
}
4747
}
48-
test(): Argument #1 ($iterable) must be of type iterable, int given, called in %s on line %d
48+
test(): Argument #1 ($iterable) must be of type Traversable|array, int given, called in %s on line %d

Zend/tests/type_declarations/iterable_002.phpt renamed to Zend/tests/type_declarations/iterable/iterable_002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ function baz(iterable $iterable = 1) {
1717

1818
?>
1919
--EXPECTF--
20-
Fatal error: Cannot use int as default value for parameter $iterable of type iterable in %s on line %d
20+
Fatal error: Cannot use int as default value for parameter $iterable of type Traversable|array in %s on line %d

Zend/tests/type_declarations/iterable_003.phpt renamed to Zend/tests/type_declarations/iterable/iterable_003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ array(0) {
2929
}
3030
object(Generator)#2 (0) {
3131
}
32-
baz(): Return value must be of type iterable, int returned
32+
baz(): Return value must be of type Traversable|array, int returned

Zend/tests/type_declarations/iterable_004.phpt renamed to Zend/tests/type_declarations/iterable/iterable_004.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ class Bar extends Foo {
2121

2222
?>
2323
--EXPECTF--
24-
Fatal error: Declaration of Bar::testScalar(iterable $iterable) must be compatible with Foo::testScalar(int $int) in %s on line %d
24+
Fatal error: Declaration of Bar::testScalar(Traversable|array $iterable) must be compatible with Foo::testScalar(int $int) in %s on line %d

Zend/tests/type_declarations/iterable_005.phpt renamed to Zend/tests/type_declarations/iterable/iterable_005.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ class TestScalar extends Test {
2929

3030
?>
3131
--EXPECTF--
32-
Fatal error: Declaration of TestScalar::method(): int must be compatible with Test::method(): iterable in %s on line %d
32+
Fatal error: Declaration of TestScalar::method(): int must be compatible with Test::method(): Traversable|array in %s on line %d
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
Test "or null"/"or be null" in type-checking errors for userland functions with iterable
3+
--FILE--
4+
<?php
5+
6+
// This should test every branch in zend_execute.c's `zend_verify_arg_type`, `zend_verify_return_type` and `zend_verify_missing_return_type` functions which produces an "or null"/"or be null" part in its error message
7+
8+
function iterableF(?iterable $param) {}
9+
try {
10+
iterableF(1);
11+
} catch (\TypeError $e) {
12+
echo $e, PHP_EOL;
13+
}
14+
15+
function returnIterable(): ?iterable {
16+
return 1;
17+
}
18+
19+
try {
20+
returnIterable();
21+
} catch (\TypeError $e) {
22+
echo $e, PHP_EOL;
23+
}
24+
25+
function returnMissingIterable(): ?iterable {
26+
}
27+
28+
try {
29+
returnMissingIterable();
30+
} catch (\TypeError $e) {
31+
echo $e, PHP_EOL;
32+
}
33+
?>
34+
--EXPECTF--
35+
TypeError: iterableF(): Argument #1 ($param) must be of type Traversable|array|null, int given, called in %s on line %d and defined in %s:%d
36+
Stack trace:
37+
#0 %s(%d): iterableF(1)
38+
#1 {main}
39+
TypeError: returnIterable(): Return value must be of type Traversable|array|null, int returned in %s:%d
40+
Stack trace:
41+
#0 %s(%d): returnIterable()
42+
#1 {main}
43+
TypeError: returnMissingIterable(): Return value must be of type Traversable|array|null, none returned in %s:%d
44+
Stack trace:
45+
#0 %s(%d): returnMissingIterable()
46+
#1 {main}

0 commit comments

Comments
 (0)