-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: incorrect location of panic during 2-phase execution of assignment statement #71054
Comments
Related Issues
Related Discussions (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
This looks like it is working correctly to me. In phase 1, we need to evaluate 3 things:
The order of evaluation between those 3 things is not specified. The first one nil-pointer panics, but that may happen before or after the call to Because phase 1 panics, phase 2 does not happen. |
The spec mentioned the phase 1 is evaluated in "usual order", which is very strict:
With that, I interpret it as the evaluation order must be:
|
It is, but that statement relates only the ordering between those events listed (function call, method call, receive operator, or binary logic operator). There is only one such event here, a function call.
More generally, the ordering of those events with respect to all other operations (of import here, a pointer dereference) is not guaranteed. |
I see, thanks for the explanation! That makes more sense. I'll close this bug. |
Isn’t this a problem though with it not being specified as the function could make the nil pointer dereference not or happen. (Ie what if foo is passed to the function call) Why not specify this? This is the same sort of undefined behavior that makes C++ such a pain. |
@robaho |
Fair enough. Probably just triggered by being bitten by similar stuff in C++. |
see #27804 |
Go version
go version go1.23.2 linux/amd64
Output of
go env
in your module/workspace:What did you do?
Original post: https://groups.google.com/g/golang-nuts/c/anNFVPtCGyQ/
Example code: https://go.dev/play/p/6RulNQwIk4n
The example code contains an assignment statement:
The assignment statement will trigger nil pointer panic and contains visible side effects in both phases as described in golang spec:
What did you see happen?
The nil pointer panic happened after
compute_value()
expression is evaluated in phase 1 but beforephase_2
variable is updated in phase 2.What did you expect to see?
The nil pointer panic happened before
compute_value()
expression is evaluated in phase 1, as mentioned in original post.The text was updated successfully, but these errors were encountered: