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: Need an easy way to get the file-level *types.Scope of an ast.Expr #8730

dsymonds opened this issue Sep 15, 2014 · 2 comments


Copy link

See golang/lint#61 for some background.

For this program,
package foo
import "syscall"
var flags uint32 = syscall.AF_INET
the go/types package will (correctly) say that the *types.Scope of the `flags` var is
the package scope rather than the file scope. That, however, makes it harder to evaluate
the RHS of the assignment outside of the assignment context, since the
"syscall" name is not defined at package scope.

Please provide an easy way to get from the *ast.ValueSpec (or any ast.Expr, or a
token.Pos) to the narrowest scope that encompasses its definition.
Copy link

rsc commented Sep 15, 2014

Comment 1:

Labels changed: added release-go1.4maybe, removed release-go1.4.

Copy link

Comment 2:

Turns out there's no need to have special go/types support: it's fairly trivial to get
to the corresponding *ast.File given a token.Pos (search if the pos is within the file),
and then map the file to the respective file scope.
Here's a complete example program (needs to be run outside the playground):
Will close for now. Feel free to re-open if this is not satisfactory.

Status changed to WorkingAsIntended.

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

4 participants