Skip to content

Zend: Add tests for relative types #17764

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

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Cannot use parent type outside a class/trait: global function
--FILE--
<?php

function foo($x): parent {};

?>
--EXPECTF--
Fatal error: Cannot use "parent" when no class scope is active in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Cannot use parent type outside a class/trait: interface
--FILE--
<?php

interface T {
function foo($x): parent;
}

?>
--EXPECTF--
Fatal error: Cannot use "parent" when current class scope has no parent in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Cannot use self type outside a class/trait: global function
--FILE--
<?php

function foo($x): self {};

?>
--EXPECTF--
Fatal error: Cannot use "self" when no class scope is active in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Cannot use static type outside a class/trait: global function
--FILE--
<?php

function foo($x): static {};

?>
--EXPECTF--
Fatal error: Cannot use "static" when no class scope is active in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Relative class types can be used for closures as it may be bound to a class
--FILE--
<?php

$fn1 = function($x): self {};
$fn2 = function($x): parent {};
$fn3 = function($x): static {};

?>
DONE
--EXPECT--
DONE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Eval Class definition should not leak memory when using compiled traits
--FILE--
<?php

trait TraitCompiled {
public function bar(): self { return new self; }
}

const EVAL_CODE = <<<'CODE'
class A {
use TraitCompiled;
}
CODE;

eval(EVAL_CODE);

$a1 = new A();
$a2 = $a1->bar();
var_dump($a2);

?>
DONE
--EXPECT--
object(A)#2 (0) {
}
DONE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Eval code should not leak memory when using traits
--FILE--
<?php

const EVAL_CODE = <<<'CODE'
trait TraitEval {
public function bar(): self { return new self; }
}
CODE;

eval(EVAL_CODE);

class A {
use TraitEval;
}

$a1 = new A();
$a2 = $a1->bar();
var_dump($a2);

?>
DONE
--EXPECT--
object(A)#2 (0) {
}
DONE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Cannot use a trait which references parent as a type in a class with no parent, single type
--FILE--
<?php
trait TraitExample {
public function bar(): parent { return parent::class; }
}

class A {
use TraitExample;
}
?>
DONE
--EXPECT--
DONE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Cannot use a trait which references parent as a type in a class with no parent, nullable type
--FILE--
<?php
trait TraitExample {
public function bar(): ?parent { return parent::class; }
}

class A {
use TraitExample;
}
?>
DONE
--EXPECT--
DONE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Cannot use a trait which references parent as a type in a class with no parent, union type
--FILE--
<?php
trait TraitExample {
public function bar(): int|parent { return parent::class; }
}

class A {
use TraitExample;
}
?>
DONE
--EXPECT--
DONE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Cannot use a trait which references parent as a type in a class with no parent, DNF type
--FILE--
<?php
trait TraitExample {
public function bar(): (X&Y)|parent { return parent::class; }
}

class A {
use TraitExample;
}
?>
DONE
--EXPECT--
DONE
Loading