Skip to content
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

spec: clarify timing of nil-checks for defer/go statements #38634

Open
mdempsky opened this issue Apr 24, 2020 · 4 comments
Open

spec: clarify timing of nil-checks for defer/go statements #38634

mdempsky opened this issue Apr 24, 2020 · 4 comments

Comments

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Apr 24, 2020

Test program: https://play.golang.org/p/rDWCdf_ha5w

This program tests various conditions where a deferred function call can panic "early" (i.e., at the time of the defer statement) or "late" (i.e., at the time the deferred function is actually invoked).

When compiled with cmd/compile, it prints "early late early \\ early late early \\ late".

When compiled with gccgo (edit: 8.0), it prints "late late early \\ late late early \\" and then crashes with a fatal error. (Edit: gccgo 10.0.1 prints "late late early \\ late late early \\ late".)

--

I think cases 2, 3, 5, and 6 where cmd/compile and gccgo agree are correctly implemented.

I think gccgo is wrong to fatally error on case 7. (If for no reason other than because the fatal error says "go of nil func value", but it's a defer statement.)

I'm unsure about cases 1, 4, and 7 otherwise though. I suspect they should all be "early" or all be "late" though.

/cc @griesemer @ianlancetaylor

@mdempsky mdempsky added this to the Unplanned milestone Apr 24, 2020
@erifan
Copy link
Contributor

@erifan erifan commented Apr 24, 2020

With a relatively new version of gccgo, this program will not crash. My gccgo version:
$ gccgo --version
gccgo (GCC) 10.0.1 20200421 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

But the print result is still "late late early \ late late early \".

@mdempsky
Copy link
Member Author

@mdempsky mdempsky commented Apr 24, 2020

@erifan Thanks, apparently I was still using gccgo 8. Testing with 10.0.1, I agree it no longer crashes. Instead, the 7th test case prints "late", like cmd/compile.

@cuonglm
Copy link
Contributor

@cuonglm cuonglm commented Apr 24, 2020

I think case 7 should be early. The Go spec said:

the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked

So we should know f is nil at the end of defer statement.

@gopherbot

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.