Skip to content

feat(reflect): add the missing Value methods - #528

Merged
paralin merged 1 commit into
s4wave:masterfrom
cogentcore:feat/reflect-value-methods
Oct 24, 2026
Merged

paralin merged 1 commit into
s4wave:masterfrom
cogentcore:feat/reflect-value-methods

Conversation

@rcoreilly

Copy link
Copy Markdown
Contributor

reflect.Value lacked SetIterKey, SetIterValue, SetCap, Clear, CanInt, CanUint, CanFloat, CanComplex, OverflowComplex, Comparable, Fields, Methods, TryRecv, TrySend, InterfaceData and SetPointer. Code calling them failed at run time, and a type that embeds reflect.Value, as encoding/json/v2's addressableValue does, failed TypeScript type checking on the promoted method forwarders.

This implements them as Go defines them. SetIterKey and SetIterValue accept the nullable MapIter that MapRange returns. TryRecv and TrySend use the channels' non-blocking checks and are async like Recv and Send (listed in meta.json). InterfaceData and SetPointer need memory layouts and unsafe pointers that values do not have, so they panic with a clear message. The new compliance test reflect_value_methods_more checks the implemented methods against native Go.

reflect.Value lacked SetIterKey, SetIterValue, SetCap, Clear, CanInt,
CanUint, CanFloat, CanComplex, OverflowComplex, Comparable, Fields,
Methods, TryRecv, TrySend, InterfaceData and SetPointer. Code that calls
them failed at run time, and a type that embeds reflect.Value, as
encoding/json/v2 does, failed TypeScript type checking on the promoted
methods.

Implement them as Go defines them. TryRecv and TrySend are async like
Recv and Send. InterfaceData and SetPointer need memory layouts and
unsafe pointers that values do not have, so they panic. Add a compliance
test for the implemented methods.

Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
@paralin
paralin merged commit 4ed4423 into s4wave:master Oct 24, 2026
7 checks passed
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.
@rcoreilly
rcoreilly deleted the feat/reflect-value-methods branch October 24, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants