]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: don't emit conversion error in non-numeric increment/decrement
authorCaio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Sat, 5 Mar 2016 20:36:56 +0000 (17:36 -0300)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 7 Mar 2016 14:34:58 +0000 (14:34 +0000)
In increment and decrement statements, explicit check that the type
of operand is numeric earlier. This avoids a related but less clear
error about converting "1" to be emitted.

So, when compiling

package main

func main() {
var x bool
x++
}

instead of emitting two errors

prog.go:5: cannot convert 1 to type bool
prog.go:5: invalid operation: x++ (non-numeric type bool)

just emits the second error.

Fixes #12525.

Change-Id: I6e81330703765bef0d6eb6c57098c1336af7c799
Reviewed-on: https://go-review.googlesource.com/20245
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue12525.go [new file with mode: 0644]

index 50b5698b7467c0401a97df3b75b05dc328ddf376..ea613220e2e7e5b8622655f282b11a85cbc04cb4 100644 (file)
@@ -521,6 +521,11 @@ OpSwitch:
                                n.Type = nil
                                return
                        }
+                       if n.Implicit && !okforarith[l.Type.Etype] {
+                               Yyerror("invalid operation: %v (non-numeric type %v)", n, l.Type)
+                               n.Type = nil
+                               return
+                       }
                        // TODO(marvin): Fix Node.EType type union.
                        op = Op(n.Etype)
                } else {
@@ -632,12 +637,6 @@ OpSwitch:
 
                if t.Etype != TIDEAL && !Eqtype(l.Type, r.Type) {
                        defaultlit2(&l, &r, true)
-                       if n.Op == OASOP && n.Implicit {
-                               Yyerror("invalid operation: %v (non-numeric type %v)", n, l.Type)
-                               n.Type = nil
-                               return
-                       }
-
                        if Isinter(r.Type) == Isinter(l.Type) || aop == 0 {
                                Yyerror("invalid operation: %v (mismatched types %v and %v)", n, l.Type, r.Type)
                                n.Type = nil
diff --git a/test/fixedbugs/issue12525.go b/test/fixedbugs/issue12525.go
new file mode 100644 (file)
index 0000000..4a54eab
--- /dev/null
@@ -0,0 +1,26 @@
+// errorcheck
+
+// Copyright 2016 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.
+
+// Issue 12525: confusing error trying to increment boolean value
+
+package main
+
+func main() {
+       var i int
+       i++
+
+       var f float64
+       f++
+
+       var c complex128
+       c++
+
+       var b bool
+       b++ // ERROR "invalid operation: b\+\+ \(non-numeric type bool\)"
+
+       var s string
+       s-- // ERROR "invalid operation: s-- \(non-numeric type string\)"
+}