Skip to content

Commit 8c0256b

Browse files
mdempskygopherbot
authored andcommitted
cmd/cgo: walk {FuncType,TypeSpec}.TypeParams fields
This CL updates the cgo tool to walk the TypeParams fields for function types and type declarations, so that C.xxx identifiers can appear within type parameter lists. Fixes #52542. Change-Id: Id02a88d529d50fe59b0a834f415c2575204ffd1f Reviewed-on: https://go-review.googlesource.com/c/go/+/453977 Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 3b3ab61 commit 8c0256b

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

misc/cgo/test/test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,3 +2295,9 @@ func test45451(t *testing.T) {
22952295
_ = reflect.New(typ)
22962296
t.Errorf("reflect.New(%v) should have panicked", typ)
22972297
}
2298+
2299+
// issue 52542
2300+
2301+
func func52542[T ~[]C.int]() {}
2302+
2303+
type type52542[T ~*C.float] struct{}

src/cmd/cgo/ast.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ func (f *File) walk(x interface{}, context astContext, visit func(*File, interfa
409409
case *ast.StructType:
410410
f.walk(n.Fields, ctxField, visit)
411411
case *ast.FuncType:
412+
if tparams := funcTypeTypeParams(n); tparams != nil {
413+
f.walk(tparams, ctxParam, visit)
414+
}
412415
f.walk(n.Params, ctxParam, visit)
413416
if n.Results != nil {
414417
f.walk(n.Results, ctxParam, visit)
@@ -496,6 +499,9 @@ func (f *File) walk(x interface{}, context astContext, visit func(*File, interfa
496499
f.walk(n.Values, ctxExpr, visit)
497500
}
498501
case *ast.TypeSpec:
502+
if tparams := typeSpecTypeParams(n); tparams != nil {
503+
f.walk(tparams, ctxParam, visit)
504+
}
499505
f.walk(&n.Type, ctxType, visit)
500506

501507
case *ast.BadDecl:

src/cmd/cgo/ast_go1.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@
77
package main
88

99
import (
10+
"go/ast"
1011
"go/token"
1112
)
1213

1314
func (f *File) walkUnexpected(x interface{}, context astContext, visit func(*File, interface{}, astContext)) {
1415
error_(token.NoPos, "unexpected type %T in walk", x)
1516
panic("unexpected type")
1617
}
18+
19+
func funcTypeTypeParams(n *ast.FuncType) *ast.FieldList {
20+
return nil
21+
}
22+
23+
func typeSpecTypeParams(n *ast.TypeSpec) *ast.FieldList {
24+
return nil
25+
}

src/cmd/cgo/ast_go118.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ func (f *File) walkUnexpected(x interface{}, context astContext, visit func(*Fil
2222
f.walk(n.Indices, ctxExpr, visit)
2323
}
2424
}
25+
26+
func funcTypeTypeParams(n *ast.FuncType) *ast.FieldList {
27+
return n.TypeParams
28+
}
29+
30+
func typeSpecTypeParams(n *ast.TypeSpec) *ast.FieldList {
31+
return n.TypeParams
32+
}

0 commit comments

Comments
 (0)