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>
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: