-
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/cgo: compiler accepts invalid declaration of methods on aliases to C types #60725
Comments
@alandonovan pointed out this hole in the current cgo detection of methods on C types, and I said it didn't seem worth worrying about, because who would put a method on a type alias? Interesting to see that people actually do this. If anybody sees a simple way to fix this, by all means go for it. |
Clearly the fix has to be done in the compiler (since it requires symbol resolution), which I think means it will generally be possible to hand-craft some valid Go source file that that spuriously triggers the error (since cgo-generated source has no special privilege). I think this means that any approach is going to be at best a heuristic and that false positives are possible. That said, false positives (artificially weird code that doesn't compile when it should) seem less important than false negatives (such as this issue). Some clues we could use for a heuristic:
Some combination of these, plus a test that the error is positively produced, is probably good enough. |
Just a note that I don't think we really need a heuristic to detect cgo-generated code. The cmd/go tool knows when it is compiling a Go file generated by cmd/cgo, and it could pass a special option to cmd/compile indicating that. Of course other people could use |
Change https://go.dev/cl/503395 mentions this issue: |
Change https://go.dev/cl/503397 mentions this issue: |
Change https://go.dev/cl/503396 mentions this issue: |
Change https://go.dev/cl/503596 mentions this issue: |
this fix is simple enough. Is it ok? @ianlancetaylor |
I think this condition is simple to fix this.
|
Well spotted! I didn't realize the compiler already had an official heuristic for isCgoGeneratedFile. Let's use that. |
@ianlancetaylor is this condition ok,isCgoGeneratedFile(T.obj.Pos()) && strings.HasPrefix(T.obj.Name(), "Ctype") ? |
@cuiweixie Yes. Thanks. |
This is another very commonly used package that will no longer compile using Go 1.21: bugst/go-serial#162 |
At the risk of sounding flippant, I might rephrase that as "another package that got away with invalid code". Fortunately the problematic cases all seem to be internal so it should be an easy fix. |
This is needed starting with Go 1.21 due to stricter enforcement of rules about adding methods to existing types, now including C types. See golang/go#60725 for further details. Signed-off-by: deadprogram <ron@hybridgroup.com>
@adonovan indeed, I submitted a PR to that repo with fix. |
This is needed starting with Go 1.21 due to stricter enforcement of rules about adding methods to existing types, now including C types. See golang/go#60725 for further details. Signed-off-by: deadprogram <ron@hybridgroup.com>
Reassigned to me for 1.24 to make it visible. CL exists. Should review. |
Pending CL CL 503596 still needs some work. |
Change https://go.dev/cl/629715 mentions this issue: |
@eliasnaur The pattern you are referring to in your example program appears (or appeared) in gioui.org/app/os_x11.go (see #59944). We fixed a crash with respect to that case. As the author of Gio, presumably you do not rely on this mechanism anymore since you filed this (current) issue? We are wondering if we can remove support from the compiler and make it an error going forward (cgo behavior is not covered by the Go 1 compatibility guarantee). |
Absolutely, go ahead and make it an error. Thank you. The current Gio code does rely on this behaviour, but I'll update it well before Go 1.24 is released if this issue is fixed. |
Change https://go.dev/cl/630335 mentions this issue: |
Follow-up on CL 629715. For #60725. Change-Id: I1b980ad44f73550b633c74fc881c70255e7d8565 Reviewed-on: https://go-review.googlesource.com/c/go/+/630335 Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Bypass: Robert Griesemer <gri@google.com>
Change https://go.dev/cl/630395 mentions this issue: |
Method receivers that denote cgo-generated types are not permitted per issues #60725 and #57926. There's no need to collect such methods in the first place. Simplify Checker.resolveBaseTypeName so that it doesn't find a base type name in these cases. Also, simplify the test case for issue #59944 and update it to use current cgo-generated output. For #60725. For #57926. For #59944. Change-Id: I70594daebc3d4d594c5b06be138f66f8927b0e58 Reviewed-on: https://go-review.googlesource.com/c/go/+/630395 Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Griesemer <gri@google.com>
This program successfully runs with Go 1.21:
I believe this program is invalid for the same reason as the program in #57926. Note that in contrast to that issue, this program uses an alias to define a method on a C type.
The text was updated successfully, but these errors were encountered: