]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: use "invalid operation: x rel y (cause)" for comparison error messages
authorRobert Griesemer <gri@golang.org>
Wed, 28 Sep 2022 03:07:30 +0000 (20:07 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 28 Sep 2022 10:36:02 +0000 (10:36 +0000)
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>

src/cmd/compile/internal/types2/expr.go
src/go/types/expr.go
src/internal/types/testdata/check/expr2.go
src/internal/types/testdata/check/typeparams.go
src/internal/types/testdata/fixedbugs/issue43671.go
src/internal/types/testdata/fixedbugs/issue51335.go
src/internal/types/testdata/fixedbugs/issue51472.go

index 1f27871c4262a92edead5e125ac82e03e7eac23a..06fb9bb9f92a0f07fd79125d6387248d8cdc3466 100644 (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
                        }
 
index 06a0de7c3529873ba03cf070102d908c71ee23d0..710205abbac3c2310380e8a42fc1f427edd2d4e7 100644 (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
                        }
 
index 1cb65ad2a297fd41c7f238abd869c53b957f690b..1929664128161940cc9de6d67381aa97f62fff2e 100644 (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() {
index b7950bf0be5a934799fea9b5d8d37c0dbc541113..0e440d8e6f9588fd9f836f8a4d818da9acec2021 100644 (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{}
index 3c78f85aa4e79400e9b8f92c0969942e837dd719..6879aecb453710fb4e85831b34ea3e6951ea7bc4 100644 (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) {
index 0b5a1af0825d557428b34136a677e3f0a698c605..35135cd1db16357d16351272d0ee45e513314cca 100644 (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\) */ }}
 }
index ecdc9547fe22361214cdebd9d6c15fde64b8608f..52ae09dad789c49be32d38343fc23faf81ac272b 100644 (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