-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: cmd/vet: report evaluation order ambiguities #27098
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
Is |
One of the three vet criteria is frequency
how often do people write this kind of expression in real programs? Is this "evaluation-order confusion" actually source of a significant number of problems in the wild? |
There are many variants of the case in the above examples. |
Yes, I also think it is really hard for func f(p1, p2 *int) {
y, z := *p1, f(p2)
fmt.Println(y, z)
} If this proposal is unpractical, I would make another proposal (a Go 2 one) to remove the unspecified behavior. |
In fact, sometimes, I feel that Go compilers should report a compilation error instead of a vet warning for the case in my starting comment. Because that code is definitely bad for sure. |
Yes... but you wrote those examples, except for the one from #25609, which is the only report by someone being bitten by this. What I asked is if this is actually a common source of errors in Go code; that's what the 3rd vet criteria asks for:
|
I can't say it is uncommon. Please see the code modifications mentioned in this issue by tamird. |
Vet can't answer this perfectly. As long as it only reported true errors, it would be OK to have false negatives. The question is: how much work is it to detect these? |
/cc @alandonovan |
The analytical firepower required to do this is far beyond vet. Every statement of the form A better approach would be a dynamic analysis along the lines of the race detector: statically transform a statement such as:
into:
The trick would be to eliminate as many of these checks as possible for efficiency. (This example gives just a flavor of the approach.) |
@alandonovan's example makes me more tend to think that this unspecified behavior is common encountered in practice and this is a fundamental design issue of Go. After all, Go, as a so called C+ language, one of its main goals is to remove as many unspecified behaviors in C as possible, in particular for the unspecified behaviors which may be common encountered. I feel this unspecified behavior can be removed for sure, there are no technique obstacles to achieve it. |
I'm not so pessimistic; I think genuine bugs will be rare. Of course that means I'm less sanguine about the value of a vet check. A competent programmer knows whether the values f and x might have something do with a and i, or whether they are definitively unrelated. This intuition, when well honed, is far stronger than any practical static analysis. Removing unspecified behavior entirely from the language is unlikely to be worth the considerable effort and dynamic costs. Even if you specify expression evaluation order completely, including the order in which error cases are checked by the runtime, and forbid "unsafe" and concurrency, the libraries on which one builds any application are not specified to that same level of detail. Specifying the evaluation order also dramatically increases register pressure in the compiler, which is why even Scheme, with arguably the cleanest semantics of any language, resisted the urge to specify the evaluation order. |
I think I agree on your opinion in theory. I just need to see some concrete examples to be |
It sounds like this is beyond vet, so closing. |
Checks for return statements that may have unwanted dependency on the evaluation order. See: golang/go#27098 golang/go#23188 Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
Checks for return statements that may have unwanted dependency on the evaluation order. See: golang/go#27098 golang/go#23188 Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
As some evaluation orders in multi value assignments are unspecified,
the following two programs may print either
0 123
or123 123
.I propose that
go vet
should warn on such use cases to avoid unexpected behaviors.The text was updated successfully, but these errors were encountered: