]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: improve error message for non-final variadic parameter
authorsmasher164 <aindurti@gmail.com>
Tue, 4 Dec 2018 11:41:39 +0000 (06:41 -0500)
committerRobert Griesemer <gri@golang.org>
Wed, 5 Dec 2018 01:35:59 +0000 (01:35 +0000)
Previously, when a function signature had defined a non-final variadic
parameter, the error message always referred to the type associated with that
parameter. However, if the offending parameter's name was part of an identifier
list with a variadic type, one could misinterpret the message, thinking the
problem had been with one of the other names in the identifer list.

    func bar(a, b ...int) {}
clear ~~~~~~~^       ^~~~~~~~ confusing

This change updates the error message and sets the column position to that of
the offending parameter's name, if it exists.

Fixes #28450.

Change-Id: I076f560925598ed90e218c25d70f9449ffd9b3ea
Reviewed-on: https://go-review.googlesource.com/c/152417
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/gc/noder.go
test/fixedbugs/issue28450.go [new file with mode: 0644]

index 23c9539b0a1ae5fed245f2ccf88f62563379439e..89e9ddb668b0618dcedd592d1c00d0774dd073fc 100644 (file)
@@ -548,7 +548,11 @@ func (p *noder) param(param *syntax.Field, dddOk, final bool) *Node {
                if !dddOk {
                        yyerror("cannot use ... in receiver or result parameter list")
                } else if !final {
-                       yyerror("can only use ... with final parameter in list")
+                       if param.Name == nil {
+                               yyerror("cannot use ... with non-final parameter")
+                       } else {
+                               p.yyerrorpos(param.Name.Pos(), "cannot use ... with non-final parameter %s", param.Name.Value)
+                       }
                }
                typ.Op = OTARRAY
                typ.Right = typ.Left
diff --git a/test/fixedbugs/issue28450.go b/test/fixedbugs/issue28450.go
new file mode 100644 (file)
index 0000000..21e5e0c
--- /dev/null
@@ -0,0 +1,18 @@
+// errorcheck
+
+// Copyright 2018 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
+
+func f(a, b, c, d ...int)       {} // ERROR "non-final parameter a" "non-final parameter b" "non-final parameter c"
+func g(a ...int, b ...int)      {} // ERROR "non-final parameter a"
+func h(...int, ...int, float32) {} // ERROR "non-final parameter"
+
+type a func(...float32, ...interface{}) // ERROR "non-final parameter"
+type b interface {
+       f(...int, ...int)                // ERROR "non-final parameter"
+       g(a ...int, b ...int, c float32) // ERROR "non-final parameter a" "non-final parameter b"
+       valid(...int)
+}