Skip to content

Commit 26eeaec

Browse files
mdempskygopherbot
authored andcommitted
[release-branch.go1.20] 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 #58776. 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]> (cherry picked from commit 37a2004) Reviewed-on: https://go-review.googlesource.com/c/go/+/472620 Auto-Submit: Dmitri Shuralyov <[email protected]> TryBot-Bypass: Dmitri Shuralyov <[email protected]>
1 parent 9629fa1 commit 26eeaec

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2194,7 +2194,18 @@ func (r *reader) expr() (res ir.Node) {
21942194
}
21952195

21962196
n := typecheck.Expr(ir.NewSelectorExpr(pos, ir.OXDOT, recv, wrapperFn.Sel)).(*ir.SelectorExpr)
2197-
assert(n.Selection == wrapperFn.Selection)
2197+
2198+
// As a consistency check here, we make sure "n" selected the
2199+
// same method (represented by a types.Field) that wrapperFn
2200+
// selected. However, for anonymous receiver types, there can be
2201+
// multiple such types.Field instances (#58563). So we may need
2202+
// to fallback to making sure Sym and Type (including the
2203+
// receiver parameter's type) match.
2204+
if n.Selection != wrapperFn.Selection {
2205+
assert(n.Selection.Sym == wrapperFn.Selection.Sym)
2206+
assert(types.Identical(n.Selection.Type, wrapperFn.Selection.Type))
2207+
assert(types.Identical(n.Selection.Type.Recv().Type, wrapperFn.Selection.Type.Recv().Type))
2208+
}
21982209

21992210
wrapper := methodValueWrapper{
22002211
rcvr: n.X.Type(),

test/fixedbugs/issue58563.dir/a.go

Lines changed: 13 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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)