Repository navigation
fix(compiler): alias shadowed names in switch init statements - #526
Merged
Merged
Conversation
A short variable declaration in the init statement of a switch or type
switch that redeclares a name its initializer reads, as in
switch pos, err := d.consumeWhitespace(pos); err {
lowered the new let declaration in the same TypeScript block as the
initializer, so the initializer read the new, uninitialized binding and
threw a ReferenceError. encoding/json/jsontext does this in checkEOF.
If statements, for statements and plain blocks already save the outer
value and rename the new binding (lowerShortDeclShadowAliases). Do the
same for the init statements of switch and type switch statements, and
add a compliance test covering each of these forms.
Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
paralin
pushed a commit
that referenced
this pull request
Oct 24, 2026
encoding/json/v2 is compiled from Go source, but it uses the internals of encoding/json/jsontext, whose override implements only Value and the formatting options. jsontext.Internal is null there, so any program that imports encoding/json/v2 panics with "invalid memory address or nil pointer dereference" while the package initializes (`var export = jsontext.Internal.Export(...)` in arshal.go), before main runs. Programs can reach it without importing it themselves, through generated symbol tables such as yaegi's standard library symbols for Go 1.27; that is how a Cogent Core web app hit it. I first tried dropping the jsontext override and compiling jsontext from source (listed as a nativeDependency of encoding/json, so RawMessage keeps working). With #526, #527 and #528, json/v2 then works and matches native Go. But every program that uses encoding/json would compile jsontext and its internal packages: for a small Marshal/Unmarshal program the compiled output grew from 1.26 MB to 1.91 MB, and because jsontext.pointerSuffixError embeds error, its promoted Error method is async, which makes fmt calls with interface arguments async as well. That seemed too high a price for programs that never use json/v2. So this adds an override for encoding/json/v2 whose strict parity ledger blocks every export. A program that uses one now fails to compile with parity-blocked-use, naming the symbol, instead of failing at run time. TestOverrideParityVerifierBlocksJSONV2 compiles a real json/v2 program and checks for that diagnostic (it fails without this change). Two compliance outputs gain an erased type-only import of encoding/json/v2, because encoding/json.Unmarshaler is an alias of jsonv2.Unmarshaler since Go 1.27; they still pass.
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.
A short variable declaration in the init statement of a switch or type switch that redeclares a name its initializer reads, as in
switch pos, err := d.consumeWhitespace(pos); err {, lowered the new let declaration in the same TypeScript block as the initializer. The initializer then read the new, uninitialized binding and threw "ReferenceError: Cannot access 'pos' before initialization". encoding/json/jsontext does this in decoderState.checkEOF, so it fails when compiled from source.If statements, for statements and plain blocks already save the outer value and rename the new binding through lowerShortDeclShadowAliases. This does the same for the init statements of switch and type switch statements, through a small lowerSwitchInit helper shared by both. The new compliance test shadow_define_init covers the switch, type switch, if, block and for forms; no existing compliance output changes.