From b091189762980836527c4aa50e3693632aea5144 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 20 Oct 2021 09:55:26 +0700 Subject: [PATCH] cmd/compile/internal/types2: print assignment operation for invalid operation errors When invoking check.binary for assignment operation, the expression will be nil, thus for printing the assignment operation error message, we need to reconstruct the statement from lhs, op, rhs. Fixes #48472 Change-Id: Ie38c3dd8069b47e508968d6e43cedcf7536559ef Reviewed-on: https://go-review.googlesource.com/c/go/+/357229 Trust: Cuong Manh Le Run-TryBot: Cuong Manh Le TryBot-Result: Go Bot Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/types2/expr.go | 6 +++++- .../internal/types2/testdata/fixedbugs/issue48472.go2 | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/types2/expr.go b/src/cmd/compile/internal/types2/expr.go index 2d22c027eb..1001554739 100644 --- a/src/cmd/compile/internal/types2/expr.go +++ b/src/cmd/compile/internal/types2/expr.go @@ -1025,7 +1025,11 @@ func (check *Checker) binary(x *operand, e syntax.Expr, lhs, rhs syntax.Expr, op // only report an error if we have valid types // (otherwise we had an error reported elsewhere already) if x.typ != Typ[Invalid] && y.typ != Typ[Invalid] { - check.errorf(x, invalidOp+"%s (mismatched types %s and %s)", e, x.typ, y.typ) + if e != nil { + check.errorf(x, invalidOp+"%s (mismatched types %s and %s)", e, x.typ, y.typ) + } else { + check.errorf(x, invalidOp+"%s %s= %s (mismatched types %s and %s)", lhs, op, rhs, x.typ, y.typ) + } } x.mode = invalid return diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48472.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48472.go2 index 5fefcaf22b..2d908f4c8b 100644 --- a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48472.go2 +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48472.go2 @@ -9,3 +9,8 @@ func g() { var i int _ = s /* ERROR invalid operation: s \+ i \(mismatched types string and int\) */ + i } + +func f(i int) int { + i /* ERROR invalid operation: i \+= "1" \(mismatched types int and untyped string\) */ += "1" + return i +} -- 2.50.0