From: Robert Griesemer Date: Wed, 28 Sep 2022 03:07:30 +0000 (-0700) Subject: go/types, types2: use "invalid operation: x rel y (cause)" for comparison error messages X-Git-Tag: go1.20rc1~874 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=dbe56ff6c7a0162f318ed2dd7331c9de82e11d6d;p=gostls13.git 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 Reviewed-by: Robert Findley TryBot-Result: Gopher Robot Auto-Submit: Robert Griesemer Run-TryBot: Robert Griesemer --- diff --git a/src/cmd/compile/internal/types2/expr.go b/src/cmd/compile/internal/types2/expr.go index 1f27871c42..06fb9bb9f9 100644 --- a/src/cmd/compile/internal/types2/expr.go +++ b/src/cmd/compile/internal/types2/expr.go @@ -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 } diff --git a/src/go/types/expr.go b/src/go/types/expr.go index 06a0de7c35..710205abba 100644 --- a/src/go/types/expr.go +++ b/src/go/types/expr.go @@ -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 } diff --git a/src/internal/types/testdata/check/expr2.go b/src/internal/types/testdata/check/expr2.go index 1cb65ad2a2..1929664128 100644 --- a/src/internal/types/testdata/check/expr2.go +++ b/src/internal/types/testdata/check/expr2.go @@ -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() { diff --git a/src/internal/types/testdata/check/typeparams.go b/src/internal/types/testdata/check/typeparams.go index b7950bf0be..0e440d8e6f 100644 --- a/src/internal/types/testdata/check/typeparams.go +++ b/src/internal/types/testdata/check/typeparams.go @@ -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{} diff --git a/src/internal/types/testdata/fixedbugs/issue43671.go b/src/internal/types/testdata/fixedbugs/issue43671.go index 3c78f85aa4..6879aecb45 100644 --- a/src/internal/types/testdata/fixedbugs/issue43671.go +++ b/src/internal/types/testdata/fixedbugs/issue43671.go @@ -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) { diff --git a/src/internal/types/testdata/fixedbugs/issue51335.go b/src/internal/types/testdata/fixedbugs/issue51335.go index 0b5a1af082..35135cd1db 100644 --- a/src/internal/types/testdata/fixedbugs/issue51335.go +++ b/src/internal/types/testdata/fixedbugs/issue51335.go @@ -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\) */ }} } diff --git a/src/internal/types/testdata/fixedbugs/issue51472.go b/src/internal/types/testdata/fixedbugs/issue51472.go index ecdc9547fe..52ae09dad7 100644 --- a/src/internal/types/testdata/fixedbugs/issue51472.go +++ b/src/internal/types/testdata/fixedbugs/issue51472.go @@ -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