Skip to content

Commit

Permalink
cmd/compile: print expression for invalid operation errors
Browse files Browse the repository at this point in the history
For #48472

Change-Id: I5072ebcf53e03fb5515c51a2ad01f02d72b30719
Reviewed-on: https://go-review.googlesource.com/c/go/+/350929
Reviewed-by: Robert Griesemer <[email protected]>
Trust: David Chase <[email protected]>
  • Loading branch information
WangLeonard authored and griesemer committed Sep 27, 2021
1 parent aeea5ba commit ff8a7e5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types2/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/cmd/compile/internal/types2/testdata/fixedbugs/issue48472.go2
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion src/go/types/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/go/types/testdata/fixedbugs/issue48472.go2
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ff8a7e5

Please sign in to comment.