-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/vet: ensure that checks work correctly in the presence of aliases #17756
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
Comments
A challenge with vet is that it cannot assume it has correct or indeed any type information for imported packages. Consequently, there's a fine balance to be struck between using syntax information, using type information that can be relied upon because it's local to a file, and using type information from dependencies, which is likely to be absent for all non-standard packages. The way go/types represents dangling aliases, that is, aliases to members of packages for which no type information is available, does not tell us even the name of the missing thing, so using aliases here would make the tool less robust. In the specific case of |
Here's a list of uses of selector expressions in vet:
The ones that use package name directly are probably fine, per Alan's comments above. It seems fairly likely that packages atomic, C, and context won't be aliased away. For unsafeptr, unsafe can't be aliased, and reflect probably won't be. It might be better for unused to use package name rather than obj.Pkg().Path(), although the packages that matter out of the box (errors, fmt, sort) are unlikely to be aliased. Leaving that decision to Alan. That's all I see from a first visual inspection of vet. Alan, if you're happy, feel free to close this, and we'll deal with other specific issues as they come up in testing. |
For the record, I audited every go/ast-using package in the standard library and fixed the important ones where aliases could lead to a crash or incorrect results, but there are rapidly diminishing returns to worrying about aliases in these vet checks. Standard packages are, I suspect, unlikely to be the targets of aliases, and vet has a very fragmentary view of things outside the few files it analyzes. |
As an example, cmd/vet/lostcancel.go contains this code:
It is looking specifically for constructs like
context.WithCancel
. But with aliases, that could be instead just be WithCancel, or something else entirely. And this is particularly likely to happen with the context package because of the golang.org/x/net to stdlib context move. I believe there are other checks for selector expressions as well that need to be updated to be alias-aware.I suspect that this sort of need will happen enough ("is this thing really
context.WithCancel
, regardless of what it happens to be called right here?") that it'd be good to have go/types support to make it easier. Haven't thought yet about what the API looks like.See also #17755.
cc @robpike @alandonovan @griesemer
The text was updated successfully, but these errors were encountered: