Skip to content

Commit fca3e5c

Browse files
committed
[dev.typeparams] cmd/compile: fix missing condition in usemethod
CL 330670 simplified usemethod, but dropped the previous condition to ensure the function have 1 or 2 result. This CL restore that condition, and also add a test for it. Change-Id: I434e3736785b43ceea0b386d8d9d01ad78a4ccd2 Reviewed-on: https://go-review.googlesource.com/c/go/+/336609 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 5ba0649 commit fca3e5c

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/cmd/compile/internal/walk/expr.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,14 @@ func usemethod(n *ir.CallExpr) {
957957
if t.NumParams() != 1 || t.Params().Field(0).Type.Kind() != pKind {
958958
return
959959
}
960-
if t.NumResults() == 2 && t.Results().Field(1).Type.Kind() != types.TBOOL {
960+
switch t.NumResults() {
961+
case 1:
962+
// ok
963+
case 2:
964+
if t.Results().Field(1).Type.Kind() != types.TBOOL {
965+
return
966+
}
967+
default:
961968
return
962969
}
963970

test/reflectmethod8.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// compile
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Make sure that the compiler can analyze non-reflect
8+
// Type.{Method,MethodByName} calls.
9+
10+
package p
11+
12+
type I interface {
13+
MethodByName(string)
14+
Method(int)
15+
}
16+
17+
type M struct{}
18+
19+
func (M) MethodByName(string) {}
20+
func (M) Method(int) {}
21+
22+
func f() {
23+
var m M
24+
I.MethodByName(m, "")
25+
I.Method(m, 42)
26+
}

0 commit comments

Comments
 (0)