From: Caio Marcelo de Oliveira Filho Date: Sat, 5 Mar 2016 18:57:17 +0000 (-0300) Subject: go/types: don't emit conversion error in non-numeric increment/decrement X-Git-Tag: go1.7beta1~1506 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=133c26b158b7764dd128f02e2d64de798c198582;p=gostls13.git go/types: don't emit conversion error in non-numeric increment/decrement 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 Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot --- diff --git a/src/go/types/stmt.go b/src/go/types/stmt.go index e0129cf0e0..c6691851fb 100644 --- a/src/go/types/stmt.go +++ b/src/go/types/stmt.go @@ -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 { diff --git a/src/go/types/testdata/stmt0.src b/src/go/types/testdata/stmt0.src index b7966ed93d..fec16e1dd7 100644 --- a/src/go/types/testdata/stmt0.src +++ b/src/go/types/testdata/stmt0.src @@ -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