From dfb538cec8bd63ed218f44d59e53cfb1e4bcb62c Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 22 Jun 2020 16:28:40 -0700 Subject: [PATCH] [dev.go2go] go/go2go: don't permit underscore as type argument The go/types package doesn't give us an easy way to get the type, so just reject it. Fixes #39743 Change-Id: I5404c10baede0fd2cf67980b06fbebd214a50dff Reviewed-on: https://go-review.googlesource.com/c/go/+/239382 Reviewed-by: Ian Lance Taylor --- src/go/go2go/rewrite.go | 7 +++++++ test/gen/err006.go2 | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/gen/err006.go2 diff --git a/src/go/go2go/rewrite.go b/src/go/go2go/rewrite.go index 7735772c44..36a4ed58b5 100644 --- a/src/go/go2go/rewrite.go +++ b/src/go/go2go/rewrite.go @@ -857,6 +857,9 @@ func (t *translator) translateTypeInstantiation(pe *ast.Expr) { qid := t.instantiatedIdent(call) typ := t.lookupType(call.Fun).(*types.Named) argList, typeList, typeArgs := t.instantiationTypes(call) + if t.err != nil { + return + } if !typeArgs { panic("no type arguments for type") } @@ -950,6 +953,10 @@ func (t *translator) instantiationTypes(call *ast.CallExpr) (argList []ast.Expr, argList = call.Args typeList = make([]types.Type, 0, len(argList)) for _, arg := range argList { + if id, ok := arg.(*ast.Ident); ok && id.Name == "_" { + t.err = fmt.Errorf("%s: go2go tool does not support using _ here", t.fset.Position(arg.Pos())) + return + } if at := t.lookupType(arg); at == nil { panic(fmt.Sprintf("%s: no type found for %T %v", t.fset.Position(arg.Pos()), arg, arg)) } else { diff --git a/test/gen/err006.go2 b/test/gen/err006.go2 new file mode 100644 index 0000000000..163369c50e --- /dev/null +++ b/test/gen/err006.go2 @@ -0,0 +1,16 @@ +// errorcheck + +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Issue 39743. +package p + +type S(type T) struct{} + +func (s S(_)) M() {} // ERROR "_" + +func F() { + S(int){}.M() +}