From 09a02162eeea33cebd6ea8eb025c95d5b67d42dc Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 11 Jun 2020 14:32:10 -0700 Subject: [PATCH] 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 --- test/gen/err003.go2 | 29 +++++++++++++++++++++++++++++ test/gen/g011.go2 | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 test/gen/err003.go2 create mode 100644 test/gen/g011.go2 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}) + } +}