Skip to content

[RFC] Implement structs (WIP) #13800

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Zend/tests/readonly_classes/readonly_enum.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ readonly enum Foo

?>
--EXPECTF--
Parse error: syntax error, unexpected token "enum", expecting "abstract" or "final" or "readonly" or "class" in %s on line %d
Parse error: syntax error, unexpected token "enum" in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/readonly_classes/readonly_interface.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ readonly interface Foo

?>
--EXPECTF--
Parse error: syntax error, unexpected token "interface", expecting "abstract" or "final" or "readonly" or "class" in %s on line %d
Parse error: syntax error, unexpected token "interface" in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/readonly_classes/readonly_trait.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ readonly trait Foo

?>
--EXPECTF--
Parse error: syntax error, unexpected token "trait", expecting "abstract" or "final" or "readonly" or "class" in %s on line %d
Parse error: syntax error, unexpected token "trait" in %s on line %d
10 changes: 10 additions & 0 deletions Zend/tests/structs/abstract.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Structs must not be marked as abstract
--FILE--
<?php

abstract struct Box {}

?>
--EXPECTF--
Fatal error: Cannot use the abstract modifier on a struct, as structs are implicitly final in %s on line %d
111 changes: 111 additions & 0 deletions Zend/tests/structs/array_access.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
--TEST--
Structs implementing ArrayAccess
--FILE--
<?php

abstract class VectorBase implements ArrayAccess {
public $elements;

public function offsetExists(mixed $offset): bool {
return array_key_exists($offset, $this->elements);
}

public function offsetSet(mixed $offset, mixed $value): void {
if ($offset) {
$this->elements[$offset] = $value;
} else {
$this->elements[] = $value;
}
}

public function offsetUnset(mixed $offset): void {
unset($this->elements[$offset]);
}
}

struct VectorByVal extends VectorBase {
public function offsetGet(mixed $offset): mixed {
return $this->elements[$offset];
}
}

struct VectorByRef extends VectorBase {
public function &offsetGet(mixed $offset): mixed {
return $this->elements[$offset];
}
}

struct Box {
public function __construct(
public int $value,
) {}

public mutating function inc() {
$this->value++;
}
}

$box = new Box(1);

$vec = new VectorByVal();
$vec[] = $box;
$vec[0]->value = 2;
var_dump($vec);
$vec[0]->inc!();
var_dump($vec);

$vec = new VectorByRef();
$vec[] = $box;
$vec[0]->value = 2;
var_dump($vec);
$vec[0]->inc!();
var_dump($vec);

var_dump($box);

?>
--EXPECT--
object(VectorByVal)#2 (1) {
["elements"]=>
array(1) {
[0]=>
object(Box)#1 (1) {
["value"]=>
int(1)
}
}
}
object(VectorByVal)#2 (1) {
["elements"]=>
array(1) {
[0]=>
object(Box)#1 (1) {
["value"]=>
int(1)
}
}
}
object(VectorByRef)#3 (1) {
["elements"]=>
array(1) {
[0]=>
object(Box)#2 (1) {
["value"]=>
int(2)
}
}
}
object(VectorByRef)#3 (1) {
["elements"]=>
array(1) {
[0]=>
object(Box)#2 (1) {
["value"]=>
int(3)
}
}
}
object(Box)#1 (1) {
["value"]=>
int(1)
}
27 changes: 27 additions & 0 deletions Zend/tests/structs/assign_op.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Assign op on structs
--FILE--
<?php

struct Box {
public function __construct(
public $value,
) {}
}

$a = new Box(0);
$b = $a;
$b->value += 1;
var_dump($a);
var_dump($b);

?>
--EXPECT--
object(Box)#1 (1) {
["value"]=>
int(0)
}
object(Box)#2 (1) {
["value"]=>
int(1)
}
27 changes: 27 additions & 0 deletions Zend/tests/structs/assign_ref.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Assign ref on structs
--FILE--
<?php

struct Box {
public $value;
}

$a = new Box();
$a->value = 1;
$b = $a;
$c = 2;
$b->value = &$c;
var_dump($a);
var_dump($b);

?>
--EXPECT--
object(Box)#1 (1) {
["value"]=>
int(1)
}
object(Box)#2 (1) {
["value"]=>
&int(2)
}
32 changes: 32 additions & 0 deletions Zend/tests/structs/assign_to_self.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Send structs
--FILE--
<?php

struct Box {
public function __construct(
public $value,
) {}
}

$a = new Box(42);
$a->value = $a;
var_dump($a);

$b = new Box(42);
$b->value = &$b;
var_dump($b);

?>
--EXPECT--
object(Box)#2 (1) {
["value"]=>
object(Box)#1 (1) {
["value"]=>
int(42)
}
}
object(Box)#3 (1) {
["value"]=>
*RECURSION*
}
33 changes: 33 additions & 0 deletions Zend/tests/structs/basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Basic structs
--FILE--
<?php

struct Point {
public function __construct(
public int $x,
public int $y,
) {}
}

$a = new Point(1, 1);
$b = $a;
$b->x = 2;
$b->y = 2;
var_dump($a);
var_dump($b);

?>
--EXPECT--
object(Point)#1 (2) {
["x"]=>
int(1)
["y"]=>
int(1)
}
object(Point)#2 (2) {
["x"]=>
int(2)
["y"]=>
int(2)
}
10 changes: 10 additions & 0 deletions Zend/tests/structs/final.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Structs must not be marked as final
--FILE--
<?php

final struct Box {}

?>
--EXPECTF--
Fatal error: Cannot use the final modifier on a struct, as structs are implicitly final in %s on line %d
43 changes: 43 additions & 0 deletions Zend/tests/structs/interior_immutability.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Basic structs
--FILE--
<?php

struct Child {
public function __construct(
public $value,
) {}
}

class Parent_ {
public function __construct(
public Child $writableChild,
public readonly Child $readonlyChild,
) {}
}

$parent = new Parent_(new Child(1), new Child(1));
$parent->writableChild->value = 2;
try {
$parent->readonlyChild->value = 2;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

var_dump($parent);

?>
--EXPECTF--
Cannot indirectly modify readonly property Parent_::$readonlyChild
object(Parent_)#1 (2) {
["writableChild"]=>
object(Child)#%d (1) {
["value"]=>
int(2)
}
["readonlyChild"]=>
object(Child)#%d (1) {
["value"]=>
int(1)
}
}
73 changes: 73 additions & 0 deletions Zend/tests/structs/is_identical.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
--TEST--
=== on structs
--FILE--
<?php

struct Box {
public function __construct(
public $value,
) {}
}

$a = new Box(1);
$b = new Box(2);
$c = $a;
$d = $a;
$d->value++;
$e = new Box('1');

$values = [$a, $b, $c, $d, $e];
foreach ($values as $l) {
foreach ($values as $r) {
echo var_export($l->value, true)
. ' === '
. var_export($r->value, true)
. ' => '
. ($l === $r ? 'true' : 'false')
. "\n";
}
}

#[\AllowDynamicProperties]
struct Point {}

$a = new Point();
$a->x = 1;
$a->y = 1;
$b = new Point();
$b->y = 1;
$b->x = 1;
var_dump($a === $b);
unset($b->y);
$b->y = 1;
var_dump($a === $b);

?>
--EXPECT--
1 === 1 => true
1 === 2 => false
1 === 1 => true
1 === 2 => false
1 === '1' => false
2 === 1 => false
2 === 2 => true
2 === 1 => false
2 === 2 => true
2 === '1' => false
1 === 1 => true
1 === 2 => false
1 === 1 => true
1 === 2 => false
1 === '1' => false
2 === 1 => false
2 === 2 => true
2 === 1 => false
2 === 2 => true
2 === '1' => false
'1' === 1 => false
'1' === 2 => false
'1' === 1 => false
'1' === 2 => false
'1' === '1' => true
bool(false)
bool(true)
Loading
Loading