From c2d8b59433bd484a1047786edeaac694a576c379 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Fri, 10 Apr 2020 12:25:39 -0700 Subject: [PATCH] go/go2go: record types of instantiated AST types Otherwise we don't know the type of an instantiated type in an instantiated function. Change-Id: If7eb75dd920a3c9fc1b6992addcd3b91b6d425a9 Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/715718 Reviewed-by: Ian Lance Taylor --- src/go/go2go/instantiate.go | 14 ++++++-------- test/gen/g007.go2 | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 test/gen/g007.go2 diff --git a/src/go/go2go/instantiate.go b/src/go/go2go/instantiate.go index 1090465365..9a4c904bda 100644 --- a/src/go/go2go/instantiate.go +++ b/src/go/go2go/instantiate.go @@ -760,7 +760,7 @@ func (t *translator) instantiateExpr(ta *typeArgs, e ast.Expr) ast.Expr { if ln == e.Len && elt == e.Elt { return e } - return &ast.ArrayType{ + r = &ast.ArrayType{ Lbrack: e.Lbrack, Len: ln, Elt: elt, @@ -770,7 +770,7 @@ func (t *translator) instantiateExpr(ta *typeArgs, e ast.Expr) ast.Expr { if fields == e.Fields { return e } - return &ast.StructType{ + r = &ast.StructType{ Struct: e.Struct, Fields: fields, Incomplete: e.Incomplete, @@ -781,7 +781,7 @@ func (t *translator) instantiateExpr(ta *typeArgs, e ast.Expr) ast.Expr { if e.TParams == nil && params == e.Params && results == e.Results { return e } - return &ast.FuncType{ + r = &ast.FuncType{ Func: e.Func, TParams: nil, Params: params, @@ -794,7 +794,7 @@ func (t *translator) instantiateExpr(ta *typeArgs, e ast.Expr) ast.Expr { if methods == e.Methods && !typesChanged { return e } - return &ast.InterfaceType{ + r = &ast.InterfaceType{ Interface: e.Interface, Methods: mergeFieldList(methods, types), Incomplete: e.Incomplete, @@ -805,7 +805,7 @@ func (t *translator) instantiateExpr(ta *typeArgs, e ast.Expr) ast.Expr { if key == e.Key && value == e.Value { return e } - return &ast.MapType{ + r = &ast.MapType{ Map: e.Map, Key: key, Value: value, @@ -815,7 +815,7 @@ func (t *translator) instantiateExpr(ta *typeArgs, e ast.Expr) ast.Expr { if value == e.Value { return e } - return &ast.ChanType{ + r = &ast.ChanType{ Begin: e.Begin, Arrow: e.Arrow, Dir: e.Dir, @@ -825,8 +825,6 @@ func (t *translator) instantiateExpr(ta *typeArgs, e ast.Expr) ast.Expr { panic(fmt.Sprintf("unimplemented Expr %T", e)) } - // We fall down to here for expressions that are not types. - if et := t.lookupType(e); et != nil { t.setType(r, t.instantiateType(ta, et)) } diff --git a/test/gen/g007.go2 b/test/gen/g007.go2 new file mode 100644 index 0000000000..c9d72b3888 --- /dev/null +++ b/test/gen/g007.go2 @@ -0,0 +1,20 @@ +// compile + +// 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. + +// Using a type literal based on a type parameter as a type argument +// in a result. + +package p + +type S(type T) struct { + f T +} + +func SliceS(type T)() S([]T) { + return S([]T){nil} +} + +var V = SliceS(int)()