aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonard Wang <wangdeyu0907@gmail.com>2021-09-20 00:23:40 +0800
committerRobert Griesemer <gri@golang.org>2021-09-27 03:07:49 +0000
commitff8a7e513bda9e89dfbde8ecefaa4514c11ec596 (patch)
tree00463ced040fb69fcc8b6c68e63e0ec6413ea694
parentaeea5bacbf79fb945edbeac6cd7630dd70c4d9ce (diff)
downloadgo-ff8a7e513bda9e89dfbde8ecefaa4514c11ec596.tar.xz
cmd/compile: print expression for invalid operation errors
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>
-rw-r--r--src/cmd/compile/internal/types2/expr.go2
-rw-r--r--src/cmd/compile/internal/types2/testdata/fixedbugs/issue48472.go211
-rw-r--r--src/go/types/expr.go2
-rw-r--r--src/go/types/testdata/fixedbugs/issue48472.go211
4 files changed, 24 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/types2/expr.go b/src/cmd/compile/internal/types2/expr.go
index 12b7b6cd9f..90c80f9de0 100644
--- a/src/cmd/compile/internal/types2/expr.go
+++ b/src/cmd/compile/internal/types2/expr.go
@@ -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
index 0000000000..5fefcaf22b
--- /dev/null
+++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48472.go2
@@ -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
+}
diff --git a/src/go/types/expr.go b/src/go/types/expr.go
index 007205a9fb..2fc5aa85d2 100644
--- a/src/go/types/expr.go
+++ b/src/go/types/expr.go
@@ -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
index 0000000000..5fefcaf22b
--- /dev/null
+++ b/src/go/types/testdata/fixedbugs/issue48472.go2
@@ -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
+}