Skip to content
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

runtime/cgo: bitfields not supported #59982

Closed
gucio321 opened this issue May 4, 2023 · 3 comments
Closed

runtime/cgo: bitfields not supported #59982

gucio321 opened this issue May 4, 2023 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@gucio321
Copy link
Contributor

gucio321 commented May 4, 2023

What version of Go are you using (go version)?

$ go version
go version go1.20.2 linux/amd64

Does this issue reproduce with the latest release?

yep

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/me/.cache/go-build"
GOENV="/home/me/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/me/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/me/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2059306280=/tmp/go-build -gno-record-gcc-switches"

What did you do?

consider the following code:

package main

/*
typedef struct Foo Foo;
struct Foo {
	int Bar;
};
*/
import "C"
import "fmt"

func main() {
	f := new(C.Foo)
	f.Bar = C.int(5)
	fmt.Println(f.Bar)
}

and now edit C code in the following way:

typedef struct Foo Foo;
struct Foo {
        int Bar : 1;
};

What did you expect to see?

everything should work

What did you see instead?

# command-line-arguments
./main.go:14:4: f.Bar undefined (type *_Ctype_struct_Foo has no field or method Bar)
./main.go:15:16: f.Bar undefined (type *_Ctype_struct_Foo has no field or method Bar)
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label May 4, 2023
@ianlancetaylor
Copy link
Contributor

Unfortunately, this simply isn't possible, as Go does not support bitfields. There is no way to represent that C struct in Go.

What you can do is write tiny functions in the C prologue that get and set the value of the field

Closing because there is nothing we can to fix this.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale May 4, 2023
@gucio321
Copy link
Contributor Author

gucio321 commented May 4, 2023

@ianlancetaylor well I don't insist on implementing bitfields in go, but these defined in C could be exported to GO anyway, couldn't they? If something exceeds bitfield size, C will throw panic, right?

@ianlancetaylor
Copy link
Contributor

How could they be exported to Go? The Go code that refers to these fields is ordinary Go code. The Go compiler is not a C compiler. The Go compiler does not support bitfields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants