fix(preprocessor): reject variadic promoted properties and callable property/constant types (#64) --skip-tests
* fix(preprocessor): reject variadic promoted properties and callable property/constant types
Two promotion/type gaps against Zend (probed on 8.4.13):
- `__construct(public int ...$x)` was accepted and even registered
the property before the variadic-position check ran. A variadic
parameter collects its arguments into an array, so there is no single
value to promote; Zend fatals with "Cannot declare variadic promoted
property". The check now precedes the property registration.
- `callable` is a calling-scope-dependent type, so Zend forbids it in
property types (declared, promoted, interface hooked) and class
constant types (class and interface), bare or as a nullable/union
member: "Property A::$x cannot have type ?callable" /
"Class constant A::X cannot have type callable". Intersection
members are left to the compound-type validation, which rejects
every non-class standard type there.
`void`/`never` property and parameter types were already rejected by
parseTypeDecl ("The type `void`/`never` is allowed only for return
type") - verified, no change needed; union members are covered by the
compound-type validation.
* test(preprocessor): cover promotion and property/constant type rules
* fix(preprocessor): reject callable inside intersection and DNF types
typeDeclContainsCallable() deliberately skipped IntersectionType, so a
DNF-nested callable such as `public (Traversable&callable)|stdClass
$value;` sailed past the property checks and died in gen_stub on
assert(!$type->isBuiltin); a bare `Traversable&callable` property
compiled outright.
Zend rejects callable while compiling the intersection type itself,
with its own diagnostic ("Type callable cannot be part of an
intersection type", probed on 8.4.13), in every declaration context and
ahead of the property/constant-specific bans — `callable|(Traversable&
callable)` reports the intersection conflict, not the property one.
A dedicated assertTypeDeclIntersectionsHaveNoCallable() walk (nullable,
union, intersection members) now runs before the existing
typeDeclContainsCallable() checks in all contexts this branch guards:
class properties, promoted properties (both via addClassProperty),
typed class constants, and interface properties/constants. Tests cover
the bare intersection member, DNF in first and second union member,
the promoted and constant/interface variants, and a callable-free DNF
property that must keep compiling.
* fix(preprocessor): validate callable intersections on the common type-declaration path
assertTypeDeclIntersectionsHaveNoCallable() was invoked only from the
property and class/interface-constant paths, so the same invalid type
in a function parameter or return declaration bypassed the check and
reached the later generator path:
function consume(Traversable&callable $value): void {}
function produce(): Traversable&callable {}
Zend rejects both while compiling the type itself ("Type callable
cannot be part of an intersection type", probed on 8.4.13), in every
declaration context.
The walk now lives in parseTypeDecl(), the declaration funnel behind
resolveTypeDecl() that parameters, returns, properties, promoted
properties, class and interface constants, and interface hooked
properties already flow through; the per-context calls are gone, and
the diagnostic points at the offending intersection member. Closure
and arrow-function signatures resolved no full type node anywhere, so
doGenClosure() now routes them through the same funnel - except bare
class names, which the native-object walk there already resolves (and,
inside trait methods, rewrites) via parseTypeDecl().
The property and constant paths resolve the declaration before
applying their own bare/nullable/union callable bans, so a type like
`callable|(Traversable&callable)` keeps reporting the intersection
conflict first, as Zend does.
New negative tests: parameter and return intersections, callable in a
DNF parameter member, and closure parameter and return intersections
(each probed against Zend 8.4.13); positive tests keep bare `callable`
parameters and callable-free DNF properties compiling.
master
parent
4ca9072f36
commit
f39f985e16
22 changed files with 295 additions and 3 deletions
@ -0,0 +1,5 @@ |
||||
<?php |
||||
function main() { |
||||
$f = function (Traversable&callable $x): void {}; |
||||
$f(null); |
||||
} |
||||
@ -0,0 +1,5 @@ |
||||
<?php |
||||
function main() { |
||||
$f = function (): Traversable&callable {}; |
||||
$f(); |
||||
} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { const callable FN = 1; } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { const (Traversable&callable)|int FN = 1; } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
interface Baggy { public (Traversable&callable)|stdClass $fn { get; } const (Traversable&callable)|int FN = 1; } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
function consume((Traversable&callable)|stdClass $value): void {} |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
function consume(Traversable&callable $value): void {} |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,8 @@ |
||||
<?php |
||||
function apply(callable $fn): void { |
||||
$fn(); |
||||
} |
||||
|
||||
function main() { |
||||
apply(function (): void {}); |
||||
} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { public function __construct(public int ...$items) {} } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { public callable $fn; } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { public (Traversable&callable)|stdClass $fn; } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { public function __construct(public (Traversable&callable)|stdClass $fn) {} } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { public (Countable&Traversable)|(Iterator&callable) $fn; } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { public Traversable&callable $fn; } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { public function __construct(public callable $fn) {} } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { public int|callable $fn; } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
class Bag { public (Countable&Traversable)|stdClass $it; } |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,4 @@ |
||||
<?php |
||||
function produce(): Traversable&callable {} |
||||
|
||||
function main() {} |
||||
@ -0,0 +1,105 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Constructor promotion and property/constant type restrictions: |
||||
* no variadic promoted properties, `callable` is banned from |
||||
* property and class-constant types (bare, nullable, or union member), |
||||
* and `callable` inside an intersection or DNF member is rejected in |
||||
* every declaration context - parameters, returns, properties, promoted |
||||
* properties, constants, interface members, and closures. |
||||
*/ |
||||
class PromotionAndPropertyTypeTest extends BaseTest |
||||
{ |
||||
public function testVariadicPromotedPropertyIsRejected(): void |
||||
{ |
||||
$this->exec('Cannot declare variadic promoted property', 'promotion_rule_variadic.php'); |
||||
} |
||||
|
||||
public function testCallablePropertyTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Property `Bag::$fn` cannot have type `callable`', 'property_rule_callable.php'); |
||||
} |
||||
|
||||
public function testCallablePromotedPropertyTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Property `Bag::$fn` cannot have type `callable`', 'property_rule_callable_promoted.php'); |
||||
} |
||||
|
||||
public function testCallableUnionPropertyTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Property `Bag::$fn` cannot have type `int|callable`', 'property_rule_callable_union.php'); |
||||
} |
||||
|
||||
public function testCallableClassConstantTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Class constant `Bag::FN` cannot have type `callable`', 'const_rule_callable.php'); |
||||
} |
||||
|
||||
public function testCallableInBareIntersectionIsRejected(): void |
||||
{ |
||||
// Zend rejects callable while compiling the intersection type itself, |
||||
// with a dedicated diagnostic; without this check the type reaches |
||||
// gen_stub, which asserts intersection members are never builtin. |
||||
$this->exec('Type callable cannot be part of an intersection type', 'property_rule_callable_intersection.php'); |
||||
} |
||||
|
||||
public function testCallableInDnfPropertyTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'property_rule_callable_dnf.php'); |
||||
} |
||||
|
||||
public function testCallableInSecondDnfMemberIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'property_rule_callable_dnf_second_member.php'); |
||||
} |
||||
|
||||
public function testCallableInDnfPromotedPropertyTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'property_rule_callable_dnf_promoted.php'); |
||||
} |
||||
|
||||
public function testCallableInDnfClassConstantTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'const_rule_callable_dnf.php'); |
||||
} |
||||
|
||||
public function testCallableInDnfInterfaceMemberTypesIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'interface_rule_callable_dnf.php'); |
||||
} |
||||
|
||||
public function testCallableInIntersectionParameterTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'param_rule_callable_intersection.php'); |
||||
} |
||||
|
||||
public function testCallableInDnfParameterTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'param_rule_callable_dnf.php'); |
||||
} |
||||
|
||||
public function testCallableInIntersectionReturnTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'return_rule_callable_intersection.php'); |
||||
} |
||||
|
||||
public function testCallableInIntersectionClosureParameterTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'closure_rule_callable_intersection_param.php'); |
||||
} |
||||
|
||||
public function testCallableInIntersectionClosureReturnTypeIsRejected(): void |
||||
{ |
||||
$this->exec('Type callable cannot be part of an intersection type', 'closure_rule_callable_intersection_return.php'); |
||||
} |
||||
|
||||
public function testCallableFreeDnfPropertyTypeStillCompiles(): void |
||||
{ |
||||
$this->compile('property_rule_dnf_valid.php'); |
||||
} |
||||
|
||||
public function testBareCallableParameterTypeStillCompiles(): void |
||||
{ |
||||
$this->compile('param_rule_callable_valid.php'); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue