fix: lookup embededded field on struct pointer

This commit is contained in:
Marc Vertes
2020-04-09 01:26:03 +02:00
committed by GitHub
parent 12942b59a0
commit 465cb578e7
2 changed files with 23 additions and 1 deletions

17
_test/struct45.go Normal file
View File

@@ -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

View File

@@ -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 // fieldIndex returns the field index from name in a struct, or -1 if not found
func (t *itype) fieldIndex(name string) int { func (t *itype) fieldIndex(name string) int {
if t.cat == ptrT { switch t.cat {
case aliasT, ptrT:
return t.val.fieldIndex(name) return t.val.fieldIndex(name)
} }
for i, field := range t.field { for i, field := range t.field {
@@ -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 // 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 { 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 { if fi := t.fieldIndex(name); fi >= 0 {
return []int{fi} return []int{fi}
} }