Skip to content

Commit 8ebf986

Browse files
authored
Zend: Add tests for relative types (#17764)
1 parent 1ad7743 commit 8ebf986

11 files changed

+169
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot use parent type outside a class/trait: global function
3+
--FILE--
4+
<?php
5+
6+
function foo($x): parent {};
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use "parent" when no class scope is active in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Cannot use parent type outside a class/trait: interface
3+
--FILE--
4+
<?php
5+
6+
interface T {
7+
function foo($x): parent;
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Cannot use "parent" when current class scope has no parent in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot use self type outside a class/trait: global function
3+
--FILE--
4+
<?php
5+
6+
function foo($x): self {};
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use "self" when no class scope is active in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot use static type outside a class/trait: global function
3+
--FILE--
4+
<?php
5+
6+
function foo($x): static {};
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use "static" when no class scope is active in %s on line %d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Relative class types can be used for closures as it may be bound to a class
3+
--FILE--
4+
<?php
5+
6+
$fn1 = function($x): self {};
7+
$fn2 = function($x): parent {};
8+
$fn3 = function($x): static {};
9+
10+
?>
11+
DONE
12+
--EXPECT--
13+
DONE
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Eval Class definition should not leak memory when using compiled traits
3+
--FILE--
4+
<?php
5+
6+
trait TraitCompiled {
7+
public function bar(): self { return new self; }
8+
}
9+
10+
const EVAL_CODE = <<<'CODE'
11+
class A {
12+
use TraitCompiled;
13+
}
14+
CODE;
15+
16+
eval(EVAL_CODE);
17+
18+
$a1 = new A();
19+
$a2 = $a1->bar();
20+
var_dump($a2);
21+
22+
?>
23+
DONE
24+
--EXPECT--
25+
object(A)#2 (0) {
26+
}
27+
DONE
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Eval code should not leak memory when using traits
3+
--FILE--
4+
<?php
5+
6+
const EVAL_CODE = <<<'CODE'
7+
trait TraitEval {
8+
public function bar(): self { return new self; }
9+
}
10+
CODE;
11+
12+
eval(EVAL_CODE);
13+
14+
class A {
15+
use TraitEval;
16+
}
17+
18+
$a1 = new A();
19+
$a2 = $a1->bar();
20+
var_dump($a2);
21+
22+
?>
23+
DONE
24+
--EXPECT--
25+
object(A)#2 (0) {
26+
}
27+
DONE
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Cannot use a trait which references parent as a type in a class with no parent, single type
3+
--FILE--
4+
<?php
5+
trait TraitExample {
6+
public function bar(): parent { return parent::class; }
7+
}
8+
9+
class A {
10+
use TraitExample;
11+
}
12+
?>
13+
DONE
14+
--EXPECT--
15+
DONE
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Cannot use a trait which references parent as a type in a class with no parent, nullable type
3+
--FILE--
4+
<?php
5+
trait TraitExample {
6+
public function bar(): ?parent { return parent::class; }
7+
}
8+
9+
class A {
10+
use TraitExample;
11+
}
12+
?>
13+
DONE
14+
--EXPECT--
15+
DONE
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Cannot use a trait which references parent as a type in a class with no parent, union type
3+
--FILE--
4+
<?php
5+
trait TraitExample {
6+
public function bar(): int|parent { return parent::class; }
7+
}
8+
9+
class A {
10+
use TraitExample;
11+
}
12+
?>
13+
DONE
14+
--EXPECT--
15+
DONE
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Cannot use a trait which references parent as a type in a class with no parent, DNF type
3+
--FILE--
4+
<?php
5+
trait TraitExample {
6+
public function bar(): (X&Y)|parent { return parent::class; }
7+
}
8+
9+
class A {
10+
use TraitExample;
11+
}
12+
?>
13+
DONE
14+
--EXPECT--
15+
DONE

0 commit comments

Comments
 (0)