]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: provide valid signature in errors invol...
authorRobert Griesemer <gri@golang.org>
Sat, 5 Jun 2021 04:38:40 +0000 (21:38 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 8 Jun 2021 01:17:12 +0000 (01:17 +0000)
This is an adjusted port of a similar fix in https://golang.org/cl/324733.

Fixes #46583.

Change-Id: Ica1410e4de561e64e58b753e3da04b32156cbaf6
Reviewed-on: https://go-review.googlesource.com/c/go/+/325369
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue46583.src [new file with mode: 0644]

index f0f769ec7054321bb2eeeac6e1daed2931a227aa..8c717cd1e58365713ca8e0e43816c8c2c80af58d 100644 (file)
@@ -582,13 +582,27 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr) {
                        goto Error
                }
 
-               // the receiver type becomes the type of the first function
-               // argument of the method expression's function type
+               // The receiver type becomes the type of the first function
+               // argument of the method expression's function type.
                var params []*Var
                if sig.params != nil {
                        params = sig.params.vars
                }
-               params = append([]*Var{NewVar(sig.recv.pos, sig.recv.pkg, sig.recv.name, x.typ)}, params...)
+               // Be consistent about named/unnamed parameters. This is not needed
+               // for type-checking, but the newly constructed signature may appear
+               // in an error message and then have mixed named/unnamed parameters.
+               // (An alternative would be to not print parameter names in errors,
+               // but it's useful to see them; this is cheap and method expressions
+               // are rare.)
+               name := ""
+               if len(params) > 0 && params[0].name != "" {
+                       // name needed
+                       name = sig.recv.name
+                       if name == "" {
+                               name = "_"
+                       }
+               }
+               params = append([]*Var{NewVar(sig.recv.pos, sig.recv.pkg, name, x.typ)}, params...)
                x.mode = value
                x.typ = &Signature{
                        tparams:  sig.tparams,
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue46583.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue46583.src
new file mode 100644 (file)
index 0000000..da1f1ff
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright 2021 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 T1 struct{}
+func (t T1) m(int) {}
+var f1 func(T1)
+
+type T2 struct{}
+func (t T2) m(x int) {}
+var f2 func(T2)
+
+type T3 struct{}
+func (T3) m(int) {}
+var f3 func(T3)
+
+type T4 struct{}
+func (T4) m(x int) {}
+var f4 func(T4)
+
+func _() {
+       f1 = T1 /* ERROR func\(T1, int\) */ .m
+       f2 = T2 /* ERROR func\(t T2, x int\) */ .m
+       f3 = T3 /* ERROR func\(T3, int\) */ .m
+       f4 = T4 /* ERROR func\(_ T4, x int\) */ .m
+}