fix: correct struct field lookup for embedded pointers (#160)

This commit is contained in:
Marc Vertes
2019-04-17 16:08:51 +02:00
committed by Ludovic Fernandez
parent e63a9ae1de
commit 637418c8a9
3 changed files with 48 additions and 11 deletions

23
_test/struct15.go Normal file
View File

@@ -0,0 +1,23 @@
package main
import (
"fmt"
"net/http"
)
type GzipResponseWriter struct {
http.ResponseWriter
index int
}
type GzipResponseWriterWithCloseNotify struct {
*GzipResponseWriter
}
func (w GzipResponseWriterWithCloseNotify) CloseNotify() <-chan bool {
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
func main() {
fmt.Println("hello")
}

15
_test/struct16.go Normal file
View File

@@ -0,0 +1,15 @@
package main
type S1 struct {
Name string
}
type S2 struct {
*S1
}
func main() {
s1 := S1{"foo"}
s2 := S2{&s1}
println(s2.Name)
}

View File

@@ -476,21 +476,20 @@ func (t *Type) fieldSeq(seq []int) *Type {
// lookupField returns a list of indices, i.e. a path to access a field in a struct object
func (t *Type) lookupField(name string) []int {
var index []int
if fi := t.fieldIndex(name); fi < 0 {
for i, f := range t.field {
if f.typ.cat != StructT {
continue
}
if fi := t.fieldIndex(name); fi >= 0 {
return []int{fi}
}
for i, f := range t.field {
switch f.typ.cat {
case PtrT, StructT:
if index2 := f.typ.lookupField(name); len(index2) > 0 {
index = append([]int{i}, index2...)
break
return append([]int{i}, index2...)
}
}
} else {
index = append(index, fi)
}
return index
return nil
}
// getMethod returns a pointer to the method definition