go/types, types2: use "invalid operation: x rel y (cause)" for comparison error messages

Matches compiler behavior and is consistent with what we do with other
binary operations.

While at it, also use parentheses rather than a colon for a couple of
errors caused by not having a core type.

For #55326.

Change-Id: I0a5cec1a31ffda98d363e5528791965a1ccb5842
Reviewed-on: https://go-review.googlesource.com/c/go/+/435618
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2022-09-27 20:07:30 -07:00 committed by Gopher Robot
parent 2c4c2a5106
commit dbe56ff6c7
7 changed files with 21 additions and 29 deletions

View File

@ -193,7 +193,7 @@ func (check *Checker) unary(x *operand, e *syntax.Operation) {
case syntax.Recv:
u := coreType(x.typ)
if u == nil {
check.errorf(x, _InvalidReceive, invalidOp+"cannot receive from %s: no core type", x)
check.errorf(x, _InvalidReceive, invalidOp+"cannot receive from %s (no core type)", x)
x.mode = invalid
return
}
@ -875,11 +875,7 @@ Error:
if switchCase {
check.errorf(x, code, "invalid case %s in switch on %s (%s)", x.expr, y.expr, cause) // error position always at 1st operand
} else {
if check.conf.CompilerErrorMessages {
check.errorf(errOp, code, invalidOp+"%s %s %s (%s)", x.expr, op, y.expr, cause)
} else {
check.errorf(errOp, code, invalidOp+"cannot compare %s %s %s (%s)", x.expr, op, y.expr, cause)
}
check.errorf(errOp, code, invalidOp+"%s %s %s (%s)", x.expr, op, y.expr, cause)
}
x.mode = invalid
}
@ -1372,7 +1368,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
typ = hint
base, _ = deref(coreType(typ)) // *T implies &T{}
if base == nil {
check.errorf(e, _InvalidLit, "invalid composite literal element type %s: no core type", typ)
check.errorf(e, _InvalidLit, "invalid composite literal element type %s (no core type)", typ)
goto Error
}

View File

@ -179,7 +179,7 @@ func (check *Checker) unary(x *operand, e *ast.UnaryExpr) {
case token.ARROW:
u := coreType(x.typ)
if u == nil {
check.invalidOp(x, _InvalidReceive, "cannot receive from %s: no core type", x)
check.invalidOp(x, _InvalidReceive, "cannot receive from %s (no core type)", x)
x.mode = invalid
return
}
@ -852,11 +852,7 @@ Error:
if switchCase {
check.errorf(x, code, "invalid case %s in switch on %s (%s)", x.expr, y.expr, cause) // error position always at 1st operand
} else {
if compilerErrorMessages {
check.invalidOp(errOp, code, "%s %s %s (%s)", x.expr, op, y.expr, cause)
} else {
check.invalidOp(errOp, code, "cannot compare %s %s %s (%s)", x.expr, op, y.expr, cause)
}
check.invalidOp(errOp, code, "%s %s %s (%s)", x.expr, op, y.expr, cause)
}
x.mode = invalid
}
@ -1351,7 +1347,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
typ = hint
base, _ = deref(coreType(typ)) // *T implies &T{}
if base == nil {
check.errorf(e, _InvalidLit, "invalid composite literal element type %s: no core type", typ)
check.errorf(e, _InvalidLit, "invalid composite literal element type %s (no core type)", typ)
goto Error
}

View File

@ -9,7 +9,7 @@ package expr2
func _bool() {
const t = true == true
const f = true == false
_ = t /* ERROR cannot compare */ < f
_ = t /* ERROR operator .* not defined */ < f
_ = 0 == t /* ERROR mismatched types untyped int and untyped bool */
var b bool
var x, y float32
@ -29,7 +29,7 @@ func arrays() {
_ = a == b
_ = a != b
_ = a /* ERROR < not defined */ < b
_ = a == nil /* ERROR cannot compare.*mismatched types */
_ = a == nil /* ERROR mismatched types */
type C [10]int
var c C
@ -53,7 +53,7 @@ func structs() {
_ = s == t
_ = s != t
_ = s /* ERROR < not defined */ < t
_ = s == nil /* ERROR cannot compare.*mismatched types */
_ = s == nil /* ERROR mismatched types */
type S struct {
x int
@ -74,7 +74,7 @@ func structs() {
x int
a [10]map[string]int
}
_ = u /* ERROR cannot compare */ == u
_ = u /* ERROR cannot be compared */ == u
}
func pointers() {

View File

@ -58,10 +58,10 @@ func min[T interface{ ~int }](x, y T) T {
}
func _[T interface{~int | ~float32}](x, y T) bool { return x < y }
func _[T any](x, y T) bool { return x /* ERROR cannot compare */ < y }
func _[T interface{~int | ~float32 | ~bool}](x, y T) bool { return x /* ERROR cannot compare */ < y }
func _[T any](x, y T) bool { return x /* ERROR type parameter T is not comparable */ < y }
func _[T interface{~int | ~float32 | ~bool}](x, y T) bool { return x /* ERROR type parameter T is not comparable */ < y }
func _[T C1[T]](x, y T) bool { return x /* ERROR cannot compare */ < y }
func _[T C1[T]](x, y T) bool { return x /* ERROR type parameter T is not comparable */ < y }
func _[T C2[T]](x, y T) bool { return x < y }
type C1[T any] interface{}

View File

@ -12,7 +12,7 @@ type C4 interface{ chan int | chan<- int }
type C5[T any] interface{ ~chan T | <-chan T }
func _[T any](ch T) {
<-ch // ERROR cannot receive from ch .* no core type
<-ch // ERROR cannot receive from ch .* \(no core type\)
}
func _[T C0](ch T) {
@ -28,7 +28,7 @@ func _[T C2](ch T) {
}
func _[T C3](ch T) {
<-ch // ERROR cannot receive from ch .* no core type
<-ch // ERROR cannot receive from ch .* \(no core type\)
}
func _[T C4](ch T) {

View File

@ -8,9 +8,9 @@ type S1 struct{}
type S2 struct{}
func _[P *S1|*S2]() {
_= []P{{ /* ERROR invalid composite literal element type P: no core type */ }}
_= []P{{ /* ERROR invalid composite literal element type P \(no core type\) */ }}
}
func _[P *S1|S1]() {
_= []P{{ /* ERROR invalid composite literal element type P: no core type */ }}
_= []P{{ /* ERROR invalid composite literal element type P \(no core type\) */ }}
}

View File

@ -21,17 +21,17 @@ func _[T interface{comparable; ~int}](x T) {
}
func _[T interface{comparable; ~[]byte}](x T) {
_ = x /* ERROR cannot compare */ == x
_ = x /* ERROR empty type set */ == x
}
// TODO(gri) The error message here should be better. See issue #51525.
func _[T interface{comparable; ~int; ~string}](x T) {
_ = x /* ERROR cannot compare */ == x
_ = x /* ERROR empty type set */ == x
}
// TODO(gri) The error message here should be better. See issue #51525.
func _[T interface{~int; ~string}](x T) {
_ = x /* ERROR cannot compare */ == x
_ = x /* ERROR empty type set */ == x
}
func _[T interface{comparable; interface{~int}; interface{int|float64}}](x T) {
@ -39,7 +39,7 @@ func _[T interface{comparable; interface{~int}; interface{int|float64}}](x T) {
}
func _[T interface{interface{comparable; ~int}; interface{~float64; comparable; m()}}](x T) {
_ = x /* ERROR cannot compare */ == x
_ = x /* ERROR empty type set */ == x
}
// test case from issue