]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/cgo: walk {FuncType,TypeSpec}.TypeParams fields
authorMatthew Dempsky <mdempsky@google.com>
Wed, 30 Nov 2022 20:31:30 +0000 (12:31 -0800)
committerGopher Robot <gobot@golang.org>
Wed, 30 Nov 2022 21:45:10 +0000 (21:45 +0000)
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>

misc/cgo/test/test.go
src/cmd/cgo/ast.go
src/cmd/cgo/ast_go1.go
src/cmd/cgo/ast_go118.go

index 109ef987f93023a4bba79219fecd74c9eeb2ab07..9d9b14ee74ec34f3cba077095d3c44000daea7cb 100644 (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{}
index c419699cb1a9ae6fd6b02667eedd9c6617e49909..81060c67edd79291f8b13cf45cf5ed0bb664625c 100644 (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:
index ce61d29095585c8f59f36f92c8cc969ca2249f7e..2f65f0f718356fd81210f441eef57c349a680f43 100644 (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
+}
index 9f759b8ee5f0ff76579e32efd3c824405bcd5244..ced30728dc9a798b63f4c15608ca914b993626db 100644 (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
+}