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

CGO: *_Ctype_char does not satisfy *any #69456

Closed
gucio321 opened this issue Sep 13, 2024 · 3 comments
Closed

CGO: *_Ctype_char does not satisfy *any #69456

gucio321 opened this issue Sep 13, 2024 · 3 comments

Comments

@gucio321
Copy link
Contributor

Go version

go version go1.23.1 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/username/.cache/go-build'
GOENV='/home/username/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/username/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/username/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/username/.config/go/telemetry'
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 -ffile-prefix-map=/tmp/go-build3506538111=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I want to write a function that converts C.char from Package A to C.char from Package B
I wrote function like that

func ConvPointer[RESULT, SOURCE *any](ptr SOURCE) RESULT {
        return (RESULT)(unsafe.Pointer(ptr))
}

What did you see happen?

when calling this function I get:

../../typewrapper/type_wrapper.go:66:37: *_Ctype_char does not satisfy *any (*_Ctype_char missing in *any)

What did you expect to see?

any as the name suggests means anything, so why C.char is not anything? Is there a reason for that?

@ianlancetaylor
Copy link
Member

That is how the language works. The type any is a name for the empty interface type interface{}. A value of type *C.char is a pointer to a C.char. A value of type *any is a pointer to a interface{}. The two are not compatible. See https://go.dev/doc/faq#convert_slice_of_interface, which is written in terms of []any but applies equally well to *any.

You can write your function as

func ConvPointer[ER, ES any, RESULT *ER, SOURCE *ES](ptr SOURCE) RESULT {
        return (RESULT)(unsafe.Pointer(ptr))
}

Closing because this is not a bug.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2024
@gucio321
Copy link
Contributor Author

oh ok, I missed that, thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants