Skip to content

Commit

Permalink
fix: lookup embededded field on struct pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes committed Apr 8, 2020
1 parent 12942b5 commit 465cb57
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 additions & 0 deletions _test/struct45.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

type T struct {
b bool
}

type T1 struct {
T
}

func main() {
t := &T1{}
t.b = true
println(t.b)
}

// Output: true
7 changes: 6 additions & 1 deletion interp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,8 @@ func (t *itype) zero() (v reflect.Value, err error) {

// fieldIndex returns the field index from name in a struct, or -1 if not found
func (t *itype) fieldIndex(name string) int {
if t.cat == ptrT {
switch t.cat {
case aliasT, ptrT:
return t.val.fieldIndex(name)
}
for i, field := range t.field {
Expand All @@ -882,6 +883,10 @@ func (t *itype) fieldSeq(seq []int) *itype {

// lookupField returns a list of indices, i.e. a path to access a field in a struct object
func (t *itype) lookupField(name string) []int {
switch t.cat {
case aliasT, ptrT:
return t.val.lookupField(name)
}
if fi := t.fieldIndex(name); fi >= 0 {
return []int{fi}
}
Expand Down

0 comments on commit 465cb57

Please sign in to comment.