From 170abddb9dffebec09759ea5e428a98df32bf3e7 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Thu, 28 Oct 2021 12:43:44 -0400 Subject: [PATCH] go/analysis/passes/stdmethods: add tests with generic receivers Ensure that the logic for detecting incorrect signatures of standard methods applies to methods on generic types. For golang/go#48704 Change-Id: I1862238ad1ff013137b12674fbc0068a7e8a3c60 Reviewed-on: https://go-review.googlesource.com/c/tools/+/359409 Trust: Robert Findley Run-TryBot: Robert Findley gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Tim King --- .../passes/stdmethods/stdmethods_test.go | 7 +++- .../testdata/src/typeparams/typeparams.go | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 go/analysis/passes/stdmethods/testdata/src/typeparams/typeparams.go diff --git a/go/analysis/passes/stdmethods/stdmethods_test.go b/go/analysis/passes/stdmethods/stdmethods_test.go index 60b1a53c77b..9bfa0327feb 100644 --- a/go/analysis/passes/stdmethods/stdmethods_test.go +++ b/go/analysis/passes/stdmethods/stdmethods_test.go @@ -9,11 +9,16 @@ import ( "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/go/analysis/passes/stdmethods" + "golang.org/x/tools/internal/typeparams" ) func Test(t *testing.T) { testdata := analysistest.TestData() - analysistest.Run(t, testdata, stdmethods.Analyzer, "a") + pkgs := []string{"a"} + if typeparams.Enabled { + pkgs = append(pkgs, "typeparams") + } + analysistest.Run(t, testdata, stdmethods.Analyzer, pkgs...) } func TestAnalyzeEncodingXML(t *testing.T) { diff --git a/go/analysis/passes/stdmethods/testdata/src/typeparams/typeparams.go b/go/analysis/passes/stdmethods/testdata/src/typeparams/typeparams.go new file mode 100644 index 00000000000..8ff8b350cb2 --- /dev/null +++ b/go/analysis/passes/stdmethods/testdata/src/typeparams/typeparams.go @@ -0,0 +1,41 @@ +// Copyright 2021 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 typeparams + +import "fmt" + +type T[P any] int + +func (T[_]) Scan(x fmt.ScanState, c byte) {} // want `should have signature Scan\(fmt\.ScanState, rune\) error` + +func (T[_]) Format(fmt.State, byte) {} // want `should have signature Format\(fmt.State, rune\)` + +type U[P any] int + +func (U[_]) Format(byte) {} // no error: first parameter must be fmt.State to trigger check + +func (U[P]) GobDecode(P) {} // want `should have signature GobDecode\(\[\]byte\) error` + +type V[P any] int // V does not implement error. + +func (V[_]) As() T[int] { return 0 } // ok - V is not an error +func (V[_]) Is() bool { return false } // ok - V is not an error +func (V[_]) Unwrap() int { return 0 } // ok - V is not an error + +type E[P any] int + +func (E[_]) Error() string { return "" } // E implements error. + +func (E[P]) As() {} // want `method As\(\) should have signature As\(interface{}\) bool` +func (E[_]) Is() {} // want `method Is\(\) should have signature Is\(error\) bool` +func (E[_]) Unwrap() {} // want `method Unwrap\(\) should have signature Unwrap\(\) error` + +type F[P any] int + +func (F[_]) Error() string { return "" } // Both F and *F implement error. + +func (*F[_]) As() {} // want `method As\(\) should have signature As\(interface{}\) bool` +func (*F[_]) Is() {} // want `method Is\(\) should have signature Is\(error\) bool` +func (*F[_]) Unwrap() {} // want `method Unwrap\(\) should have signature Unwrap\(\) error`