summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2023-02-21 13:16:22 -0800
committerGopher Robot <gobot@golang.org>2023-03-01 22:03:12 +0000
commit26eeaec89c1e78696bfa8ad000d4c8275e3bdb75 (patch)
treea17536c4b16797cd6b2cf233e4ad4877ad16ba9c
parent9629fa1874f269cdc488081912afb45a7b34da86 (diff)
downloadgo-git-26eeaec89c1e78696bfa8ad000d4c8275e3bdb75.tar.gz
[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 <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> (cherry picked from commit 37a2004b431df6cdd3260cbfe2ddb7673e94b9ef) Reviewed-on: https://go-review.googlesource.com/c/go/+/472620 Auto-Submit: Dmitri Shuralyov <dmitshur@google.com> TryBot-Bypass: Dmitri Shuralyov <dmitshur@google.com>
-rw-r--r--src/cmd/compile/internal/noder/reader.go13
-rw-r--r--test/fixedbugs/issue58563.dir/a.go13
-rw-r--r--test/fixedbugs/issue58563.dir/main.go16
-rw-r--r--test/fixedbugs/issue58563.go7
4 files changed, 48 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/noder/reader.go b/src/cmd/compile/internal/noder/reader.go
index 591d09ba78..bc2151e178 100644
--- a/src/cmd/compile/internal/noder/reader.go
+++ b/src/cmd/compile/internal/noder/reader.go
@@ -2194,7 +2194,18 @@ func (r *reader) expr() (res ir.Node) {
}
n := typecheck.Expr(ir.NewSelectorExpr(pos, ir.OXDOT, recv, wrapperFn.Sel)).(*ir.SelectorExpr)
- assert(n.Selection == wrapperFn.Selection)
+
+ // As a consistency check here, we make sure "n" selected the
+ // same method (represented by a types.Field) that wrapperFn
+ // selected. However, for anonymous receiver types, there can be
+ // multiple such types.Field instances (#58563). So we may need
+ // to fallback to making sure Sym and Type (including the
+ // receiver parameter's type) match.
+ if n.Selection != wrapperFn.Selection {
+ assert(n.Selection.Sym == wrapperFn.Selection.Sym)
+ assert(types.Identical(n.Selection.Type, wrapperFn.Selection.Type))
+ assert(types.Identical(n.Selection.Type.Recv().Type, wrapperFn.Selection.Type.Recv().Type))
+ }
wrapper := methodValueWrapper{
rcvr: n.X.Type(),
diff --git a/test/fixedbugs/issue58563.dir/a.go b/test/fixedbugs/issue58563.dir/a.go
new file mode 100644
index 0000000000..2b716c1b33
--- /dev/null
+++ b/test/fixedbugs/issue58563.dir/a.go
@@ -0,0 +1,13 @@
+// Copyright 2023 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 a
+
+func Start() interface{ Stop() } {
+ return new(Stopper)
+}
+
+type Stopper struct{}
+
+func (s *Stopper) Stop() {}
diff --git a/test/fixedbugs/issue58563.dir/main.go b/test/fixedbugs/issue58563.dir/main.go
new file mode 100644
index 0000000000..18a90fcf05
--- /dev/null
+++ b/test/fixedbugs/issue58563.dir/main.go
@@ -0,0 +1,16 @@
+// Copyright 2023 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 main
+
+import "test/a"
+
+func main() {
+ stop := start()
+ defer stop()
+}
+
+func start() func() {
+ return a.Start().Stop
+}
diff --git a/test/fixedbugs/issue58563.go b/test/fixedbugs/issue58563.go
new file mode 100644
index 0000000000..5c4c5c0a8f
--- /dev/null
+++ b/test/fixedbugs/issue58563.go
@@ -0,0 +1,7 @@
+// compiledir
+
+// Copyright 2023 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 ignored