1
0
mirror of https://github.com/golang/go synced 2024-11-26 01:57:56 -07:00

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 <mdempsky@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Matthew Dempsky 2022-11-30 12:31:30 -08:00 committed by Gopher Robot
parent 3b3ab61692
commit 8c0256b398
4 changed files with 29 additions and 0 deletions

View File

@ -2295,3 +2295,9 @@ func test45451(t *testing.T) {
_ = reflect.New(typ)
t.Errorf("reflect.New(%v) should have panicked", typ)
}
// issue 52542
func func52542[T ~[]C.int]() {}
type type52542[T ~*C.float] struct{}

View File

@ -409,6 +409,9 @@ func (f *File) walk(x interface{}, context astContext, visit func(*File, interfa
case *ast.StructType:
f.walk(n.Fields, ctxField, visit)
case *ast.FuncType:
if tparams := funcTypeTypeParams(n); tparams != nil {
f.walk(tparams, ctxParam, visit)
}
f.walk(n.Params, ctxParam, visit)
if n.Results != nil {
f.walk(n.Results, ctxParam, visit)
@ -496,6 +499,9 @@ func (f *File) walk(x interface{}, context astContext, visit func(*File, interfa
f.walk(n.Values, ctxExpr, visit)
}
case *ast.TypeSpec:
if tparams := typeSpecTypeParams(n); tparams != nil {
f.walk(tparams, ctxParam, visit)
}
f.walk(&n.Type, ctxType, visit)
case *ast.BadDecl:

View File

@ -7,6 +7,7 @@
package main
import (
"go/ast"
"go/token"
)
@ -14,3 +15,11 @@ func (f *File) walkUnexpected(x interface{}, context astContext, visit func(*Fil
error_(token.NoPos, "unexpected type %T in walk", x)
panic("unexpected type")
}
func funcTypeTypeParams(n *ast.FuncType) *ast.FieldList {
return nil
}
func typeSpecTypeParams(n *ast.TypeSpec) *ast.FieldList {
return nil
}

View File

@ -22,3 +22,11 @@ func (f *File) walkUnexpected(x interface{}, context astContext, visit func(*Fil
f.walk(n.Indices, ctxExpr, visit)
}
}
func funcTypeTypeParams(n *ast.FuncType) *ast.FieldList {
return n.TypeParams
}
func typeSpecTypeParams(n *ast.TypeSpec) *ast.FieldList {
return n.TypeParams
}