Skip to content

Commit 37a2004

Browse files
mdempskygopherbot
authored andcommitted
cmd/compile: relax overly strict assertion
The assertion here was to make sure the newly constructed and typechecked expression selected the same receiver-qualified method, but in the case of anonymous receiver types we can actually end up with separate types.Field instances corresponding to each types.Type instance. In that case, the assertion spuriously failed. The fix here is to relax and assertion and just compare the method's name and type (including receiver type). Fixes #58563. Change-Id: I67d51ddb020e6ed52671473c93fc08f283a40886 Reviewed-on: https://go-review.googlesource.com/c/go/+/471676 Auto-Submit: Matthew Dempsky <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 70efe9f commit 37a2004

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

src/cmd/compile/internal/noder/reader.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,18 @@ func (r *reader) expr() (res ir.Node) {
21842184
}
21852185

21862186
n := typecheck.Expr(ir.NewSelectorExpr(pos, ir.OXDOT, recv, wrapperFn.Sel)).(*ir.SelectorExpr)
2187-
assert(n.Selection == wrapperFn.Selection)
2187+
2188+
// As a consistency check here, we make sure "n" selected the
2189+
// same method (represented by a types.Field) that wrapperFn
2190+
// selected. However, for anonymous receiver types, there can be
2191+
// multiple such types.Field instances (#58563). So we may need
2192+
// to fallback to making sure Sym and Type (including the
2193+
// receiver parameter's type) match.
2194+
if n.Selection != wrapperFn.Selection {
2195+
assert(n.Selection.Sym == wrapperFn.Selection.Sym)
2196+
assert(types.Identical(n.Selection.Type, wrapperFn.Selection.Type))
2197+
assert(types.Identical(n.Selection.Type.Recv().Type, wrapperFn.Selection.Type.Recv().Type))
2198+
}
21882199

21892200
wrapper := methodValueWrapper{
21902201
rcvr: n.X.Type(),

test/fixedbugs/issue58563.dir/a.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2023 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 a
6+
7+
func Start() interface{ Stop() } {
8+
return new(Stopper)
9+
}
10+
11+
type Stopper struct{}
12+
13+
func (s *Stopper) Stop() {}

test/fixedbugs/issue58563.dir/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2023 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 main
6+
7+
import "test/a"
8+
9+
func main() {
10+
stop := start()
11+
defer stop()
12+
}
13+
14+
func start() func() {
15+
return a.Start().Stop
16+
}

test/fixedbugs/issue58563.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compiledir
2+
3+
// Copyright 2023 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+
package ignored

0 commit comments

Comments
 (0)