go.tools/go/gcimporter: correct package for imported methods
R=adonovan CC=golang-codereviews https://golang.org/cl/48630044
This commit is contained in:
parent
d6eb8982f6
commit
ff72a95f05
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue