Skip to content

Commit ba1f629

Browse files
runtime/cgo: add support for any param and return type
1 parent c58d075 commit ba1f629

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

src/cmd/cgo/gcc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,9 @@ func (p *Package) hasPointer(f *File, t ast.Expr, top bool) bool {
11211121
if t.Name == "error" {
11221122
return true
11231123
}
1124+
if t.Name == "any" {
1125+
return true
1126+
}
11241127
if goTypes[t.Name] != nil {
11251128
return false
11261129
}

src/cmd/cgo/internal/test/cgo_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func TestSetEnv(t *testing.T) { testSetEnv(t) }
106106
func TestThreadLock(t *testing.T) { testThreadLockFunc(t) }
107107
func TestUnsignedInt(t *testing.T) { testUnsignedInt(t) }
108108
func TestZeroArgCallback(t *testing.T) { testZeroArgCallback(t) }
109+
func Test76340(t *testing.T) { test76340(t) }
109110

110111
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
111112
func BenchmarkGoString(b *testing.B) { benchGoString(b) }

src/cmd/cgo/internal/test/test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,18 @@ typedef struct {
953953
} issue69086struct;
954954
static int issue690861(issue69086struct* p) { p->b = 1234; return p->c; }
955955
static int issue690862(unsigned long ul1, unsigned long ul2, unsigned int u, issue69086struct s) { return (int)(s.b); }
956+
957+
typedef struct { void *t; void *v; } GoInterface;
958+
extern int exportAny76340Param(GoInterface);
959+
extern GoInterface exportAny76340Return(int);
960+
961+
int issue76340testFromC(GoInterface obj) {
962+
return exportAny76340Param(obj);
963+
}
964+
965+
GoInterface issue76340returnFromC(int val) {
966+
return exportAny76340Return(val);
967+
}
956968
*/
957969
import "C"
958970

@@ -2396,3 +2408,22 @@ func test69086(t *testing.T) {
23962408
t.Errorf("call: got %d, want 1234", got)
23972409
}
23982410
}
2411+
2412+
// Issue 76340.
2413+
func test76340(t *testing.T) {
2414+
var emptyInterface C.GoInterface
2415+
r1 := C.issue76340testFromC(emptyInterface)
2416+
if r1 != 0 {
2417+
t.Errorf("issue76340testFromC with nil interface: got %d, want 0", r1)
2418+
}
2419+
2420+
r2 := C.issue76340returnFromC(42)
2421+
if r2.t == nil && r2.v == nil {
2422+
t.Error("issue76340returnFromC(42) returned nil interface")
2423+
}
2424+
2425+
r3 := C.issue76340returnFromC(0)
2426+
if r3.t != nil || r3.v != nil {
2427+
t.Errorf("issue76340returnFromC(0) returned non-nil interface: got %v, want nil", r3)
2428+
}
2429+
}

src/cmd/cgo/internal/test/testx.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,21 @@ func test49633(t *testing.T) {
595595
t.Errorf("msg = %q, want 'hello'", v.msg)
596596
}
597597
}
598+
599+
//export exportAny76340Param
600+
func exportAny76340Param(obj any) C.int {
601+
if obj == nil {
602+
return 0
603+
}
604+
605+
return 1
606+
}
607+
608+
//export exportAny76340Return
609+
func exportAny76340Return(val C.int) any {
610+
if val == 0 {
611+
return nil
612+
}
613+
614+
return int(val)
615+
}

src/cmd/cgo/out.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,9 @@ func (p *Package) doCgoType(e ast.Expr, m map[ast.Expr]bool) *Type {
15281528
if t.Name == "error" {
15291529
return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")}
15301530
}
1531+
if t.Name == "any" {
1532+
return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")}
1533+
}
15311534
if r, ok := goTypes[t.Name]; ok {
15321535
return goTypesFixup(r)
15331536
}

src/runtime/cgocall.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,9 @@ func cgoCheckResult(val any) {
795795

796796
ep := efaceOf(&val)
797797
t := ep._type
798+
if t == nil {
799+
return
800+
}
798801
cgoCheckArg(t, ep.data, !t.IsDirectIface(), false, cgoResultFail)
799802
}
800803

0 commit comments

Comments
 (0)