-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: cmd/vet: require explicit variable shadowing #31064
Comments
Apparently such a check already exists, though it is experimental, see: #29260 |
What @beoran said. I think it only makes sense to keep this proposal open if it proposes some way to enable the shadow checker by default. See Alan's reasoning as to why that hasn't been a good idea so far. |
@alandonovan wrote:
Change (2) above is that improved heuristic: shadows created with This isn't a request for a new -shadow switch. |
Given that vet runs as part of |
It seems possible that the error handling proposal will let us consider changing I suggest that we postpone taking action on this issue until after the error handling proposal has landed or been rejected. |
@ianlancetaylor, how could future error handling abolish redeclaration by Note the initial Draft Design requires @josharian, undoubtedly a lot of existing code would be newly flagged. We'd have to roll out in stages; Also, wouldn't performance improve by fixing needless shadows?
|
No, the generated code should be unaffected. There's no (heap) allocation here. |
@networkimprov The ability to make that kind of change is the point of #28221. People won't get the new behavior in old code. It still may not be feasible. But my point is that I think we'll have a better idea later. There is no rush. |
@randall77, I meant an extra stack object per iteration. EDIT: That seems to trigger a GC(?) every 500K iterations in https://play.golang.org/p/mKCYL9AyJJs @ianlancetaylor, #28221 doesn't really help; when revising existing code, no one wants to either: We're stuck with redeclaration. That is good reason to distinguish it from shadowing. In any event, we could take the first step and implement this proposal for a -shadow switch. |
I feel the heuristic proposed in this issue is too simple and will give too many false positives. I think the thing to do is to improve upon the existing shadow checker heuristic so it can actually be enabled by default. This probably will involve quite a bit of research on existing go code to see where implicit shadowing is used erroneously. |
Normally it would require a single extra stack object for all iterations. In your example code you're taking the address of the inner |
I think all discussions about shadowing should wait until we see what happens with error handling. Maybe that will help, maybe it won't. Let's find out first. I'm putting this proposal on hold. |
@randall77 is the memory-use impact of doing |
That effect on memory impact is very specific to the exact version of the compiler being used. And note that the impact is not due to |
Even if it's not a permanent feature, it can have a dramatic unforeseen impact on memory use. Shouldn't that be documented somewhere easy-to-find? |
That's true, but it's also true of many implementation pragmatics, in every language. The correct place to document them is not in the language spec, but in a guide to performance optimization, and every good guide to performance optimization in Go already says something to the effect of "understand the escape analysis". |
@gopherbot add Go2 To be fair, I suggested an addition to the FAQ, not the spec :-) |
Could we take the Hold off this proposal, since error handling changes have been deferred indefinitely? |
Done. |
cmd/vet/README lists the requirements for a vet check. |
False positives would be eliminated by adding code to make intended shadows explicit. See (2) in Changes to go vet. Adding that code is the proposed cost to alleviate the possibility of bugs due to shadowing, which bite new and experienced Go users. |
Sorry, @networkimprov, but having to type Putting this check in vet (and enabling in go test, which we want to be able to do for everything in vet) would break essentially all Go code in existence. This is a non-starter. This seems like a likely decline, because it is a (very) backwards-incompatible change. Leaving open for a week for final comments. |
Ah, I'd overlooked how common error variable shadows are. I religiously avoid that, but most ppl don't. |
Branched from #377.
Problem
Unintended shadows are easy to create with
:=
and cause nearly invisible bugs.Go takes one of eight possible paths for
a, b := f()
, as each var is defined, assigned, or shadowed. One cannot tell which without searching prior code, so it's easy to write something that behaves incorrectly.Proposal
Benefits provided
a) eliminate bugs due to unintended
:=
shadowsb) reduce the complexity of
:=
statements (a, b := f()
would have three paths)c) no language change; tiny learning curve
Changes to go vet
Let go vet flag implicit shadows,
s := t
.Let
var s T
orvar s = t
in the new scope silence go vet for intended shadows.If accepted, consider...
From #30321: Let
var name
allow redeclaration of the name within its scope, without creating a shadow. This idea is a companion to explicit shadowing. (Python also permits this.)The text was updated successfully, but these errors were encountered: