go.tools/go/gcimporter: correct package for imported methods

R=adonovan
CC=golang-codereviews
https://golang.org/cl/48630044
This commit is contained in:
Robert Griesemer 2014-01-07 14:46:10 -08:00
parent d6eb8982f6
commit ff72a95f05
2 changed files with 26 additions and 1 deletions

View File

@ -875,9 +875,12 @@ func (p *parser) parseMethodDecl() {
base := deref(recv.Type()).(*types.Named)
// parse method name, signature, and possibly inlined body
pkg, name := p.parseName(true)
_, name := p.parseName(true)
sig := p.parseFunc(recv)
// methods always belong to the same package as the base type object
pkg := base.Obj().Pkg()
// add method to type unless type was imported before
// and method exists already
// TODO(gri) This leads to a quadratic algorithm - ok for now because method counts are small.

View File

@ -192,3 +192,25 @@ func TestIssue5815(t *testing.T) {
}
}
}
// Smoke test to ensure that imported methods get the correct package.
func TestCorrectMethodPackage(t *testing.T) {
// This package does not handle gccgo export data.
if runtime.Compiler == "gccgo" {
return
}
imports := make(map[string]*types.Package)
_, err := Import(imports, "net/http")
if err != nil {
t.Fatal(err)
}
mutex := imports["sync"].Scope().Lookup("Mutex").(*types.TypeName).Type()
mset := types.NewPointer(mutex).MethodSet() // methods of *sync.Mutex
sel := mset.Lookup(nil, "Lock")
lock := sel.Obj().(*types.Func)
if got, want := lock.Pkg().Path(), "sync"; got != want {
t.Errorf("got package path %q; want %q", got, want)
}
}