diff --git a/test/gen/err003.go2 b/test/gen/err003.go2 new file mode 100644 index 0000000000..20a9f9701c --- /dev/null +++ b/test/gen/err003.go2 @@ -0,0 +1,29 @@ +// 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. + +package p + +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() { + FromStrings(Settable)([]string{"1"}) // ERROR "Settable does not satisfy Setter.*method Set" +} diff --git a/test/gen/g011.go2 b/test/gen/g011.go2 new file mode 100644 index 0000000000..92b7091904 --- /dev/null +++ b/test/gen/g011.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 Setter interface { + Set(string) +} + +func FromStrings(type *T Setter)(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) + } + 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 := FromStrings(Settable)([]string{"1"}) + if len(s) != 1 || s[0] != 1 { + log.Fatalf("got %v, want %v", s, []int{1}) + } +}