]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: print expression for invalid operation errors
authorLeonard Wang <wangdeyu0907@gmail.com>
Sun, 19 Sep 2021 16:23:40 +0000 (00:23 +0800)
committerRobert Griesemer <gri@golang.org>
Mon, 27 Sep 2021 03:07:49 +0000 (03:07 +0000)
For #48472

Change-Id: I5072ebcf53e03fb5515c51a2ad01f02d72b30719
Reviewed-on: https://go-review.googlesource.com/c/go/+/350929
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: David Chase <drchase@google.com>

src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue48472.go2 [new file with mode: 0644]
src/go/types/expr.go
src/go/types/testdata/fixedbugs/issue48472.go2 [new file with mode: 0644]

index 12b7b6cd9f557414bca71860ed9cbeedae69559d..90c80f9de0b8726cfa138618379aaa2022ad8d72 100644 (file)
@@ -1019,7 +1019,7 @@ 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+"mismatched types %s and %s", x.typ, y.typ)
+                       check.errorf(x, invalidOp+"%s (mismatched types %s and %s)", e, 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
new file mode 100644 (file)
index 0000000..5fefcaf
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func g() {
+       var s string
+       var i int
+       _ = s /* ERROR invalid operation: s \+ i \(mismatched types string and int\) */ + i
+}
index 007205a9fb4145bf7ed1bf90c79ede3669cbddbf..2fc5aa85d2ec0a40a46b29e0e3153c7f66fe8af7 100644 (file)
@@ -994,7 +994,7 @@ func (check *Checker) binary(x *operand, e ast.Expr, lhs, rhs ast.Expr, op token
                        if e != nil {
                                posn = e
                        }
-                       check.invalidOp(posn, _MismatchedTypes, "mismatched types %s and %s", x.typ, y.typ)
+                       check.invalidOp(posn, _MismatchedTypes, "%s (mismatched types %s and %s)", e, x.typ, y.typ)
                }
                x.mode = invalid
                return
diff --git a/src/go/types/testdata/fixedbugs/issue48472.go2 b/src/go/types/testdata/fixedbugs/issue48472.go2
new file mode 100644 (file)
index 0000000..5fefcaf
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func g() {
+       var s string
+       var i int
+       _ = s /* ERROR invalid operation: s \+ i \(mismatched types string and int\) */ + i
+}