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

So, when checking

package main

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

instead of emitting the error

prog.go:5:2: cannot convert 1 (untyped int constant) to bool

emits

prog.go:5:2: invalid operation: x++ (non-numeric type bool).

Updates #12525.

Change-Id: I00aa6bd0bb23267a2fe10ea3f5a0b20bbf3552bc
Reviewed-on: https://go-review.googlesource.com/20244
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/go/types/stmt.go
src/go/types/testdata/stmt0.src

index e0129cf0e09997b69f8c525a43029224c98311dd..c6691851fb28902080f43e281ae9da66a715dac3 100644 (file)
@@ -346,7 +346,17 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
                        check.invalidAST(s.TokPos, "unknown inc/dec operation %s", s.Tok)
                        return
                }
+
                var x operand
+               check.expr(&x, s.X)
+               if x.mode == invalid {
+                       return
+               }
+               if !isNumeric(x.typ) {
+                       check.invalidOp(s.X.Pos(), "%s%s (non-numeric type %s)", s.X, s.Tok, x.typ)
+                       return
+               }
+
                Y := &ast.BasicLit{ValuePos: s.X.Pos(), Kind: token.INT, Value: "1"} // use x's position
                check.binary(&x, nil, s.X, Y, op)
                if x.mode == invalid {
index b7966ed93d384abbaa86d7bf2b9a1e04badc97f3..fec16e1dd7ab0eceb86e2e137f0cf46acf7260b8 100644 (file)
@@ -164,7 +164,7 @@ func incdecs() {
        const c = 3.14
        c /* ERROR "cannot assign" */ ++
        s := "foo"
-       s /* ERROR "cannot convert" */ --
+       s /* ERROR "invalid operation" */ --
        3.14 /* ERROR "cannot assign" */ ++
        var (
                x int