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

Core Foundation type fails cast unless source type is explicitly mentioned in code #24772

Closed
fivegreenapples opened this issue Apr 9, 2018 · 2 comments

Comments

@fivegreenapples
Copy link

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

go1.10 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ben/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/ben/Documents/Coding/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/gb/hp3qsnwd0jz64k6gpnnt2ws40000gn/T/go-build705521711=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Below code demonstrates the problem.

package cgotest

/*
#cgo LDFLAGS: -framework CoreFoundation
#include <CoreFoundation/CFBase.h>
#include <CoreFoundation/CFString.h>
*/
import "C"

// MyFunc demonstrates compilation problem.
func MyFunc() {

	var typeRef C.CFTypeRef

	cfStringRef := C.CFCopyDescription(typeRef)
	// This cast triggers 'cannot convert cfStringRef (type _Ctype_CFStringRef) to type _Ctype_CFTypeRef'
	defer C.CFRelease(C.CFTypeRef(cfStringRef))

	// Reference C.CFStringRef explicitly in code. Uncomment this line to allow compilation.
	// var _ C.CFStringRef

}

Wordy description...
I want to cast from C.CFStringRef to C.CFTypeRef in order to call C.CFRelease(...). This is a valid cast as both types are represented as uintptrs (as per https://go-review.googlesource.com/c/go/+/66332).

What is weird (and I believe a bug) is that the code does indeed compile if the C.CFStringRef type is mentioned in the Go code. i.e. uncomment the last line in that func and the compilation error goes away.

A possibly related thing that I also find weird is that you need the CFString.h include even though the CFStringRef type is defined in CFBase.h. Without that (and the last line uncommented) I get the same compilation error.

What did you expect to see?

Successful compilation

What did you see instead?

cannot convert cfStringRef (type _Ctype_CFStringRef) to type _Ctype_CFTypeRef

@randall77
Copy link
Contributor

I think this is a dup of #24161 .
You can use the empty var declaration as a workaround for now.

@KatelynHaworth
Copy link
Contributor

Just as a note, this is now fixed in 1.10.4 and 1.11

@golang golang locked and limited conversation to collaborators Oct 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants