-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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/cgo: C.malloc does not support comma error return values #16309
Comments
I think the likely resolution of this is to say that Is there any reason that it is important to get an |
@ianlancetaylor If I'm think differently. Whether |
I think |
Please explain the rationale for excepting C.malloc from the documented CGO multi-valued return behavior.
If there is an overriding concern, I don't yet understand it. If possible, please explain it. |
I think the reason is likely historical artifact or overlook.
I don't think fixing it buys much. The only documented errno return is
ENOMEM,
and you can check the returned pointer for the same effect.
Anyway, I suspect that Go would has crashed somewhere else already if malloc
ever runs out of memory.
|
While the case is certainly obscure, it seems reasonable to me to make C.malloc consistent with other functions and keep the documentation simple. Arguably, the obscurity of the case even supports keeping the documentation simple. Is there some technical reason why this isn't easy to fix? |
This would be a breaking change to the API |
How is that a breaking change? It is already the case that Cgo code may call C functions with either a single or a single plus error result. Code that is already written to not "request" the error result from Please explain how this is an incompatible change? |
Supporting code that previously didn't compile, without changing behavior for code that did previously compile, doesn't seem like a breaking change to me. |
How so? Currently the only way to call C.malloc is with zero or one return values. I don't think anyone's proposing changing the behavior in this case (including the panic behavior). This just adds the possibility to call it with two return values. The only oddity would be that, presumably, we would still want it to panic in the zero or one case, and not panic in the two case. This is different from other C functions, but not without precedent in the language (e.g., one- versus two-assignment type assertions). |
ok calm yourself I misunderstood apologies |
Ok. That's understandable. If it were true, the "breaks API compatibility" card would clearly trump the desire to avoid gratuitous one-off, contrary-to-documented behavior. That's why the (mistaken?) claim of incompatible change comes across as such a big deal. |
and when I say "gratuitous one-off, contrary-to-documented" behavior, I would be glad to be proved wrong. I just don't think the conversation thus far on this issue has demonstrated that it's not a gratuitous deviation from documented behavior. Maybe it's the search for that answer that will bring about a satisfactory closing of this issue. Or, maybe it is what I think it is, and should be fixed to support calling the same way other Cgo functions can be called. |
I'm fine with handling an |
@ianlancetaylor help? I don't recognize the CL initials. I would be glad to roll up my sleeves and dig into this, because gratuitous inconsistency consistently bugs me. I'm reading https://golang.org/doc/contribute.html now as a first step. |
CL = "change list". Think "PR" (Pull request) or "patch". You found the right contributing docs. |
C.malloc is special because we guarantee it can never fail. I will send a CL documenting this. |
CL https://golang.org/cl/31811 mentions this issue. |
@rsc @ianlancetaylor thank you both for this. The CL https://golang.org/cl/31811 LGTM assuming https://golang.org/cl/31768 also is accepted at the same release. |
@sh4m1l65 Both changes will be in the Go 1.8 release. |
Please answer these questions before submitting your issue. Thanks!
go version
)?go env
)?I ran
cgom.go
whose source is at https://play.golang.org/p/lFkVjkjn5WIn cgo manual (https://golang.org/cmd/cgo/), the manual states that
I think C.malloc is C function. And Any C function may be called in a multiple assignemt context. Therefore, C.malloc should be called in a multiple assignment.
The idea was correct only if compilation would be successful.
Unfortunately, reality is not. C.malloc cannot be used in that way, even though free function can be used in that way.
C.malloc function seems not to be part of a set of C functions, because contraposition of "Any C function may be called in a multiple assignment context" is "if a function may not be called in a multiple context, then the function is not C function".
This contradicts usual idea that malloc should be C function. Go override some of special functions as malloc, etc. But these functions do not act like C functions.
The text was updated successfully, but these errors were encountered: