Skip to content
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
17 changes: 15 additions & 2 deletions Zend/Optimizer/zend_inference.c
Original file line number Diff line number Diff line change
Expand Up @@ -2275,8 +2275,21 @@ static uint32_t binary_op_result_type(
/* Handle potentially overloaded operators.
* This could be made more precise by checking the class type, if known. */
if ((t1_type & MAY_BE_OBJECT) || (t2_type & MAY_BE_OBJECT)) {
/* This is somewhat GMP specific. */
tmp |= MAY_BE_OBJECT | MAY_BE_FALSE | MAY_BE_RC1;
switch (opcode) {
case ZEND_ADD:
case ZEND_SUB:
case ZEND_MUL:
case ZEND_DIV:
case ZEND_MOD:
case ZEND_POW:
/* Operable methods may return any non-void/never type. */
tmp |= MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_RC1 | MAY_BE_RCN;
break;
default:
/* This is somewhat GMP specific. */
tmp |= MAY_BE_OBJECT | MAY_BE_FALSE | MAY_BE_RC1;
break;
}
}
}

Expand Down
34 changes: 34 additions & 0 deletions Zend/tests/operable/anon_class_alias.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Anonymous Operable class with explicit final operator method works and can be safely aliased
--FILE--
<?php

$a = new readonly class(10) implements Operable {
public function __construct(public int $v) {}

final public function __add(int $x): static {
return new static($this->v + $x);
}
};

var_dump(($a + 5)->v);

class_alias(get_class($a), 'AliasedAnon');
eval('readonly class SubAnon extends AliasedAnon {}');

$sub = new SubAnon(20);
var_dump(($sub + 7)->v);

$rmAnon = new ReflectionMethod($a, '__add');
$rmSub = new ReflectionMethod(SubAnon::class, '__add');
var_dump($rmAnon->isOperatorMethod(), $rmAnon->isFinal());
var_dump($rmSub->isOperatorMethod(), $rmSub->isFinal());

?>
--EXPECT--
int(15)
int(27)
bool(true)
bool(true)
bool(true)
bool(true)
12 changes: 12 additions & 0 deletions Zend/tests/operable/error_abstract_non_readonly.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Non-readonly abstract class cannot implement Operable
--FILE--
<?php

abstract class BadAbstract implements Operable {
abstract public function __add(int $x): self;
}

?>
--EXPECTF--
Fatal error: Class BadAbstract implements Operable and must be declared readonly in %s on line %d
22 changes: 22 additions & 0 deletions Zend/tests/operable/error_anon_non_final.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Anonymous Operable class must declare concrete operator methods final (blocks class_alias override)
--FILE--
<?php

$a = new readonly class implements Operable {
public function __add(int $x): self {
return $this;
}
};

class_alias(get_class($a), 'SneakyBase');
eval('readonly class Evil extends SneakyBase {}');
eval('final readonly class EvilGrand extends Evil {
public function __add(int $x): self {
return $this;
}
}');

?>
--EXPECTF--
Fatal error: Operator method Operable@anonymous::__add() on non-final class must be declared final in %s on line 3
12 changes: 12 additions & 0 deletions Zend/tests/operable/error_backed_enum.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Backed enum cannot implement Operable
--FILE--
<?php

enum BadBackedEnum: int implements Operable {
case A = 1;
}

?>
--EXPECTF--
Fatal error: Enum BadBackedEnum cannot implement Operable in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_default_param.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator method parameters cannot have default values
--FILE--
<?php

readonly class Bad implements Operable {
public function __mod(int $x, OperandSide $side = OperandSide::Left): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__mod() parameters cannot have default values in %s on line %d
22 changes: 22 additions & 0 deletions Zend/tests/operable/error_early_bind_subclass.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Early-bound subclass of Operable parent is validated for operator methods
--FILE--
<?php

abstract readonly class BaseOp implements Operable {
abstract public function __add(int $x): self;
}

final readonly class BadChildOp extends BaseOp {
public function __add(int $x): self {
return $this;
}

public function __mul(string $bad): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method BadChildOp::__mul() has an unsupported operand parameter type in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/operable/error_enum.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Unit enum cannot implement Operable
--FILE--
<?php

enum BadEnum implements Operable {
case A;
}

?>
--EXPECTF--
Fatal error: Enum BadEnum cannot implement Operable in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/operable/error_interface_invalid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Invalid operator method declared on interface extending Operable is rejected
--FILE--
<?php

interface BadInterface extends Operable {
public function __mul(array $a): self;
}

?>
--EXPECTF--
Fatal error: Operator method BadInterface::__mul() has an unsupported operand parameter type in %s on line %d
23 changes: 23 additions & 0 deletions Zend/tests/operable/error_non_operable_parent_grand_override.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Grandchild cannot override final operator method inherited through Operable child from non-Operable base
--FILE--
<?php

readonly class Base {
final public function __add(int $x): self {
return $this;
}
}

readonly class Child extends Base implements Operable {
}

readonly class Grand extends Child {
public function __add(int $x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Cannot override final method Base::__add() in %s on line %d
17 changes: 17 additions & 0 deletions Zend/tests/operable/error_non_operable_parent_invalid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Non-Operable parent cannot smuggle invalid operator method into Operable child
--FILE--
<?php

readonly class Base {
public function &__add(int $x): self {
return $this;
}
}

final readonly class Child extends Base implements Operable {
}

?>
--EXPECTF--
Fatal error: Operator method Child::__add() cannot return by reference in %s on line %d
20 changes: 20 additions & 0 deletions Zend/tests/operable/error_non_operable_parent_non_final.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Operable class cannot inherit non-final concrete operator method from non-Operable ancestor
--FILE--
<?php

readonly class Base1 {
public function __add(int $x): self {
return $this;
}
}

readonly class Base2 extends Base1 {
}

readonly class Child extends Base2 implements Operable {
}

?>
--EXPECTF--
Fatal error: Operable class Child cannot inherit non-final operator method Base1::__add() in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_non_readonly.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Non-readonly class cannot implement Operable
--FILE--
<?php

class Bad implements Operable {
public function __add(int $x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Class Bad implements Operable and must be declared readonly in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_override_final.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Concrete operator method on non-final Operable class must be declared final
--FILE--
<?php

readonly class ParentOp implements Operable {
public function __add(int $x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method ParentOp::__add() on non-final class must be declared final in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_param_by_ref.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator method first parameter cannot be passed by reference
--FILE--
<?php

final readonly class Bad implements Operable {
public function __add(int &$x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__add() parameters cannot be passed by reference in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_param_iterable.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator method rejects iterable operand parameter type
--FILE--
<?php

readonly class Bad implements Operable {
public function __add(iterable $x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__add() has an unsupported operand parameter type in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_param_mixed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator method rejects mixed operand parameter type
--FILE--
<?php

readonly class Bad implements Operable {
public function __add(mixed $x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__add() has an unsupported operand parameter type in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_param_nullable.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator method rejects nullable operand parameter type
--FILE--
<?php

readonly class Bad implements Operable {
public function __add(?int $x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__add() has an unsupported operand parameter type in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_param_object.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator method rejects object operand parameter type
--FILE--
<?php

readonly class Bad implements Operable {
public function __add(object $x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__add() has an unsupported operand parameter type in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_param_string.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator method rejects string operand parameter type
--FILE--
<?php

readonly class Bad implements Operable {
public function __mul(string $s): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__mul() has an unsupported operand parameter type in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_param_union_bool.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator method rejects disallowed primitive inside union type
--FILE--
<?php

readonly class Bad implements Operable {
public function __add(int|bool $x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__add() has an unsupported operand parameter type in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_return_by_ref.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator methods cannot return by reference
--FILE--
<?php

final readonly class Bad implements Operable {
public function &__add(int $x): self {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__add() cannot return by reference in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/operable/error_return_missing.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Operator method must declare a return type
--FILE--
<?php

readonly class Bad implements Operable {
public function __add(int $x) {
return $this;
}
}

?>
--EXPECTF--
Fatal error: Operator method Bad::__add() must declare a return type in %s on line %d
Loading
Loading