]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: print assignment operation for invalid operation errors
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 20 Oct 2021 02:55:26 +0000 (09:55 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 20 Oct 2021 05:28:45 +0000 (05:28 +0000)
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 <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue48472.go2

index 2d22c027eb8767eb8207653ab3004770fb1cf7dd..1001554739614cc01d6c301c8408baa6efd420bd 100644 (file)
@@ -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
index 5fefcaf22bcfa881ada7008a74bdc744c59564da..2d908f4c8b56c7c6ab799080a5c863bfd5a234d5 100644 (file)
@@ -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
+}