1
0
mirror of https://github.com/golang/go synced 2024-11-22 23:50:03 -07:00

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>
This commit is contained in:
Leonard Wang 2021-09-20 00:23:40 +08:00 committed by Robert Griesemer
parent aeea5bacbf
commit ff8a7e513b
4 changed files with 24 additions and 2 deletions

View 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

View File

@ -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
}

View 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

View File

@ -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
}