-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: defer of embedded method resolves pointer too early #52025
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
Could you please elaborate more? Not only the arguments, but also the function value is also evaluated when |
Per the spec:
|
Related: #38634 The interpretation I've been leaning towards lately is that So in the example above, Note that cmd/compile gives the same behavior whether you write |
That is true, but here again there is a difference between gc and gccgo. With gc the nil dereference happens when the |
So it sounds like the argument is that the Maybe that is right but I'm not yet convinced. The rules for method promotion say that the method |
It sounds like both compilers have bugs here. Assuming @ianlancetaylor is correct (
Does that sound right? |
Sounds right to me, yes. |
I'm still of the opinion that implicit addressing/dereferencing and embedded field traversal should happen right away, both in the case of method values and deferred method calls. That is, I think In cases where (Also, in case it sounded like I was suggesting otherwise, I'm sure cmd/compile has issues/inconsistencies in this area too. To date, I've mostly tried to just maintain existing method selector semantics within cmd/compile while unifying call handling, and haven't gotten far enough yet to be pressed to make any hard decisions that risk changing existing behavior.) |
Not intuitive but that makes sense if that's how I should start thinking about it. Seems though So is the issue here is that |
@nveeser I wouldn't recommend trying to rationalize the compilers' (or at least cmd/compile's) existing behaviors here. |
Didn't this issue formally deny this? Quoting @cuonglm's wording In that issue thread (replace relevant identifers):
I do agree some wording in spec need be more clear. |
The wrapper function is gc specific implementation, there's no visible in source code nor in the Go spec that S2 has Close method in case of promoted method. |
Which gccgo version do you use? It looks |
It looks gc doesn't make the wrapper, which is why the program panics by using gc. |
Looks at the result of |
Apologies - I wasn't looking at the compiler, just reflecting back the discussion so far, to clarify what I am reading. Sounds like the open question is whether the method value expression |
Novice question - why would s2.Close panic? The issue is that s2.S1 is nil, not s2 and a nil receiver is valid right? |
But it is not used in
A nil receiver is valid. The panic is caused by dereferencing |
Yes, I answered for your statement that gc does not make the wrapper, it does make! Like I said (and discussed with you in the past), that's gc specific implementation, and happen much later after typecheck. In fact, go/types also normalize |
I tested with |
Rolling forward to 1.20. Please comment if you disagree. Thanks. |
This issue might be only a spec issue. But this one is a real bug. |
This was found in triage, ping @griesemer @mdempsky @ianlancetaylor. Do we want any kind of resolution here in Go 1.22? If yes, and you'd like to do it, feel free to assign to yourself. If not, please push into Backlog. Thanks! |
Consider this test case:
When run with the gc compiler, this crashes:
When run with the gccgo compiler it exits successfully without printing anything.
I think the difference is that with the gc compiler the
defer
statement is resolving the embedded pointer at the point of thedefer
, and thus deferred a call to a nil pointer. If that is correct, then that seems wrong. Adefer
statement resolves all the arguments, but it shouldn't resolves2
intos2.S1
.Or maybe gccgo is incorrectly not crashing, but I don't yet see it.
CC @randall77 @griesemer @mdempsky
The text was updated successfully, but these errors were encountered: