Skip to content

Commit 9ca590d

Browse files
committed
gen_stub: fix invalid C variable name for namespaced types in union/intersection type list
When generating a union or intersection type list with multiple class types, the variable holding each zend_string* was declared using toVarEscapedName() (backslashes replaced by underscores), but the subsequent ZEND_TYPE_INIT_CLASS() reference used toEscapedName() (backslashes escaped as \\), producing an invalid C identifier.
1 parent e60f880 commit 9ca590d

File tree

7 files changed

+103
-8
lines changed

7 files changed

+103
-8
lines changed

build/gen_stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,8 +2375,8 @@ protected function getTypeCode(string $variableLikeName, string &$code): string
23752375
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->num_types = $classTypeCount;\n";
23762376

23772377
foreach ($arginfoType->classTypes as $k => $classType) {
2378-
$escapedClassName = $classType->toEscapedName();
2379-
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->types[$k] = (zend_type) ZEND_TYPE_INIT_CLASS({$variableLikeType}_{$variableLikeName}_class_{$escapedClassName}, 0, 0);\n";
2378+
$varEscapedClassName = $classType->toVarEscapedName();
2379+
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->types[$k] = (zend_type) ZEND_TYPE_INIT_CLASS({$variableLikeType}_{$variableLikeName}_class_{$varEscapedClassName}, 0, 0);\n";
23802380
}
23812381

23822382
$typeMaskCode = $this->type->toArginfoType()->toTypeMask();

ext/zend_test/test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static zend_class_entry *zend_test_forbid_dynamic_call;
6969
static zend_class_entry *zend_test_ns_foo_class;
7070
static zend_class_entry *zend_test_ns_unlikely_compile_error_class;
7171
static zend_class_entry *zend_test_ns_not_unlikely_compile_error_class;
72+
static zend_class_entry *zend_test_ns_bar_class;
7273
static zend_class_entry *zend_test_ns2_foo_class;
7374
static zend_class_entry *zend_test_ns2_ns_foo_class;
7475
static zend_class_entry *zend_test_unit_enum;
@@ -1571,6 +1572,7 @@ PHP_MINIT_FUNCTION(zend_test)
15711572
zend_test_ns_foo_class = register_class_ZendTestNS_Foo();
15721573
zend_test_ns_unlikely_compile_error_class = register_class_ZendTestNS_UnlikelyCompileError();
15731574
zend_test_ns_not_unlikely_compile_error_class = register_class_ZendTestNS_NotUnlikelyCompileError();
1575+
zend_test_ns_bar_class = register_class_ZendTestNS_Bar();
15741576
zend_test_ns2_foo_class = register_class_ZendTestNS2_Foo();
15751577
zend_test_ns2_ns_foo_class = register_class_ZendTestNS2_ZendSubNS_Foo();
15761578

ext/zend_test/test.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ class Foo {
363363
public function method(): int {}
364364
}
365365

366+
interface Bar {}
367+
366368
class UnlikelyCompileError {
367369
/* This method signature would create a compile error due to the string
368370
* "ZendTestNS\UnlikelyCompileError" in the generated macro call */
@@ -383,6 +385,8 @@ public function method(): ?NotUnlikelyCompileError {}
383385

384386
class Foo {
385387
public ZendSubNS\Foo $foo;
388+
public ZendSubNS\Foo&\ZendTestNS\Bar $intersectionProp;
389+
public ZendSubNS\Foo|\ZendTestNS\Bar $unionProp;
386390

387391
public function method(): void {}
388392
}

ext/zend_test/test_arginfo.h

Lines changed: 37 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/zend_test/test_decl.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/zend_test/test_legacy_arginfo.h

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
gen_stub.php: union and intersection type properties with namespaced classes
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
$rp = new ReflectionProperty(ZendTestNS2\Foo::class, 'intersectionProp');
9+
$type = $rp->getType();
10+
11+
var_dump($type instanceof ReflectionIntersectionType);
12+
foreach ($type->getTypes() as $t) {
13+
var_dump($t->getName());
14+
}
15+
16+
$rp = new ReflectionProperty(ZendTestNS2\Foo::class, 'unionProp');
17+
$type = $rp->getType();
18+
19+
var_dump($type instanceof ReflectionUnionType);
20+
foreach ($type->getTypes() as $t) {
21+
var_dump($t->getName());
22+
}
23+
24+
?>
25+
--EXPECT--
26+
bool(true)
27+
string(25) "ZendTestNS2\ZendSubNS\Foo"
28+
string(14) "ZendTestNS\Bar"
29+
bool(true)
30+
string(25) "ZendTestNS2\ZendSubNS\Foo"
31+
string(14) "ZendTestNS\Bar"

0 commit comments

Comments
 (0)