Commit 133c26b1 authored by Caio Marcelo de Oliveira Filho's avatar Caio Marcelo de Oliveira Filho Committed by Matthew Dempsky

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/20244Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent a03bdc3e
......@@ -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 {
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment