Skip to content

fix(reflect): make nil slices, typed nil interfaces and map lookups match Go - #546

Merged
paralin merged 1 commit into
s4wave:devfrom
cogentcore:fix/reflect-nil-values
Oct 25, 2026
Merged

paralin merged 1 commit into
s4wave:devfrom
cogentcore:fix/reflect-nil-values

Conversation

@rcoreilly

Copy link
Copy Markdown
Contributor

Three reflect results differed from Go.

reflect.Zero and New(t).Elem() gave an empty slice for a slice type, so IsNil was false for the zero value of every slice type, and a slice set to Zero was not nil. The zero value of a slice type is now null, as the compiler makes a nil slice.

IsNil of an interface Value holding a typed nil, such as an interface holding a nil slice, was true: isTypedNilValue looked through the interface. An interface with a dynamic type is not nil, so IsNil of an Interface Value is now true only when it holds nothing.

MapIndex looked the key up by key.Interface(), which boxes a number with its type, so it missed number keys, which SetMapIndex and compiled code store unboxed; and for a missing key it gave a valid Value of an invalid type rather than the zero Value. It now looks the key up as SetMapIndex stores it and gives the zero Value for a missing key or a nil map.

reflect_nil_values checks each case against Go.

…atch Go

reflect.Zero and New gave an empty slice rather than a nil one for a slice type, so IsNil was false for the zero value of every slice type. IsNil of an interface Value holding a typed nil, such as a nil slice, was true, although the interface has a dynamic type. MapIndex looked up the key's Interface, which boxes a number, so it did not find a number key that SetMapIndex had stored, and it gave a valid Value for a missing key.

The zero value of a slice type is now nil, IsNil of an interface is false when it holds a typed nil, and MapIndex looks the key up as SetMapIndex stores it, giving the zero Value for a missing key.

Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
@paralin
paralin changed the base branch from master to dev October 25, 2026 18:38
@paralin
paralin merged commit 72b9d50 into s4wave:dev Oct 25, 2026
7 checks passed
@paralin

paralin commented Oct 25, 2026

Copy link
Copy Markdown
Member

Landed on master as 9b50f0b, rebased onto the batch #532 to #550, with regenerated fixtures in fca8071. MapIndex reads key.value directly, since value is a public getter, without the cast.

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