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
cmd/compile: add hint to type errors when builtin identifiers are shadowed #22822
Comments
If this mistake is already a compile error, wouldn't a vet warning be a bit redundant? |
Also we can't flag these indiscriminately, you can write valid code that does that. Vet checks need to satisfy a 'precision' requirement. I'm not sure how easy it'll be to avoid false positives here. |
@mvdan: it's only a compile error if @ALTree: I thought the point of |
These built-in functions are identifiers, not keywords, for good reason. One of the reasons is to allow adding new built-ins in the future without breaking existing code. The other is to allow users to shadow them at their discretion. In neither case should vet warn about perfectly fine code.
If they're being used incorrectly, it will cause a compile time error. If they are being used correctly, the user intended to use them that way. Neither case warrants a vet check IMHO. |
Hi @dominikh - Thanks for the feedback. The reason why I filed this issue in the first place was because I came across this situation myself via a compilation error and it took me some not-insignificant-amount of time to figure out what the root cause was. I suspect that others have encountered this problem at times, so I'm not sure that the user always intends to "shadow" There's a maintainability argument to be made, too. If a user shadows
First, I'm not sure why anyone would intentionally shadow a function with a variable. Shadowing it with a substitute function, perhaps. Is this common practice among Go programs? Second, was this actually intended? Is there any documentation or history behind this? |
@otterley shadowing builtin functions isn't particularly encouraged, but it can be useful sometimes. Similarly, it is sometimes useful to name a function parameter Perhaps the right solution here would be to improve the compile error messages, by adding something like |
@mvdan Augmenting the error message in the compiler would be a fine substitute! |
I have rewritten the issue title. I don't think a full-blown proposal is necessary for this, but I would like some input from others like @griesemer and @mdempsky. I have to say that in my early days of Go development I did shadow builtin names like |
I'm open to expanding the compiler error message to include extra information if folks want to suggest something concrete (e.g., which existing error message to change, how to change it, and when exactly we should print something else). The most likely candidate in my mind is this error message:
|
@mdempsky that's the error that the example in the original post shows, and I agree that it would be the most useful as a start. More can be added later. The message for this error would be changed when the identifier name matches a builtin function. How exactly to change the message can be fine-tuned later, but I was thinking something along the lines of what I mentioned before - I'm not familiar with the norm and style for these error hints, so pointers are welcome. I'll give this a look in the 1.11 window. |
We don't have any other "did you mean X?" error messages currently. That feels non-idiomatic to me. I'm thinking maybe something like "builtin len shadowed at foo.go:42". |
I thought I remembered similar hints, such as in the
You mean a second, separate error, or as an addition to the original error? The position info seems useful in any case, hadn't though about that. |
As @mdempsky already said, the compiler doesn't provide hints in error messages, at least not in the suggested form. We do have hints for misspelled identifiers (e.g. https://play.golang.org/p/8PJCltlBau ). Any improvement here should be in the error reporting and in the same vain. I think reporting the location of the
But even this is a slippery slope. Are we going to do this everywhere we refer to an object by name? Why not? Maybe it's good enough to do it for shadowed built-ins. |
Shadowing a builtin is not itself an error, so it would have to be an addition to the original. We occasionally print multiline errors when there's additional information to include relevant to an error. |
No, not an additional error. Simply making the existing error clearer by adding position information for the object in question so it's clear where it's defined. For instance, if |
Change https://golang.org/cl/97455 mentions this issue: |
@griesemer do you think we should mirror this better error message in go/types? I'm happy to submit a CL for it too. I also wonder if this should be extended to other scenarios. For example, shadowing a type can result in failed type conversions and declarations:
The first case could be caught by having my CL cover type builtins too, not just the funcs. The second would require another separate fix. |
@mvdan Let's leave it as is for now. Also, go/types already reports not just the type but also what it is (variable, constant, etc.) which makes it clearer. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?1.9.2
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?darwin/amd64
What did you do?
The following code containing an assignment to
len
fails to compile, butgo vet
doesn't warn about why: https://play.golang.org/p/ap8LOPyn6MWhat did you expect to see?
A warning about assignment to the
len
variable. (Go also allows the user to assign to other built-in-but-not-reserved names, such ascap
andappend
.)What did you see instead?
No warnings.
The text was updated successfully, but these errors were encountered: