test: add a couple of pointer method test cases

Change-Id: If91c8a61fcda93eeb5affe51ef8f537736a28bce
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/768839
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Ian Lance Taylor 2020-06-11 14:32:10 -07:00 committed by Robert Griesemer
parent d65e7b7b0f
commit 09a02162ee
2 changed files with 72 additions and 0 deletions

29
test/gen/err003.go2 Normal file
View File

@ -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"
}

43
test/gen/g011.go2 Normal file
View File

@ -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})
}
}