Description
https://pkg.go.dev/go/ast is the package declaring the syntax tree types for parsed Go code, where https://pkg.go.dev/go/parser produces them.
However, note that it also has a rather rudimentary form of typechecking, as it attempts to keep track of objects and scopes. However, this doesn't really work in practice, because go/ast
and go/parser
do not implement a proper Go typechecker; that's https://pkg.go.dev/go/types.
I find this to be very unfortunate in terms of confusing users, especially those new to Go tools and static analysis who are not aware of this historical gotcha. For example, I was just helping someone on Slack who was finding that objects were sometimes not resolved correctly, and the reason was the use of ast.Object
rather than types.Object
. See their code for context.
For some more context, we added a mode bit to go/parser
to skip object resolution in #46485, bringing significant speedups at basically no cost for many programs, since they made no use of this partial object resolution.
We can't outright remove types and fields like ast.Object
for backwards compatibility concerns, but we could certainly warn users against the use of ast.Object
. For instance:
package ast
// [...]
//
// Deprecated: go/ast does not implement a full Go type checker; use go/types.Object instead.
type Object [...]
// [...]
//
// Deprecated: go/ast does not implement a full Go type checker; use go/types.Scope instead.
type Scope [...]
It is true that, potentially, using the partial object resolution in go/ast
is fine for some use cases. Perhaps they only deal with very simple Go code that doesn't run into any of the limitations against go/types
. I think it should be fine for those (hopefully very rare) cases to consciously ignore the deprecation notice.