]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: better error message for invalid method expression
authorRobert Griesemer <gri@golang.org>
Tue, 6 Dec 2022 00:17:56 +0000 (16:17 -0800)
committerGopher Robot <gobot@golang.org>
Tue, 6 Dec 2022 18:32:51 +0000 (18:32 +0000)
Fixes #53358.

Change-Id: I38528da1596b6e1aaedcab89b1513fb8acac596c
Reviewed-on: https://go-review.googlesource.com/c/go/+/455335
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/call.go
src/go/types/call.go
src/internal/types/testdata/check/expr3.go
src/internal/types/testdata/check/methodsets.go
src/internal/types/testdata/fixedbugs/issue53358.go [new file with mode: 0644]

index ffff11ea6e1c350ff0f166c1593585100e7e0f45..50343bf77a6aecde608f31310409aaf949497031 100644 (file)
@@ -573,7 +573,11 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr, def *Named) {
                }
 
                if indirect {
-                       check.errorf(e.Sel, InvalidMethodExpr, "cannot call pointer method %s on %s", sel, x.typ)
+                       if x.mode == typexpr {
+                               check.errorf(e.Sel, InvalidMethodExpr, "invalid method expression %s.%s (needs pointer receiver (*%s).%s)", x.typ, sel, x.typ, sel)
+                       } else {
+                               check.errorf(e.Sel, InvalidMethodExpr, "cannot call pointer method %s on %s", sel, x.typ)
+                       }
                        goto Error
                }
 
index 7a9329613d44ec89804baa0e22681dc0c08b2271..5558244f1beadf8b28a1d7aea418d0b850dbeb2b 100644 (file)
@@ -577,7 +577,11 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr, def *Named) {
                }
 
                if indirect {
-                       check.errorf(e.Sel, InvalidMethodExpr, "cannot call pointer method %s on %s", sel, x.typ)
+                       if x.mode == typexpr {
+                               check.errorf(e.Sel, InvalidMethodExpr, "invalid method expression %s.%s (needs pointer receiver (*%s).%s)", x.typ, sel, x.typ, sel)
+                       } else {
+                               check.errorf(e.Sel, InvalidMethodExpr, "cannot call pointer method %s on %s", sel, x.typ)
+                       }
                        goto Error
                }
 
index 2ca39866fcc2aeb1be625167653f5c9698c23a60..2da59841c60556e6fe3fa8e3c9d88e678b417e84 100644 (file)
@@ -157,10 +157,10 @@ func (*T) m() {}
 func method_expressions() {
        _ = T.a /* ERROR "no field or method" */
        _ = T.x /* ERROR "has no method" */
-       _ = T.m /* ERROR "cannot call pointer method m on T" */
+       _ = T.m /* ERROR "invalid method expression T\.m \(needs pointer receiver \(\*T\)\.m\)" */
        _ = (*T).m
 
-       var f func(*T) = T.m /* ERROR "cannot call pointer method m on T" */
+       var f func(*T) = T.m /* ERROR "invalid method expression T\.m \(needs pointer receiver \(\*T\)\.m\)" */
        var g func(*T) = (*T).m
        _, _ = f, g
 
index b0eb14cf50f94a298563c3f3c3e340b3809ef58c..d145bc09f51620680b0237380454351241011f45 100644 (file)
@@ -29,7 +29,7 @@ type T3 struct {
 func _() {
        var (
                _ func(T0) = T0.v0
-               _ = T0.p0 /* ERROR "cannot call pointer method p0 on T0" */
+               _ = T0.p0 /* ERROR invalid method expression T0\.p0 \(needs pointer receiver \(\*T0\)\.p0\) */
 
                _ func (*T0) = (*T0).v0
                _ func (*T0) = (*T0).p0
@@ -40,7 +40,7 @@ func _() {
                _ func(T2) = T2.p2
 
                _ func(T3) = T3.v0
-               _ func(T3) = T3.p0 /* ERROR "cannot call pointer method p0 on T3" */
+               _ func(T3) = T3.p0 /* ERROR invalid method expression T3\.p0 \(needs pointer receiver \(\*T3\)\.p0\) */
                _ func(T3) = T3.v1
                _ func(T3) = T3.p1
                _ func(T3) = T3.v2
diff --git a/src/internal/types/testdata/fixedbugs/issue53358.go b/src/internal/types/testdata/fixedbugs/issue53358.go
new file mode 100644 (file)
index 0000000..774136f
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2022 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 p
+
+type A struct{}
+
+func (*A) m() int { return 0 }
+
+var _ = A.m /* ERROR invalid method expression A\.m \(needs pointer receiver \(\*A\)\.m\) */ ()
+var _ = (*A).m(nil)
+
+type B struct{ A }
+
+var _ = B.m // ERROR invalid method expression B\.m \(needs pointer receiver \(\*B\)\.m\)
+var _ = (*B).m
+
+var _ = struct{ A }.m // ERROR invalid method expression struct{A}\.m \(needs pointer receiver \(\*struct{A}\)\.m\)