Skip to content

Commit 019ad98

Browse files
committed
cmd/cgo: update to handle ast.IndexListExpr
Allows cgo to work with generics. Updates #47781. Change-Id: Id1a5d1a0a8193c5b157e3e671b1490d687d10384 Reviewed-on: https://go-review.googlesource.com/c/go/+/353882 Trust: Matthew Dempsky <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent be571a3 commit 019ad98

File tree

6 files changed

+62
-4
lines changed

6 files changed

+62
-4
lines changed

misc/cgo/test/typeparam.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package cgotest
6+
7+
// #include <stddef.h>
8+
import "C"
9+
10+
func generic[T, U any](t T, u U) {}
11+
12+
func useGeneric() {
13+
const zero C.size_t = 0
14+
15+
generic(zero, zero)
16+
generic[C.size_t, C.size_t](0, 0)
17+
}

misc/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// directory.)
99
module misc
1010

11-
go 1.12
11+
go 1.18

src/cmd/cgo/ast.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ func (f *File) walk(x interface{}, context astContext, visit func(*File, interfa
338338

339339
// everything else just recurs
340340
default:
341-
error_(token.NoPos, "unexpected type %T in walk", x)
342-
panic("unexpected type")
341+
f.walkUnexpected(x, context, visit)
343342

344343
case nil:
345344

src/cmd/cgo/ast_go1.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !go1.18
6+
// +build !go1.18
7+
8+
package main
9+
10+
import (
11+
"go/token"
12+
)
13+
14+
func (f *File) walkUnexpected(x interface{}, context astContext, visit func(*File, interface{}, astContext)) {
15+
error_(token.NoPos, "unexpected type %T in walk", x)
16+
panic("unexpected type")
17+
}

src/cmd/cgo/ast_go118.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build go1.18
6+
// +build go1.18
7+
8+
package main
9+
10+
import (
11+
"go/ast"
12+
"go/token"
13+
)
14+
15+
func (f *File) walkUnexpected(x interface{}, context astContext, visit func(*File, interface{}, astContext)) {
16+
switch n := x.(type) {
17+
default:
18+
error_(token.NoPos, "unexpected type %T in walk", x)
19+
panic("unexpected type")
20+
21+
case *ast.IndexListExpr:
22+
f.walk(&n.X, ctxExpr, visit)
23+
f.walk(n.Indices, ctxExpr, visit)
24+
}
25+
}

src/cmd/cgo/gcc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ func (p *Package) rewriteName(f *File, r *Ref, addPosition bool) ast.Expr {
15061506
Args: []ast.Expr{getNewIdent(name.Mangle)},
15071507
}
15081508
case "type":
1509-
// Okay - might be new(T)
1509+
// Okay - might be new(T), T(x), Generic[T], etc.
15101510
if r.Name.Type == nil {
15111511
error_(r.Pos(), "expression C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
15121512
}

0 commit comments

Comments
 (0)