diff --git a/gnovm/pkg/gnolang/gonative.go b/gnovm/pkg/gnolang/gonative.go index 2fbf34ed1b1..cd0e3b9ce9f 100644 --- a/gnovm/pkg/gnolang/gonative.go +++ b/gnovm/pkg/gnolang/gonative.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "reflect" + "unsafe" "github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat" ) @@ -1133,7 +1134,12 @@ func gno2GoValue(tv *TypedValue, rv reflect.Value) (ret reflect.Value) { if ftv.IsUndefined() { continue } - gno2GoValue(ftv, rv.Field(i)) + fv := rv.Field(i) + if !fv.CanSet() { + // Normally private fields can not bet set via reflect. Override this limitation. + fv = reflect.NewAt(fv.Type(), unsafe.Pointer(fv.UnsafeAddr())).Elem() + } + gno2GoValue(ftv, fv) } case *MapType: // If uninitialized map, return zero value. diff --git a/gnovm/tests/files/struct59.gno b/gnovm/tests/files/struct59.gno new file mode 100644 index 00000000000..6c26978a808 --- /dev/null +++ b/gnovm/tests/files/struct59.gno @@ -0,0 +1,15 @@ +package main + +import "fmt" + +type X struct { + v string +} + +func main() { + x := X{v: "a"} + fmt.Printf("test %#v\n", x) +} + +// Output: +// test struct { v string }{v:"a"}