-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: improve assertion when PkgName is found in Package.Scope #72785
Comments
Although the wording of the assertion could be improved (e.g. by adding a case for PkgName, or printing the type of exp), I think the logic is sound. The problem is in these lines of your program:
A PkgName object should never appear at package scope. PkgName objects are created only by import declarations, and they create objects in the file scope. Any packages created by go/types (from syntax) or go/importer (from serialized packages) will preserve this invariant. I'm curious: why are you creating entire packages and their objects using the go/types |
Hi Alan, thanks for the insights. What I am really trying to do is to allow imported constants in types.Eval. So that the following work
Where Like this: https://go.dev/play/p/_V7qZ0KW6mU ( see https://github.com/etnz/calc/ for the full small project, that I stripped out to reproduce the bug ) I wonder if there is a more normal/safer way to import packages so that "foo.A + foo.B" works? |
What you're doing seems reasonable, but unfortunately go/types doesn't yet provide quite the necessary API functions for this kind of evaluation of constructed fragments, useful though it would be. |
It seems that the Checker is actually handling and expecting PkgName so this seem to be a correct approach: Line 689 in 644b984
So the bug is probably here: Line 729 in 644b984
When the lookup finds an exported pkgName it is wrongly returned. The latter seems cleaner, but might have more side effects. I'll be happy to implement the fix if it is agreed on. WDYT? |
Go version
go version 1.24
Output of
go env
in your module/workspace:What did you do?
Here is a way to reproduce the bug:
https://go.dev/play/p/q4u3qBDA7lO
go/types.Eval function panics with "unreachable" instead of an error.
You can switch the bool "bug" to false to see the "expected" normal error.
It is allowed in Go to import a package with an exported name (see https://go.dev/play/p/ruGWMm9SRLI )
When reproducing this schema with go sources it doesn't panic: https://go.dev/play/p/T22H349IOYc
What did you see happen?
Code is panicking.
What did you expect to see?
an error should have been returned.
The text was updated successfully, but these errors were encountered: