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

cmd/cgo: const in c translate to go const without obey original type. #64923

Open
newacorn opened this issue Jan 2, 2024 · 1 comment
Open
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@newacorn
Copy link

newacorn commented Jan 2, 2024

Go version

go version go1.21.5 darwin/amd64

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

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/acorn/Library/Caches/go-build'
GOENV='/Users/acorn/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/acorn/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/acorn/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/acorn/workspace/programming/golang/go1.21.5'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/acorn/workspace/programming/golang/go1.21.5/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.21.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
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 -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/n1/750_y87d4cx826dsgx4rrd9r0000gn/T/go-build1840385617=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

reg2expand.go

package main

// size_t var1 = ~(size_t)0;
// const size_t const1 = ~(size_t)0;
import "C"
import "log"


func main() {
	println(C.var1)
	// 18446744073709551615
	println(C.const1)
	// -1
	log.Println(C.var1 == C.const1)
	// (_Ciconst_const1) (untyped int constant -1) overflows uint64
}
❯ go build ./reg2expand.go  
# command-line-arguments
./reg2expand.go:12:20: (_Ciconst_const1) (untyped int constant -1) overflows uint64
1. compile error happen when:

a unsigined type c const with a value bigger than go math.MaxInt.
when this translated const compare with same type c variable or const.

2.why

cgo translate a const in this case to a negative value.
_cgo_gotypes.go

var _Cvar_var1 *_Ctype_size_t = (*_Ctype_size_t)(unsafe.Pointer(&__cgo_var1))
const _Ciconst_const1 = -0x1
3. questiont source

c pcre2 library has a const named PCRE2_UNSET.

#define PCRE2_UNSET           (~(PCRE2_SIZE)0)

when check whether a group is set come with compiler error.

4. temp workaround

define a same type variable whith the const value.

package main

// #cgo pkg-config: libpcre2-8
// #define PCRE2_CODE_UNIT_WIDTH 8
// #include <pcre2.h>
// PCRE2_SIZE GOPCRE2_UNSET = PCRE2_UNSET;
// PCRE2_SIZE mockVectorIndex = PCRE2_UNSET;
import "C"

func main() {
	println(C.mockVectorIndex == C.GOPCRE2_UNSET)
}

What did you expect to see?

go tool cgo reg2expand.go

_cgo_gotypes.go

const _Ciconst_const1 = 18446744073709551615

What did you see instead?

go tool cgo reg2expand.go

_cgo_gotypes.go

const _Ciconst_const1 = -0x1
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 2, 2024
@mdempsky mdempsky added the NeedsFix The path to resolution is known, but the work has not been done. label Jan 2, 2024
@mdempsky mdempsky added this to the Backlog milestone Jan 2, 2024
@mdempsky
Copy link
Member

mdempsky commented Jan 2, 2024

I agree in principle that

// size_t var1 = ~(size_t)0;
// const size_t const1 = ~(size_t)0;
import "C"

should provide equal values when accessed via cgo, though there might be backwards compatibility concerns with fixing that now.

/cc @golang/compiler

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. NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

3 participants