Skip to content

Commit 83d64ea

Browse files
committed
feat(zend, reflection): enforce generic-arg arity at call sites and inheritance
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 416232c commit 83d64ea

51 files changed

Lines changed: 1280 additions & 612 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Zend/tests/generics/erasure/turbofish_no_runtime_effect.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
--TEST--
2-
Erasure: turbofish has zero runtime effect
2+
Erasure: turbofish (with matching arity) has zero runtime effect
33
--FILE--
44
<?php
5-
function f($x) { return $x * 2; }
5+
function f<T>($x) { return $x * 2; }
66
var_dump(f(5));
77
var_dump(f::<int>(5));
8-
var_dump(f::<int, string, float>(5));
8+
var_dump(f::<string>(5));
99
?>
1010
--EXPECT--
1111
int(10)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Inheritance arity: extends a non-generic class with type arguments is a compile error
3+
--FILE--
4+
<?php
5+
class Plain {}
6+
class Bad extends Plain<int> {}
7+
?>
8+
--EXPECTF--
9+
Fatal error: Too many generic type arguments to extends Plain in Bad, 1 passed and exactly 0 expected in %s on line %d
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Inheritance arity: extends Pair<K, V=mixed> with 3 args is a compile error
3+
--FILE--
4+
<?php
5+
class Pair<K, V = mixed> {}
6+
class Bad extends Pair<int, string, float> {}
7+
?>
8+
--EXPECTF--
9+
Fatal error: Too many generic type arguments to extends Pair in Bad, 3 passed and at most 2 expected in %s on line %d
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Inheritance arity: extends with too few type arguments is a compile error
3+
--FILE--
4+
<?php
5+
class Pair<K, V> {}
6+
class Bad extends Pair<int> {}
7+
?>
8+
--EXPECTF--
9+
Fatal error: Too few generic type arguments to extends Pair in Bad, 1 passed and exactly 2 expected in %s on line %d
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Inheritance arity: extends with too many type arguments is a compile error
3+
--FILE--
4+
<?php
5+
class Box<T> {}
6+
class Bad extends Box<int, string> {}
7+
?>
8+
--EXPECTF--
9+
Fatal error: Too many generic type arguments to extends Box in Bad, 2 passed and exactly 1 expected in %s on line %d
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Inheritance arity: implements with too few type arguments is a compile error
3+
--FILE--
4+
<?php
5+
interface Map<K, V> {}
6+
class Bad implements Map<int> {}
7+
?>
8+
--EXPECTF--
9+
Fatal error: Too few generic type arguments to implements Map in Bad, 1 passed and exactly 2 expected in %s on line %d
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Inheritance arity: interface extends another interface with wrong arity is a compile error
3+
--FILE--
4+
<?php
5+
interface J<T> {}
6+
interface IExt extends J<int, string> {}
7+
?>
8+
--EXPECTF--
9+
Fatal error: Too many generic type arguments to extends J in IExt, 2 passed and exactly 1 expected in %s on line %d
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Inheritance arity: use trait with too many type arguments is a compile error
3+
--FILE--
4+
<?php
5+
trait Holder<T> { public T $v; }
6+
class Bad { use Holder<int, string>; }
7+
?>
8+
--EXPECTF--
9+
Fatal error: Too many generic type arguments to use Holder in Bad, 2 passed and exactly 1 expected in %s on line %d
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Inheritance arity: defaults extend the accepted range
3+
--FILE--
4+
<?php
5+
class Pair<K, V = mixed> {}
6+
interface Maybe<T = mixed> {}
7+
trait Slot<T = int> {}
8+
9+
// Required-only arity
10+
class P1 extends Pair<int> {}
11+
// Including the optional
12+
class P2 extends Pair<int, string> {}
13+
// Trait with default — works without args
14+
class T1 { use Slot; }
15+
16+
echo "OK\n";
17+
?>
18+
--EXPECT--
19+
OK

Zend/tests/generics/syntax/turbofish_complex_args.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Generic syntax: turbofish with composite type arguments
33
--FILE--
44
<?php
5-
function f($x) { return $x; }
5+
function f<A, B = mixed>($x) { return $x; }
66
echo f::<int|string>(42), "\n";
77
echo f::<int|string, (Throwable&Stringable)|null>('hi'), "\n";
88
?>

0 commit comments

Comments
 (0)