Skip to content

Commit 78d7959

Browse files
ondrejmirtesclaude
andcommitted
Reset every memo property of a scope natively cloned by ScopeOps::scopeWith()
The native scopeWith() clones the scope instead of building a new one through the factory like the twin's duplicateWith(), and reset a hand-kept list of memo properties on the clone. NodeCallbackScope's askedTypes/askedNativeTypes memos were missing from it, so a scope a rule derived from its callback scope (invalidateExpression(), mergeWith(), ...) answered a synthetic node from the callback scope's memo. The list also named truthyScopes/falseyScopes, which no longer exist. The reset list is now derived from the class: every typed instance property with a declared default is reset to that default — exactly the properties a freshly constructed scope starts with (promoted properties have no default, and the untyped promoted $nodeCallback is excluded by the typed condition). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 1b6afc0 commit 78d7959

3 files changed

Lines changed: 66 additions & 46 deletions

File tree

‎turbo-ext/src/ScopeOps.cpp‎

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,48 @@ typedef struct {
3232
int32_t in_function_calls_stack;
3333
int32_t in_first_level_statement;
3434
int32_t after_extract_call;
35-
/* memo props to reset (missing => -1) */
36-
int32_t resolved_types;
37-
int32_t truthy_scopes;
38-
int32_t falsey_scopes;
39-
int32_t node_callback_scope;
40-
int32_t scope_out_of_first_level;
41-
int32_t scope_with_promoted_native;
42-
int32_t walk_scope; /* NodeCallbackScope */
43-
int32_t seeded_walk_scope; /* NodeCallbackScope */
44-
int32_t truthy_value_exprs; /* NodeCallbackScope */
45-
int32_t falsey_value_exprs; /* NodeCallbackScope */
35+
/* the per-instance memos to reset: every typed instance property with a
36+
* declared default — the twin builds a new scope, where each of them
37+
* starts at that default. A promoted property never has one (typed; the
38+
* untyped promoted $nodeCallback's implicit null is why untyped ones do
39+
* not count), and the constructor assigns only default-less $namespace */
40+
uint32_t memo_count;
41+
uint32_t *memo_offsets;
4642
} pt_scope_offsets;
4743

4844
static HashTable pt_scope_offsets_cache;
4945
static bool pt_scope_offsets_cache_inited = false;
5046

5147
static void pt_scope_offsets_free(zval *zv)
5248
{
53-
efree(Z_PTR_P(zv));
49+
pt_scope_offsets *off = (pt_scope_offsets *) Z_PTR_P(zv);
50+
if (off->memo_offsets != NULL) efree(off->memo_offsets);
51+
efree(off);
52+
}
53+
54+
/* collects the memo slots of ce (see pt_scope_offsets::memo_offsets) — every
55+
* declaring class of the chain, so a parent's private memos count too */
56+
static void pt_scope_offsets_collect_memos(zend_class_entry *ce, pt_scope_offsets *off)
57+
{
58+
off->memo_count = 0;
59+
off->memo_offsets = ce->default_properties_count > 0
60+
? (uint32_t *) safe_emalloc((size_t) ce->default_properties_count, sizeof(uint32_t), 0)
61+
: NULL;
62+
for (zend_class_entry *declaring = ce; declaring != NULL; declaring = declaring->parent) {
63+
for (auto entry : zv::TableRef(&declaring->properties_info)) {
64+
zend_property_info *info = (zend_property_info *) Z_PTR_P(entry.value().raw());
65+
if (info->ce != declaring || (info->flags & ZEND_ACC_STATIC) != 0 || !ZEND_TYPE_IS_SET(info->type)) continue;
66+
if (Z_TYPE(CE_DEFAULT_PROPERTIES_TABLE(ce)[OBJ_PROP_TO_NUM(info->offset)]) == IS_UNDEF) continue;
67+
bool seen = false;
68+
for (uint32_t i = 0; i < off->memo_count; i++) {
69+
if (off->memo_offsets[i] == info->offset) {
70+
seen = true;
71+
break;
72+
}
73+
}
74+
if (!seen && off->memo_count < (uint32_t) ce->default_properties_count) off->memo_offsets[off->memo_count++] = info->offset;
75+
}
76+
}
5477
}
5578

5679
static pt_scope_offsets *pt_scope_offsets_for(zend_class_entry *ce)
@@ -73,16 +96,7 @@ static pt_scope_offsets *pt_scope_offsets_for(zend_class_entry *ce)
7396
off->in_function_calls_stack = pt_instance_prop_offset(ce, "inFunctionCallsStack", sizeof("inFunctionCallsStack") - 1);
7497
off->in_first_level_statement = pt_instance_prop_offset(ce, "inFirstLevelStatement", sizeof("inFirstLevelStatement") - 1);
7598
off->after_extract_call = pt_instance_prop_offset(ce, "afterExtractCall", sizeof("afterExtractCall") - 1);
76-
off->resolved_types = pt_instance_prop_offset(ce, "resolvedTypes", sizeof("resolvedTypes") - 1);
77-
off->truthy_scopes = pt_instance_prop_offset(ce, "truthyScopes", sizeof("truthyScopes") - 1);
78-
off->falsey_scopes = pt_instance_prop_offset(ce, "falseyScopes", sizeof("falseyScopes") - 1);
79-
off->node_callback_scope = pt_instance_prop_offset(ce, "nodeCallbackScope", sizeof("nodeCallbackScope") - 1);
80-
off->scope_out_of_first_level = pt_instance_prop_offset(ce, "scopeOutOfFirstLevelStatement", sizeof("scopeOutOfFirstLevelStatement") - 1);
81-
off->scope_with_promoted_native = pt_instance_prop_offset(ce, "scopeWithPromotedNativeTypes", sizeof("scopeWithPromotedNativeTypes") - 1);
82-
off->walk_scope = pt_instance_prop_offset(ce, "walkScope", sizeof("walkScope") - 1);
83-
off->seeded_walk_scope = pt_instance_prop_offset(ce, "seededWalkScope", sizeof("seededWalkScope") - 1);
84-
off->truthy_value_exprs = pt_instance_prop_offset(ce, "truthyValueExprs", sizeof("truthyValueExprs") - 1);
85-
off->falsey_value_exprs = pt_instance_prop_offset(ce, "falseyValueExprs", sizeof("falseyValueExprs") - 1);
99+
pt_scope_offsets_collect_memos(ce, off);
86100

87101
zend_hash_add_ptr(&pt_scope_offsets_cache, ce->name, off);
88102
return off;
@@ -258,16 +272,14 @@ class ScopeOps
258272
cloneObj.propAtOffset((uint32_t) off->after_extract_call).assign(zv::Val::boolean(afterExtractCall));
259273

260274
/* fresh-constructor defaults for per-instance memos */
261-
resetToEmptyArray(cloneObj, off->resolved_types);
262-
resetToEmptyArray(cloneObj, off->truthy_scopes);
263-
resetToEmptyArray(cloneObj, off->falsey_scopes);
264-
resetToNull(cloneObj, off->node_callback_scope);
265-
resetToNull(cloneObj, off->scope_out_of_first_level);
266-
resetToNull(cloneObj, off->scope_with_promoted_native);
267-
resetToNull(cloneObj, off->walk_scope);
268-
resetToNull(cloneObj, off->seeded_walk_scope);
269-
resetToEmptyArray(cloneObj, off->truthy_value_exprs);
270-
resetToEmptyArray(cloneObj, off->falsey_value_exprs);
275+
zval *defaults = CE_DEFAULT_PROPERTIES_TABLE(Z_OBJCE_P(scope));
276+
for (uint32_t i = 0; i < off->memo_count; i++) {
277+
uint32_t offset = off->memo_offsets[i];
278+
/* the engine's own default-property copy (a persistent default is duplicated) */
279+
zval fresh;
280+
ZVAL_COPY_OR_DUP(&fresh, &defaults[OBJ_PROP_TO_NUM(offset)]);
281+
cloneObj.propAtOffset(offset).assign(zv::Val::adopt(fresh));
282+
}
271283

272284
zval out;
273285
ZVAL_OBJ(&out, clone);
@@ -1159,20 +1171,6 @@ class ScopeOps
11591171
obj.propAtOffset((uint32_t) offset).assign(zv::Arr::copyOfTable(value));
11601172
}
11611173

1162-
/* $obj->prop = [] — a memo reset to the fresh-constructor default */
1163-
static void resetToEmptyArray(zv::ObjRef obj, int32_t offset)
1164-
{
1165-
if (offset < 0) return;
1166-
obj.propAtOffset((uint32_t) offset).assign(zv::Arr::empty());
1167-
}
1168-
1169-
/* $obj->prop = null — a memo reset to the fresh-constructor default */
1170-
static void resetToNull(zv::ObjRef obj, int32_t offset)
1171-
{
1172-
if (offset < 0) return;
1173-
obj.propAtOffset((uint32_t) offset).assign(zv::Val::null());
1174-
}
1175-
11761174
/* $holder->expr for a checked ExpressionTypeHolder */
11771175
static zend_object *holderExpr(zv::Ref holder)
11781176
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace WalkTraceCallbackScopeMemoReset;
4+
5+
// a rule deriving a scope from its callback scope (invalidateExpression(),
6+
// assignExpression(), mergeWith() ...) must get one that answers afresh: the
7+
// callback scope memoizes asked types by node, and the derived scope is a new
8+
// object in the twin, so the memo must not travel with the native clone
9+
function (\stdClass $o): void {
10+
if ($o->p === 1) {
11+
echo $o->p;
12+
}
13+
};

‎turbo-ext/tests/walk-trace.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ function walkTraceContainer(string $root, string $mode, array $analysedFiles): C
151151
if ($node instanceof Node\Expr\FuncCall || $node instanceof Node\Expr\MethodCall) {
152152
$line .= ' P=' . $scope->pushInFunctionCall(null, null, false)->popInFunctionCall()->getType($node)->describe($precise);
153153
}
154+
if ($node instanceof Node\Expr\PropertyFetch) {
155+
// a scope a rule derives from the callback scope answers afresh:
156+
// the callback scope's asked-type memo (keyed by node, so a
157+
// synthetic node the walk never stored) stays behind
158+
$synthetic = clone $node;
159+
$line .= ' Y=' . $scope->getType($synthetic)->describe($precise)
160+
. ' I=' . $scope->invalidateExpression($synthetic)->getType($synthetic)->describe($precise)
161+
. ' M=' . $scope->mergeWith($scope->assignExpression($synthetic, new \PHPStan\Type\StringType(), new \PHPStan\Type\StringType()))->getType($synthetic)->describe($precise);
162+
}
154163
}
155164
} elseif ($node instanceof Node\Stmt && $scope instanceof MutatingScope) {
156165
$line .= ' S=' . json_encode($scope->debug(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);

0 commit comments

Comments
 (0)