Skip to content

Commit

Permalink
interp: fix method lookup on aliased types
Browse files Browse the repository at this point in the history
In aliased type declarations, when the target type was imported from
an external package rather than declared locally, the aliased type was
overwritten by target, loosing ability to lookup methods on the aliased
type. Aliasing on imported types is now properly detected and handled.

Fixes #971.
  • Loading branch information
mvertes authored Nov 30, 2020
1 parent 81e1e5f commit b25ee3f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
15 changes: 15 additions & 0 deletions _test/time15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "time"

type TimeValue time.Time

func (v *TimeValue) decode() { println("in decode") }

func main() {
var tv TimeValue
tv.decode()
}

// Output:
// in decode
5 changes: 3 additions & 2 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,10 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
return false
}

if n.child[1].kind == identExpr {
switch n.child[1].kind {
case identExpr, selectorExpr:
n.typ = &itype{cat: aliasT, val: typ, name: typeName}
} else {
default:
n.typ = typ
n.typ.name = typeName
}
Expand Down
5 changes: 3 additions & 2 deletions interp/gta.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,11 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
return false
}

if n.child[1].kind == identExpr {
switch n.child[1].kind {
case identExpr, selectorExpr:
n.typ = &itype{cat: aliasT, val: typ, name: typeName, path: importPath, field: typ.field, incomplete: typ.incomplete, scope: sc, node: n.child[0]}
copy(n.typ.method, typ.method)
} else {
default:
n.typ = typ
n.typ.name = typeName
n.typ.path = importPath
Expand Down
5 changes: 3 additions & 2 deletions interp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
if v, ok := pkg[name]; ok {
t.cat = valueT
t.rtype = v.Type()
if isBinType(v) { // a bin type is encoded as a pointer on nil value
if isBinType(v) {
// A bin type is encoded as a pointer on a typed nil value.
t.rtype = t.rtype.Elem()
}
} else {
Expand Down Expand Up @@ -736,7 +737,7 @@ func (t *itype) finalize() (*itype, error) {
}

// ReferTo returns true if the type contains a reference to a
// full type name. It allows to asses a type recursive status.
// full type name. It allows to assess a type recursive status.
func (t *itype) referTo(name string, seen map[*itype]bool) bool {
if t.path+"/"+t.name == name {
return true
Expand Down

0 comments on commit b25ee3f

Please sign in to comment.