Skip to content

Commit c5c78d8

Browse files
ondrejmirtesclaude
andcommitted
Declare the native NodeTraverser's properties exactly like php-parser's
The native class declared $visitors and $stopTraversal by hand, untyped, with stopTraversal defaulting to false, while php-parser has `protected array $visitors = []` and `protected bool $stopTraversal` (typed, uninitialized until traverse()). A PHP subclass redeclaring `protected array $visitors` was a fatal error natively, and reading stopTraversal before traverse() returned false instead of throwing. Declare them through the generated declareProperties(), clear IS_PROP_UNINIT when traverse() writes stopTraversal, and let traverse()/removeVisitor() on an unset $visitors throw the engine's uninitialized-property Error like the twin's read does. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent a027f78 commit c5c78d8

2 files changed

Lines changed: 108 additions & 15 deletions

File tree

‎turbo-ext/src/NodeTraverser.cpp‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,11 @@ class NodeTraverser
243243
zv::ArrRef(prop.raw()).push(visitor);
244244
}
245245

246-
void removeVisitor(zv::Ref visitor)
246+
/* false means it threw */
247+
bool removeVisitor(zv::Ref visitor)
247248
{
248249
zv::Ref prop = visitorsProp();
249-
if (!prop.isArray()) return;
250+
if (UNEXPECTED(!prop.isArray())) return throwVisitorsUninitialized();
250251

251252
/* array_search() with loose comparison, like the PHP implementation */
252253
bool found = false;
@@ -261,7 +262,7 @@ class NodeTraverser
261262
}
262263
pos++;
263264
}
264-
if (!found) return;
265+
if (!found) return true;
265266

266267
/* array_splice($visitors, $index, 1, []) — reindexes */
267268
zv::ArrRef old(prop.raw());
@@ -273,15 +274,14 @@ class NodeTraverser
273274
}
274275
}
275276
prop.assign(std::move(rebuilt));
277+
return true;
276278
}
277279

278280
/* traverse(); UNDEF result means a pending exception */
279281
zv::Val traverse(HashTable *nodesTable)
280282
{
281-
zv::ObjRef selfObj(self);
282-
283283
/* $this->stopTraversal = false */
284-
selfObj.propAtWrite(slots::stopTraversal, zv::Val::boolean(false));
284+
writeStopTraversal(false);
285285

286286
if (UNEXPECTED(!buildVisitorPlan())) return zv::Val();
287287

@@ -323,7 +323,7 @@ class NodeTraverser
323323
}
324324

325325
/* persist stopTraversal like the PHP implementation */
326-
selfObj.propAtWrite(slots::stopTraversal, zv::Val::boolean(stop));
326+
writeStopTraversal(stop);
327327

328328
return zv::Val(std::move(nodes));
329329
}
@@ -729,10 +729,7 @@ class NodeTraverser
729729
bool buildVisitorPlan()
730730
{
731731
zv::Ref visitors = visitorsProp();
732-
if (UNEXPECTED(!visitors.isArray())) {
733-
zend_throw_error(NULL, "phpstan_turbo: NodeTraverser visitors is not an array");
734-
return false;
735-
}
732+
if (UNEXPECTED(!visitors.isArray())) return throwVisitorsUninitialized();
736733

737734
zend_function *baseEnter = NULL;
738735
zend_function *baseLeave = NULL;
@@ -832,6 +829,27 @@ class NodeTraverser
832829
return zv::ObjRef(self).propAt(slots::visitors).deref();
833830
}
834831

832+
/* the typed property is an array unless a subclass unset() it: reading
833+
* it then throws the engine's "must not be accessed before
834+
* initialization" Error, as the twin's read does; always false */
835+
bool throwVisitorsUninitialized() const
836+
{
837+
zval rv;
838+
zend_read_property(self->ce, self, "visitors", sizeof("visitors") - 1, false, &rv);
839+
if (!EG(exception)) {
840+
zend_throw_error(NULL, "phpstan_turbo: NodeTraverser visitors is not an array");
841+
}
842+
return false;
843+
}
844+
845+
/* $this->stopTraversal = $value — a typed property the constructor
846+
* leaves uninitialized, so the write also clears IS_PROP_UNINIT */
847+
void writeStopTraversal(bool value)
848+
{
849+
zv::ObjRef(self).propAtWrite(slots::stopTraversal, zv::Val::boolean(value));
850+
Z_PROP_FLAG_P(OBJ_PROP_NUM(self, slots::stopTraversal)) = 0;
851+
}
852+
835853
zend_object *self;
836854
pt_visitor_plan *plan = NULL;
837855
uint32_t nvisitors = 0;
@@ -855,9 +873,10 @@ void pt_register_node_traverser()
855873
{
856874
reg::Class cls("PhpParser\\NodeTraverser");
857875
ptdecl::NodeTraverser::declareClass(cls);
858-
/* "visitors" must stay slot 0 and "stopTraversal" slot 1 (PT_NT_PROP_*) */
859-
cls.protectedArrayProperty("visitors");
860-
cls.protectedBoolProperty("stopTraversal", false);
876+
/* `protected array $visitors = []` and `protected bool $stopTraversal`
877+
* (typed, uninitialized until traverse()), exactly the twin's: a
878+
* subclass may redeclare them */
879+
ptdecl::NodeTraverser::declareProperties(cls);
861880

862881
cls.classConstantLong("DONT_TRAVERSE_CHILDREN", NodeTraverser::DONT_TRAVERSE_CHILDREN);
863882
cls.classConstantLong("STOP_TRAVERSAL", NodeTraverser::STOP_TRAVERSAL);
@@ -883,7 +902,7 @@ void pt_register_node_traverser()
883902
cls.method(sigs::removeVisitor, [](INTERNAL_FUNCTION_PARAMETERS) {
884903
zval *visitor;
885904
if (!zp::parse<zp::Obj>(execute_data, visitor)) RETURN_THROWS();
886-
NodeTraverser(Z_OBJ_P(ZEND_THIS)).removeVisitor(zv::Ref(visitor));
905+
if (UNEXPECTED(!NodeTraverser(Z_OBJ_P(ZEND_THIS)).removeVisitor(zv::Ref(visitor)))) RETURN_THROWS();
887906
});
888907

889908
cls.method(sigs::traverse, [](INTERNAL_FUNCTION_PARAMETERS) {

‎turbo-ext/tests/parser-visitors.php‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,77 @@ function f() {
170170
);
171171
}
172172
}
173+
174+
// ---- NodeTraverser semantics beyond the visitor ports ----
175+
// Each probe runs once against php-parser's NodeTraverser and once against
176+
// the native one (a subclass of each is declared from the same source), and
177+
// the observations must be identical modulo the prefix.
178+
$ntNormalize = static fn (string $s): string => str_replace(['PHPStanTurbo\\NodeTraverser', '_native'], ['PhpParser\\NodeTraverser', '_php'], $s);
179+
$ntObserve = static function (callable $probe) use ($ntNormalize): array {
180+
try {
181+
$result = $probe();
182+
} catch (\Throwable $e) {
183+
return ['threw', get_class($e), $ntNormalize($e->getMessage())];
184+
}
185+
186+
return ['returned', $ntNormalize(serialize($result))];
187+
};
188+
$ntSides = ['php' => \PhpParser\NodeTraverser::class, 'native' => \PHPStanTurbo\NodeTraverser::class];
189+
$ntCompare = static function (string $label, callable $probeForSide) use ($ntSides, $ntObserve): void {
190+
$observed = [];
191+
foreach ($ntSides as $side => $traverserClass) {
192+
$observed[$side] = $ntObserve(static fn () => $probeForSide($side, $traverserClass));
193+
}
194+
check($observed['php'] === $observed['native'], "NodeTraverser: $label: " . json_encode($observed));
195+
};
196+
197+
// the properties are declared like the twin's: `protected array $visitors = []`
198+
// and `protected bool $stopTraversal` (typed, uninitialized until traverse())
199+
foreach ($ntSides as $side => $traverserClass) {
200+
eval(sprintf(
201+
'final class NtRedeclaring_%1$s extends \\%2$s { protected array $visitors = []; protected bool $stopTraversal = false; public function stop(): bool { return $this->stopTraversal; } }
202+
final class NtProbing_%1$s extends \\%2$s { public function stop(): bool { return $this->stopTraversal; } public function dropVisitors(): void { unset($this->visitors); } }',
203+
$side,
204+
$traverserClass,
205+
));
206+
}
207+
foreach (['visitors', 'stopTraversal'] as $ntProperty) {
208+
$ntCompare("declaration of \$$ntProperty", static function (string $side, string $traverserClass) use ($ntProperty): array {
209+
$property = new \ReflectionProperty($traverserClass, $ntProperty);
210+
return [(string) $property->getType(), $property->hasDefaultValue(), $property->getDefaultValue(), $property->isProtected()];
211+
});
212+
}
213+
$ntCompare('a subclass redeclaring the typed properties', static function (string $side): array {
214+
$class = 'NtRedeclaring_' . $side;
215+
$traverser = new $class();
216+
return [$traverser->stop(), $traverser->traverse([]), $traverser->stop()];
217+
});
218+
$ntCompare('stopTraversal before traverse()', static function (string $side): bool {
219+
$class = 'NtProbing_' . $side;
220+
return (new $class())->stop();
221+
});
222+
$ntCompare('stopTraversal after traverse()', static function (string $side): bool {
223+
$class = 'NtProbing_' . $side;
224+
$traverser = new $class();
225+
$traverser->traverse([]);
226+
return $traverser->stop();
227+
});
228+
$ntCompare('traverse() with $visitors unset', static function (string $side): array {
229+
$class = 'NtProbing_' . $side;
230+
$traverser = new $class();
231+
$traverser->dropVisitors();
232+
return $traverser->traverse([]);
233+
});
234+
$ntCompare('removeVisitor() with $visitors unset', static function (string $side): void {
235+
$class = 'NtProbing_' . $side;
236+
$traverser = new $class();
237+
$traverser->dropVisitors();
238+
$traverser->removeVisitor(new \PhpParser\NodeVisitor\NameResolver());
239+
});
240+
$ntCompare('addVisitor() with $visitors unset', static function (string $side): array {
241+
$class = 'NtProbing_' . $side;
242+
$traverser = new $class();
243+
$traverser->dropVisitors();
244+
$traverser->addVisitor(new \PhpParser\NodeVisitor\NodeConnectingVisitor());
245+
return $traverser->traverse([new \PhpParser\Node\Stmt\Nop()]);
246+
});

0 commit comments

Comments
 (0)