-
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: cgo treats typedef and struct as different types #7786
Labels
Milestone
Comments
Yes. On FreeBSD 11 go "1.2.1 freebsd/amd64" works. I updated the test case. Attachments:
|
Confirmed. This is what I see using clang on linux/amd64 lucky(~/src/usb) % go build -x WORK=/tmp/go-build477308532 mkdir -p $WORK/usb/_obj/ mkdir -p $WORK/ cd /home/dfc/src/usb CGO_LDFLAGS="-g" "-O2" /home/dfc/go/pkg/tool/linux_amd64/cgo -objdir $WORK/usb/_obj/ -- -I $WORK/usb/_obj/ usb.go /home/dfc/go/pkg/tool/linux_amd64/6c -F -V -w -trimpath $WORK -I $WORK/usb/_obj/ -I /home/dfc/go/pkg/linux_amd64 -o $WORK/usb/_obj/_cgo_defun.6 -D GOOS_linux -D GOARCH_amd64 $WORK/usb/_obj/_cgo_defun.c clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -print-libgcc-file-name clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -I $WORK/usb/_obj/ -g -O2 -o $WORK/usb/_obj/_cgo_main.o -c $WORK/usb/_obj/_cgo_main.c clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -I $WORK/usb/_obj/ -g -O2 -o $WORK/usb/_obj/_cgo_export.o -c $WORK/usb/_obj/_cgo_export.c clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -I $WORK/usb/_obj/ -g -O2 -o $WORK/usb/_obj/usb.cgo2.o -c $WORK/usb/_obj/usb.cgo2.c clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -o $WORK/usb/_obj/_cgo_.o $WORK/usb/_obj/_cgo_main.o $WORK/usb/_obj/_cgo_export.o $WORK/usb/_obj/usb.cgo2.o -g -O2 /home/dfc/go/pkg/tool/linux_amd64/cgo -objdir $WORK/usb/_obj/ -dynimport $WORK/usb/_obj/_cgo_.o -dynout $WORK/usb/_obj/_cgo_import.c /home/dfc/go/pkg/tool/linux_amd64/6c -F -V -w -trimpath $WORK -I $WORK/usb/_obj/ -I /home/dfc/go/pkg/linux_amd64 -o $WORK/usb/_obj/_cgo_import.6 -D GOOS_linux -D GOARCH_amd64 $WORK/usb/_obj/_cgo_import.c clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -o $WORK/usb/_obj/_all.o $WORK/usb/_obj/_cgo_export.o $WORK/usb/_obj/usb.cgo2.o -g -O2 -Wl,-r -nostdlib /usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a /home/dfc/go/pkg/tool/linux_amd64/6g -o $WORK/usb.a -trimpath $WORK -p usb -D _/home/dfc/src/usb -I $WORK -pack $WORK/usb/_obj/_cgo_gotypes.go $WORK/usb/_obj/usb.cgo1.go # usb ./usb.go:16[/tmp/go-build477308532/usb/_obj/usb.cgo1.go:18]: cannot use c.ctx (type *_Ctype_libusb_context) as type *[0]byte in argument to _Cfunc_libusb_exit It's probably something clang is doing wrong, this isn't the first time. Status changed to Accepted. |
Have a project, that fails too with 1.3: https://github.com/orofarne/go-mapnik With gcc, not clang. Works with 1.2.1. ..... ./mapnik.go:74: cannot use p.p (type *[0]byte) as type *C.mapnik_projection_t in argument to _Cfunc_mapnik_projection_free ./mapnik.go:80: cannot use p.p (type *[0]byte) as type *C.mapnik_projection_t in argument to _Cfunc_mapnik_projection_forward ./mapnik.go:90: cannot use _Cfunc_mapnik_map(C.uint(width), C.uint(height)) (type *C.mapnik_map_t) as type *[0]byte in field value ..... But I can't to reduce problem to minimal example. |
I can also confirm this issue moving from 1.2.1 to 1.3 (see https://code.google.com/p/azul3d/issues/detail?id=50). ... ../../azul3d.org/v0/chippy/wrappers/x11/glx.go:93: cannot use d.voidPtr() (type *[0]byte) as type *C.Display in argument to _Cfunc_glXCreateNewContext ../../azul3d.org/v0/chippy/wrappers/x11/glx.go:105: cannot use d.voidPtr() (type *[0]byte) as type *C.Display in argument to _Cfunc_glXDestroyContext ... |
this new behavior is introduced by https://golang.org/cl/76450043 |
I think we should try to make this work, although I am not sure how. Here is the simplified case: package usb // struct libusb_context; // typedef struct libusb_context libusb_context; // void libusb_exit(struct libusb_context *ctx) {}; import "C" type Context struct { ctx *C.libusb_context done chan struct{} } func (c *Context) Close() error { close(c.done) if c.ctx != nil { C.libusb_exit(c.ctx) } c.ctx = nil return nil } The problem is that c.txt is a *C.libusb_context but libusb_exit is defined to take a *C.struct_libusb_context. One is using the typedef name and one is using the struct name directly. Cgo treats those as different. We've had heuristics in the past to try to merge them but maybe they were removed or maybe they never worked, and the [0]byte bug was keeping things working incorrectly. |
CL https://golang.org/cl/98580046 mentions this issue. |
Owner changed to @rsc. Status changed to Started. |
This issue was closed by revision 0782ee3. Status changed to Fixed. |
wheatman
pushed a commit
to wheatman/go-akaros
that referenced
this issue
Jun 25, 2018
…eable For incomplete struct S, C.T and C.struct_S were interchangeable in Go 1.2 and earlier, because all incomplete types were interchangeable (even C.struct_S1 and C.struct_S2). CL 76450043, which fixed issue 7409, made different incomplete types different from Go's point of view, so that they were no longer completely interchangeable. However, imprecision about C.T and C.struct_S - really the same underlying C type - is the one behavior enabled by the bug that is most likely to be depended on by existing cgo code. Explicitly allow it, to keep that code working. Fixes golang#7786. LGTM=iant, r R=golang-codereviews, iant, r CC=golang-codereviews https://golang.org/cl/98580046
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
by Guxianjie:
Attachments:
The text was updated successfully, but these errors were encountered: