Skip to content

Comments

fix: allow reflect.StructOf dynamic struct fields in expr.Compile#937

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-issue-936
Draft

fix: allow reflect.StructOf dynamic struct fields in expr.Compile#937
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-issue-936

Conversation

Copy link
Contributor

Copilot AI commented Feb 24, 2026

reflect.StructOf requires PkgPath to be set for lowercase-named fields, which causes Go's reflection API to treat them as unexported. PR #846's IsExported() check in the checker then silently skips these fields, producing an "unknown name" compile error for what is a valid runtime-accessible field.

Changes

  • checker/nature/utils.go: Scope the IsExported() guard to named struct types only (s.rType.PkgPath() != ""). Anonymous types (including all reflect.StructOf outputs, whose type-level PkgPath() is always "") allow all fields through. The Checker not failing when accessing unexported struct fields #844 fix for regular Go structs is fully preserved.

  • vm/runtime/runtime.go: Add exported ValueInterface(reflect.Value) any that falls back to type-specific getters (Bool(), Int(), String(), etc.) when Interface() would panic on an unexported field. Update FetchField and Fetch to use it; extend the IsExported gate in Fetch to also pass anonymous-struct fields.

  • builtin/lib.go: Mirror the same gate change and switch to runtime.ValueInterface in the get function.

Example

dynType := reflect.StructOf([]reflect.StructField{
    {Name: "value", Type: reflect.TypeFor[bool](), PkgPath: "github.com/some/package"},
})
env := reflect.New(dynType).Elem().Interface()

// Was: "unknown name value" in v1.17.7+
// Now: compiles and evaluates correctly
program, err := expr.Compile("value", expr.Env(env))
result, err := expr.Run(program, env) // → false (zero value)

ValueInterface covers all primitive kinds; non-primitive unexported fields in anonymous structs (e.g. nested struct, slice) will still panic with a descriptive message, as there is no way to retrieve them without unsafe.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…le (#936)

Co-authored-by: antonmedv <141232+antonmedv@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue with bug report #936 fix: allow reflect.StructOf dynamic struct fields in expr.Compile Feb 24, 2026
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