fix(state): share storage key between state init and named-sub capture#531
Merged
fix(state): share storage key between state init and named-sub capture#531
Conversation
A `state` variable declared in a block at top level (no enclosing sub) and referenced by a named sub defined in the same block was reading an uninitialized scalar: the sub saw undef even though the outer block had set the value. Root cause: the persistent-variable key differed between the two sides. - The `state $x = ...` initializer stores the value in global storage using `PersistentVariable.beginVariable(sigilNode.id, name)` where `sigilNode.id` is assigned by `OperatorParser` for `state` decls. - When a named sub captures the same lexical, `SubroutineParser` allocated a *separate* begin id via `RuntimeCode.evalBeginIds.computeIfAbsent(ast, k -> classCounter++)`, producing a different global key. The sub's captured field was thus a fresh empty RuntimeScalar. Fix: in the named-sub capture path, reuse the state variable's existing `ast.id` as the begin id (and register it in `evalBeginIds`). As a secondary safety net, `StateVariable.retrieveStateScalar/Array/ Hash` now fall back to global storage when the per-sub map has no entry, so a sub accessing a state var declared in an outer (non-sub) scope sees the shared storage even if capture didn't wire it up. Before this fix, `jcpan -t QRCode::Encoder` failed in `t/01-hello.t`, `t/02-best.t`, `t/03-version-7.t`: `state $table = [...]` in the block-scoped `QRSpec` helpers was undef inside `qrspec_data_size` and friends, producing "no suitable version", "not enough bits, wrong version?", and a cascade of `Use of uninitialized value` warnings. After the fix, QRCode::Encoder's full test suite passes. Generated with [Devin](https://cli.devin.ai/docs) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
3a0525b to
0f8b355
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix a bug where a
statevariable declared in a block at top level (no enclosing sub) and referenced by a named sub defined in the same block was read as undef inside the sub, even after the outer block had initialized it.Found while investigating
jcpan -t QRCode::Encoder, which failed across 3 test files becauseQRCode::Encoder::QRSpecuses this pattern heavily:{ state $table = [ ...big data... ]; sub qrspec_data_size ($version, $level) { my $item = $table->[$version - 1]; # $table was undef! return $item->{words} - $item->{ec}{$level}; } }Root cause
The persistent-variable key differed between the two sides:
state $x = ...stores the value in global storage underPersistentVariable.beginVariable(sigilNode.id, name), wheresigilNode.idis assigned byOperatorParserforstatedeclarations.SubroutineParserallocated a separate begin id viaRuntimeCode.evalBeginIds.computeIfAbsent(ast, k -> classCounter++), producing a different global key. The sub's captured field was thus a fresh empty scalar.Fix
SubroutineParser), forstatedeclarations reuse the existingast.idas the begin id instead of allocating a new one, and register it inevalBeginIds.StateVariable.retrieveStateScalar/Array/Hashnow fall back to global storage when the per-sub map has no entry, so a sub accessing a state var declared in an outer (non-sub) scope still sees the shared storage.Test plan
{ state $t = [10]; sub f { $t->[0] } }; print f()now prints10.make make()+state $c = 0; ++$c— instances still have independent counters).jcpan -t QRCode::Encoder— all tests pass (t/00-load, t/01-hello, t/02-best, t/03-version-7; also Math::ReedSolomon::Encoder dependency).make(unit tests) passes.Generated with Devin