Skip to content

Commit 170abdd

Browse files
committed
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 <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Tim King <[email protected]>
1 parent d6a9af8 commit 170abdd

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

go/analysis/passes/stdmethods/stdmethods_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ import (
99

1010
"golang.org/x/tools/go/analysis/analysistest"
1111
"golang.org/x/tools/go/analysis/passes/stdmethods"
12+
"golang.org/x/tools/internal/typeparams"
1213
)
1314

1415
func Test(t *testing.T) {
1516
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, stdmethods.Analyzer, "a")
17+
pkgs := []string{"a"}
18+
if typeparams.Enabled {
19+
pkgs = append(pkgs, "typeparams")
20+
}
21+
analysistest.Run(t, testdata, stdmethods.Analyzer, pkgs...)
1722
}
1823

1924
func TestAnalyzeEncodingXML(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package typeparams
6+
7+
import "fmt"
8+
9+
type T[P any] int
10+
11+
func (T[_]) Scan(x fmt.ScanState, c byte) {} // want `should have signature Scan\(fmt\.ScanState, rune\) error`
12+
13+
func (T[_]) Format(fmt.State, byte) {} // want `should have signature Format\(fmt.State, rune\)`
14+
15+
type U[P any] int
16+
17+
func (U[_]) Format(byte) {} // no error: first parameter must be fmt.State to trigger check
18+
19+
func (U[P]) GobDecode(P) {} // want `should have signature GobDecode\(\[\]byte\) error`
20+
21+
type V[P any] int // V does not implement error.
22+
23+
func (V[_]) As() T[int] { return 0 } // ok - V is not an error
24+
func (V[_]) Is() bool { return false } // ok - V is not an error
25+
func (V[_]) Unwrap() int { return 0 } // ok - V is not an error
26+
27+
type E[P any] int
28+
29+
func (E[_]) Error() string { return "" } // E implements error.
30+
31+
func (E[P]) As() {} // want `method As\(\) should have signature As\(interface{}\) bool`
32+
func (E[_]) Is() {} // want `method Is\(\) should have signature Is\(error\) bool`
33+
func (E[_]) Unwrap() {} // want `method Unwrap\(\) should have signature Unwrap\(\) error`
34+
35+
type F[P any] int
36+
37+
func (F[_]) Error() string { return "" } // Both F and *F implement error.
38+
39+
func (*F[_]) As() {} // want `method As\(\) should have signature As\(interface{}\) bool`
40+
func (*F[_]) Is() {} // want `method Is\(\) should have signature Is\(error\) bool`
41+
func (*F[_]) Unwrap() {} // want `method Unwrap\(\) should have signature Unwrap\(\) error`

0 commit comments

Comments
 (0)