Commit 1caaff6b authored by Robert Griesemer's avatar Robert Griesemer

go/types: embedded fields can be predeclared types

R=adonovan, r
CC=golang-dev
https://golang.org/cl/7376055
parent d6a057c9
...@@ -300,7 +300,11 @@ func lookupFieldBreadthFirst(list []embeddedType, name QualifiedName) (res looku ...@@ -300,7 +300,11 @@ func lookupFieldBreadthFirst(list []embeddedType, name QualifiedName) (res looku
// this level, f.Type appears multiple times at the next // this level, f.Type appears multiple times at the next
// level. // level.
if f.IsAnonymous && res.mode == invalid { if f.IsAnonymous && res.mode == invalid {
next = append(next, embeddedType{deref(f.Type).(*NamedType), e.multiples}) // Ignore embedded basic types - only user-defined
// named types can have methods or have struct fields.
if t, _ := deref(f.Type).(*NamedType); t != nil {
next = append(next, embeddedType{t, e.multiples})
}
} }
} }
...@@ -377,7 +381,11 @@ func lookupField(typ Type, name QualifiedName) lookupResult { ...@@ -377,7 +381,11 @@ func lookupField(typ Type, name QualifiedName) lookupResult {
// Possible optimization: If the embedded type // Possible optimization: If the embedded type
// is a pointer to the current type we could // is a pointer to the current type we could
// ignore it. // ignore it.
next = append(next, embeddedType{typ: deref(f.Type).(*NamedType)}) // Ignore embedded basic types - only user-defined
// named types can have methods or have struct fields.
if t, _ := deref(f.Type).(*NamedType); t != nil {
next = append(next, embeddedType{typ: t})
}
} }
} }
if len(next) > 0 { if len(next) > 0 {
......
...@@ -44,6 +44,28 @@ func issue4355() { ...@@ -44,6 +44,28 @@ func issue4355() {
_ = t /* ERROR "no single field or method" */ .X _ = t /* ERROR "no single field or method" */ .X
} }
// Embedded fields can be predeclared types.
func _() {
type T0 struct{
int
float32
f int
}
var x T0
_ = x.int
_ = x.float32
_ = x.f
type T1 struct{
T0
}
var y T1
_ = y.int
_ = y.float32
_ = y.f
}
// Borrowed from the FieldByName test cases in reflect/all_test.go. // Borrowed from the FieldByName test cases in reflect/all_test.go.
type D1 struct { type D1 struct {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment