golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.31k stars 17.58k forks source link

cmd/cgo: pointers of Objective-C classes compatibility #7039

Open hajimehoshi opened 10 years ago

hajimehoshi commented 10 years ago
What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.

A pointer of an Objective-C class can't be assigned to a variable as a pointer of
another Objective-C class. For example:

package main

// #cgo CFLAGS: -x objective-c                                                          

//                                                                                      

// @class Foo;                                                                          

// @class Bar;                                                                          

import "C"

func main() {
        var foo *C.Foo
        var bar *C.Bar

        foo = bar // ?                                                                                                                                                                                                                                                          

        print(foo)
        print(bar)
}

What is the expected output?
Compile Error

What do you see instead?
Compile succeeded
("0x00x0" was printed)

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
Mac OS X 10.9

Which version are you using?  (run 'go version')
go version go1.2 darwin/amd64

Please provide any additional information below.
ianlancetaylor commented 10 years ago

Comment 1:

For the record, both foo and bar are given the Go type "*[0]byte", and as such one can
be assigned to the other.

Labels changed: added release-none, repo-main.

Status changed to Accepted.

mdempsky commented 10 years ago

Comment 2:

For what it's worth, Go 1.3 on linux/amd64 using either CC=gcc or CC=clang, C.Foo and
C.Bar are now rewritten as _Ctype_struct_Foo and _Ctype_struct_Bar, which are both
defined as "struct{}".  So the Go compiler now rejects the "foo = bar" assignment
because "*_Ctype_struct_Foo" and "*_Ctype_struct_Bar" are distinct types.
Thus I think the "expected output" from the issue report is now satisfied, though it
might be worthwhile if someone could verify on OS X too.