fix: correct implicit struct field name for pointer (#158)
This commit is contained in:
committed by
Ludovic Fernandez
parent
9b1a0c006c
commit
e63a9ae1de
15
_test/struct12.go
Normal file
15
_test/struct12.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type S1 struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type S2 struct {
|
||||
*S1
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(S2{})
|
||||
}
|
||||
18
_test/struct13.go
Normal file
18
_test/struct13.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Fromage struct {
|
||||
http.Server
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := Fromage{}
|
||||
fmt.Println(a.Server)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// { <nil> <nil> 0s 0s 0s 0s 0 map[] <nil> <nil> 0 0 {{0 0} 0} <nil> {0 0} map[] map[] <nil> []}
|
||||
18
_test/struct14.go
Normal file
18
_test/struct14.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Fromage struct {
|
||||
*http.Server
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := Fromage{}
|
||||
fmt.Println(a.Server)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// <nil>
|
||||
@@ -122,18 +122,18 @@ func (interp *Interpreter) Gta(root *Node, rpath string) error {
|
||||
|
||||
case TypeSpec:
|
||||
typeName := n.child[0].ident
|
||||
if n.child[1].kind == Ident {
|
||||
var typ *Type
|
||||
typ, err = nodeType(interp, scope, n.child[1])
|
||||
n.typ = &Type{cat: AliasT, val: typ, name: typeName, pkgPath: rpath}
|
||||
} else {
|
||||
n.typ, err = nodeType(interp, scope, n.child[1])
|
||||
n.typ.name = typeName
|
||||
n.typ.pkgPath = rpath
|
||||
}
|
||||
var typ *Type
|
||||
typ, err = nodeType(interp, scope, n.child[1])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if n.child[1].kind == Ident {
|
||||
n.typ = &Type{cat: AliasT, val: typ, name: typeName, pkgPath: rpath}
|
||||
} else {
|
||||
n.typ = typ
|
||||
n.typ.name = typeName
|
||||
n.typ.pkgPath = rpath
|
||||
}
|
||||
// Type may already be declared for a receiver in a method function
|
||||
if scope.sym[typeName] == nil {
|
||||
scope.sym[typeName] = &Symbol{kind: Typ}
|
||||
|
||||
@@ -341,14 +341,7 @@ func nodeType(interp *Interpreter, scope *Scope, n *Node) (*Type, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name := c.child[0].ident
|
||||
if name == "" && c.child[0].kind == SelectorExpr {
|
||||
name = c.child[0].child[1].ident
|
||||
}
|
||||
if name == "" {
|
||||
return nil, n.cfgError("empty field name")
|
||||
}
|
||||
t.field = append(t.field, StructField{name: name, embed: true, typ: typ})
|
||||
t.field = append(t.field, StructField{name: fieldName(c.child[0]), embed: true, typ: typ})
|
||||
t.incomplete = t.incomplete || typ.incomplete
|
||||
} else {
|
||||
l := len(c.child)
|
||||
@@ -370,6 +363,20 @@ func nodeType(interp *Interpreter, scope *Scope, n *Node) (*Type, error) {
|
||||
return t, err
|
||||
}
|
||||
|
||||
// fieldName returns an implicit struct field name according to node kind
|
||||
func fieldName(n *Node) string {
|
||||
switch n.kind {
|
||||
case SelectorExpr:
|
||||
return fieldName(n.child[1])
|
||||
case StarExpr:
|
||||
return fieldName(n.child[0])
|
||||
case Ident:
|
||||
return n.ident
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
var zeroValues [MaxT]reflect.Value
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in New Issue
Block a user