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

x/tools/cmd/eg: matches should be limited to value expressions #14728

Open
mdempsky opened this issue Mar 9, 2016 · 2 comments
Open

x/tools/cmd/eg: matches should be limited to value expressions #14728

mdempsky opened this issue Mar 9, 2016 · 2 comments
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@mdempsky
Copy link
Member

mdempsky commented Mar 9, 2016

$ cat a.go
package a
func before(x *int) int { return *x }
func after(x *int) int { return 42 }

$ cat b.go
package b
func x(p **int) {}

$ eg -t a.go b.go
=== b.go (1 matches)
package b

func x(p 42)    {}

The problem appears to be that in the type expression **int, the subexpression *int has type *int, so eg is deciding that it's a candidate match for the original expression. However, *int is a type expression, so it doesn't make sense to replace it with a value expression like 42.

I think eg needs to make use of go/types.TypeAndValue's IsValue method.

/cc @alandonovan

@mdempsky mdempsky added this to the Unreleased milestone Mar 9, 2016
@mdempsky
Copy link
Member Author

The problem is that eg uses go/types.(*Info).TypeOf (in (*Transformer).matchWildcard) to find the type of a candidate expression to match against, but TypeOf doesn't distinguish type vs value expressions.

Easiest fix seems to be to duplicate TypeOf into eg and augment it with the extra type vs value logic. I'm curious if this functionality is useful to other go/types consumers though.

@gopherbot
Copy link

CL https://golang.org/cl/40772 mentions this issue.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants