Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/types: imported anonymous interface type handled incorrectly #20046

Closed
griesemer opened this issue Apr 19, 2017 · 2 comments
Closed

go/types: imported anonymous interface type handled incorrectly #20046

griesemer opened this issue Apr 19, 2017 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@griesemer
Copy link
Contributor

mkdir tmp
cd tmp
echo 'package a; var X interface { F() int }' > a.go
echo 'package b; import "./a"; var _ = a.X.F()' > b.go
go tool compile a.go
gotype -c=gc b.go

leads to:

b.go:1:34: invalid operation: a.X (variable of type interface{F() int}) has no field or method F

but the program is correct. Both:

go tool compile b.go

and

gotype -c=source b.go

work as expected.

@griesemer griesemer self-assigned this Apr 19, 2017
@griesemer griesemer added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 19, 2017
@griesemer griesemer added this to the Go1.9 milestone Apr 19, 2017
@griesemer
Copy link
Contributor Author

Prelim. finding: It appears that go/types.Interface.Complete() is never called for the imported interface type. As a result, while the method's been imported, the internal interface type representation has an empty allMethods field and hence F is not found.

Investigating gcimporter.

@griesemer
Copy link
Contributor Author

Turns out that imported unnamed interface types are never Completed() in the gcimporter if trackAllTypes is disabled (which it is, because cmd/compile cannot handle cycles through anonymous types). The original importer code didn't have this bug because all types were tracked. As soon as the interface is given a name, it shows up in the list of imported types, and then it is completed.

It's surprising that this hasn't show up before: I suspect that most code doesn't declare variables (and function result types!) that are anonymous interfaces, and where they do, it's the empty interface (which happens to work).

gopherbot pushed a commit to golang/tools that referenced this issue Apr 20, 2017
This is a backport of https://go-review.googlesource.com/#/c/41198/.

For golang/go#20046.

Change-Id: I58448c7dcc5e835d5c774e253cb56fec6e154f12
Reviewed-on: https://go-review.googlesource.com/41204
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
@golang golang locked and limited conversation to collaborators Apr 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants