diff --git a/test/gen/g008.go2 b/test/gen/g008.go2 new file mode 100644 index 0000000000..72c99be0e6 --- /dev/null +++ b/test/gen/g008.go2 @@ -0,0 +1,41 @@ +// run + +// 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. + +package main + +type Setter interface { + Set(string) +} + +func FromStrings(type T Setter)(s []string) []T { + result := make([]T, len(s)) + for i, v := range s { + result[i].Set(v) + } + return result +} + +type Settable int + +func (p *Settable) Set(s string) { + *p = 0 +} + +func F() { + defer func() { + if recover() == nil { + panic("did not panic as expected") + } + }() + // This should type check but should panic at run time, + // because it will make a slice of *Settable and then call + // Set on a nil value. + FromStrings(*Settable)([]string{"1"}) +} + +func main() { + F() +} diff --git a/test/gen/g009.go2 b/test/gen/g009.go2 new file mode 100644 index 0000000000..bc376ccad5 --- /dev/null +++ b/test/gen/g009.go2 @@ -0,0 +1,43 @@ +// run + +// 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. + +package main + +import ( + "log" + "strconv" +) + +type Setter2(type B) interface { + Set(string) + type *B +} + +func FromStrings2(type T interface{}, PT Setter2(T))(s []string) []T { + result := make([]T, len(s)) + for i, v := range s { + p := PT(&result[i]) + p.Set(v) + } + return result +} + +type Settable int + +func (p *Settable) Set(s string) { + i, err := strconv.Atoi(s) + if err != nil { + log.Fatal(err) + } + *p = Settable(i) +} + +func main() { + s := FromStrings2(Settable, *Settable)([]string{"1"}) + if len(s) != 1 || s[0] != 1 { + log.Fatalf("got %v, want %v", s, []int{1}) + } +} diff --git a/test/gen/g010.go2 b/test/gen/g010.go2 new file mode 100644 index 0000000000..b28780b791 --- /dev/null +++ b/test/gen/g010.go2 @@ -0,0 +1,38 @@ +// run + +// 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. + +package main + +import ( + "log" + "strconv" +) + +func FromStrings3(type T)(s []string, set func(*T, string)) []T { + results := make([]T, len(s)) + for i, v := range s { + set(&results[i], v) + } + return results +} + +type Settable int + +func (p *Settable) Set(s string) { + i, err := strconv.Atoi(s) + if err != nil { + log.Fatal(err) + } + *p = Settable(i) +} + +func main() { + s := FromStrings3(Settable)([]string{"1"}, + func(p *Settable, s string) { p.Set(s) }) + if len(s) != 1 || s[0] != 1 { + log.Fatalf("got %v, want %v", s, []int{1}) + } +}