Skip to content

Commit

Permalink
[dev.go2go] cmd/go2go: switch to unified parameter syntax
Browse files Browse the repository at this point in the history
Type and ordinary parameter lists use the same syntax except
for the square brackets or parentheses, respectively.

Change-Id: I6fb93e93bbfc649a2c1ef195d130e0dcee212c79
Reviewed-on: https://go-review.googlesource.com/c/go/+/250717
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
griesemer committed Aug 26, 2020
1 parent 1964d56 commit 27c9d6e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/cmd/go2go/go2go_test.go
Expand Up @@ -198,7 +198,7 @@ package main
import "fmt"
func PrintSlice(type Elem)(s []Elem) {
func PrintSlice[Elem any](s []Elem) {
for _, v := range s {
fmt.Println(v)
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestTransitiveGo1(t *testing.T) {
testFiles{
{
"a/a.go2",
`package a; func ident(type T)(v T) T { return v }; func F1(v int) int { return ident(v) }`,
`package a; func ident[T any](v T) T { return v }; func F1(v int) int { return ident(v) }`,
},
{
"b/b.go",
Expand Down
4 changes: 2 additions & 2 deletions src/go/go2go/go2go.go
Expand Up @@ -126,7 +126,7 @@ func rewriteFilesInPath(importer *Importer, importPath, dir string, go2files []s
// for error messages.
func RewriteBuffer(importer *Importer, filename string, file []byte) ([]byte, error) {
fset := token.NewFileSet()
pf, err := parser.ParseFile(fset, filename, file, 0)
pf, err := parser.ParseFile(fset, filename, file, parser.UnifiedParamLists)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func checkGoFile(dir, f string) error {
func parseFiles(importer *Importer, dir string, go2files []string, fset *token.FileSet) ([]*ast.Package, error) {
pkgs := make(map[string]*ast.Package)
for _, go2f := range go2files {
var mode parser.Mode
mode := parser.UnifiedParamLists // overrides UseBrackets
if importer.UseBrackets {
mode = parser.UseBrackets
}
Expand Down
15 changes: 9 additions & 6 deletions test/gen/g011.go2
Expand Up @@ -11,16 +11,19 @@ import (
"strconv"
)

type Setter interface {
type Setter[B any] interface {
Set(string)
type *B
}

func FromStrings[*T Setter](s []string) []T {
func FromStrings[T any, PT Setter[T]](s []string) []T {
result := make([]T, len(s))
for i, v := range s {
// result[i] is an addressable value of type T,
// so it's OK to call Set.
result[i].Set(v)
// The type of &result[i] is *T which is in the type list
// of Setter, so we can convert it to PT.
p := PT(&result[i])
// PT has a Set method.
p.Set(v)
}
return result
}
Expand All @@ -36,7 +39,7 @@ func (p *Settable) Set(s string) {
}

func main() {
s := FromStrings[Settable]([]string{"1"})
s := FromStrings[Settable, *Settable]([]string{"1"})
if len(s) != 1 || s[0] != 1 {
log.Fatalf("got %v, want %v", s, []int{1})
}
Expand Down
2 changes: 1 addition & 1 deletion test/gen/g041.go2
Expand Up @@ -7,6 +7,6 @@
// Issue 40015.
package p

func F1[type](s []int) {}
func F1[](s []int) {}

func F2() { F1([]int{1, 2}) }
4 changes: 2 additions & 2 deletions test/gen/g043.go2
Expand Up @@ -17,11 +17,11 @@ type Number interface {

type MySlice []int

type SC[type E] interface {
type SC[E any] interface {
type []E
}

func DoubleDefined[type S SC[E], E Number](s S) S {
func DoubleDefined[S SC[E], E Number](s S) S {
r := make(S, len(s))
for i, v := range s {
r[i] = v + v
Expand Down

0 comments on commit 27c9d6e

Please sign in to comment.