]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: embedded fields can be predeclared types
authorRobert Griesemer <gri@golang.org>
Tue, 26 Feb 2013 04:42:29 +0000 (20:42 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 26 Feb 2013 04:42:29 +0000 (20:42 -0800)
R=adonovan, r
CC=golang-dev
https://golang.org/cl/7376055

src/pkg/go/types/operand.go
src/pkg/go/types/testdata/decls3.src

index 6b902e301556d1bfd5796057f58c3477dcdc77bc..c4c32b2f4dd0eea4bba2efe3499d391677c7afa6 100644 (file)
@@ -300,7 +300,11 @@ func lookupFieldBreadthFirst(list []embeddedType, name QualifiedName) (res looku
                                        // this level, f.Type appears multiple times at the next
                                        // level.
                                        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 {
                                // Possible optimization: If the embedded type
                                // is a pointer to the current type we could
                                // 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 {
index 4bc7d414941058876678207faab9ead0166b06c3..6aa9f90e9ff06dbd216d9f4b87e7a11760e7d41d 100644 (file)
@@ -44,6 +44,28 @@ func issue4355() {
        _ = 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.
 
 type D1 struct {