1
0
mirror of https://github.com/golang/go synced 2024-11-27 04:01:19 -07:00

cmd/compile/internal/types2: avoid unpacking single-value LHS

For ++/-- statements, we know that syntax.AssignStmt.Lhs is a
single expression. Avoid unpacking (and allocating a slice) in
that case. Minor optimization.

Change-Id: I6615fd12277b1cd7d4f8b86e0b9d39f27708c13e
Reviewed-on: https://go-review.googlesource.com/c/go/+/477915
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2023-03-20 14:29:05 -07:00 committed by Gopher Robot
parent cbcef91a10
commit acce3abb7e

View File

@ -446,26 +446,23 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
check.assignment(&val, uch.elem, "send")
case *syntax.AssignStmt:
lhs := unpackExpr(s.Lhs)
if s.Rhs == nil {
// x++ or x--
if len(lhs) != 1 {
check.errorf(s, InvalidSyntaxTree, "%s%s requires one operand", s.Op, s.Op)
return
}
// (no need to call unpackExpr as s.Lhs must be single-valued)
var x operand
check.expr(&x, lhs[0])
check.expr(&x, s.Lhs)
if x.mode == invalid {
return
}
if !allNumeric(x.typ) {
check.errorf(lhs[0], NonNumericIncDec, invalidOp+"%s%s%s (non-numeric type %s)", lhs[0], s.Op, s.Op, x.typ)
check.errorf(s.Lhs, NonNumericIncDec, invalidOp+"%s%s%s (non-numeric type %s)", s.Lhs, s.Op, s.Op, x.typ)
return
}
check.assignVar(lhs[0], &x)
check.assignVar(s.Lhs, &x)
return
}
lhs := unpackExpr(s.Lhs)
rhs := unpackExpr(s.Rhs)
switch s.Op {
case 0: