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

x/tools/go/ast/astutil: Apply should not traverse Sel of SelectorExpr #28440

Open
dtolpin opened this Issue Oct 27, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@dtolpin
Copy link
Contributor

dtolpin commented Oct 27, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)? go version go1.11.1 linux/amd64

Does this issue reproduce with the latest release? yes

What operating system and processor architecture are you using (go env)?

OARCH="amd64"
GOBIN=""
GOCACHE="/home/dvd/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/dvd/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/dvd/work/infergo/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build174838993=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Read the source code and ran tests.

astutil.go:

   246		case *ast.SelectorExpr:
   247			a.apply(n, "X", nil, n.X)
   248			a.apply(n, "Sel", nil, n.Sel)

What did you expect to see?

I expect to NOT see line 248.

What did you see instead?

Apply traverses Sel field of SelectorExpr as a child. This is not composable: although Sel is an Ident node implementing ast.Expr, it is not an expression. What happens is that the user has to check in the Ident case that the parent is not SelectorExpr or the field is not "Sel". It is much better in my opinion to just let the API user to process Sel in the SelectorExpr.

Not traversing children of SelectorExpr is not a great workaround either because X (the first field) can be as complicated a tree as one wishes.

@gopherbot gopherbot added this to the Unreleased milestone Oct 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment