Skip to content

Commit dbe56ff

Browse files
griesemergopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]>
1 parent 2c4c2a5 commit dbe56ff

File tree

7 files changed

+21
-29
lines changed

7 files changed

+21
-29
lines changed

src/cmd/compile/internal/types2/expr.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (check *Checker) unary(x *operand, e *syntax.Operation) {
193193
case syntax.Recv:
194194
u := coreType(x.typ)
195195
if u == nil {
196-
check.errorf(x, _InvalidReceive, invalidOp+"cannot receive from %s: no core type", x)
196+
check.errorf(x, _InvalidReceive, invalidOp+"cannot receive from %s (no core type)", x)
197197
x.mode = invalid
198198
return
199199
}
@@ -875,11 +875,7 @@ Error:
875875
if switchCase {
876876
check.errorf(x, code, "invalid case %s in switch on %s (%s)", x.expr, y.expr, cause) // error position always at 1st operand
877877
} else {
878-
if check.conf.CompilerErrorMessages {
879-
check.errorf(errOp, code, invalidOp+"%s %s %s (%s)", x.expr, op, y.expr, cause)
880-
} else {
881-
check.errorf(errOp, code, invalidOp+"cannot compare %s %s %s (%s)", x.expr, op, y.expr, cause)
882-
}
878+
check.errorf(errOp, code, invalidOp+"%s %s %s (%s)", x.expr, op, y.expr, cause)
883879
}
884880
x.mode = invalid
885881
}
@@ -1372,7 +1368,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
13721368
typ = hint
13731369
base, _ = deref(coreType(typ)) // *T implies &T{}
13741370
if base == nil {
1375-
check.errorf(e, _InvalidLit, "invalid composite literal element type %s: no core type", typ)
1371+
check.errorf(e, _InvalidLit, "invalid composite literal element type %s (no core type)", typ)
13761372
goto Error
13771373
}
13781374

src/go/types/expr.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (check *Checker) unary(x *operand, e *ast.UnaryExpr) {
179179
case token.ARROW:
180180
u := coreType(x.typ)
181181
if u == nil {
182-
check.invalidOp(x, _InvalidReceive, "cannot receive from %s: no core type", x)
182+
check.invalidOp(x, _InvalidReceive, "cannot receive from %s (no core type)", x)
183183
x.mode = invalid
184184
return
185185
}
@@ -852,11 +852,7 @@ Error:
852852
if switchCase {
853853
check.errorf(x, code, "invalid case %s in switch on %s (%s)", x.expr, y.expr, cause) // error position always at 1st operand
854854
} else {
855-
if compilerErrorMessages {
856-
check.invalidOp(errOp, code, "%s %s %s (%s)", x.expr, op, y.expr, cause)
857-
} else {
858-
check.invalidOp(errOp, code, "cannot compare %s %s %s (%s)", x.expr, op, y.expr, cause)
859-
}
855+
check.invalidOp(errOp, code, "%s %s %s (%s)", x.expr, op, y.expr, cause)
860856
}
861857
x.mode = invalid
862858
}
@@ -1351,7 +1347,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
13511347
typ = hint
13521348
base, _ = deref(coreType(typ)) // *T implies &T{}
13531349
if base == nil {
1354-
check.errorf(e, _InvalidLit, "invalid composite literal element type %s: no core type", typ)
1350+
check.errorf(e, _InvalidLit, "invalid composite literal element type %s (no core type)", typ)
13551351
goto Error
13561352
}
13571353

src/internal/types/testdata/check/expr2.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package expr2
99
func _bool() {
1010
const t = true == true
1111
const f = true == false
12-
_ = t /* ERROR cannot compare */ < f
12+
_ = t /* ERROR operator .* not defined */ < f
1313
_ = 0 == t /* ERROR mismatched types untyped int and untyped bool */
1414
var b bool
1515
var x, y float32
@@ -29,7 +29,7 @@ func arrays() {
2929
_ = a == b
3030
_ = a != b
3131
_ = a /* ERROR < not defined */ < b
32-
_ = a == nil /* ERROR cannot compare.*mismatched types */
32+
_ = a == nil /* ERROR mismatched types */
3333

3434
type C [10]int
3535
var c C
@@ -53,7 +53,7 @@ func structs() {
5353
_ = s == t
5454
_ = s != t
5555
_ = s /* ERROR < not defined */ < t
56-
_ = s == nil /* ERROR cannot compare.*mismatched types */
56+
_ = s == nil /* ERROR mismatched types */
5757

5858
type S struct {
5959
x int
@@ -74,7 +74,7 @@ func structs() {
7474
x int
7575
a [10]map[string]int
7676
}
77-
_ = u /* ERROR cannot compare */ == u
77+
_ = u /* ERROR cannot be compared */ == u
7878
}
7979

8080
func pointers() {

src/internal/types/testdata/check/typeparams.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ func min[T interface{ ~int }](x, y T) T {
5858
}
5959

6060
func _[T interface{~int | ~float32}](x, y T) bool { return x < y }
61-
func _[T any](x, y T) bool { return x /* ERROR cannot compare */ < y }
62-
func _[T interface{~int | ~float32 | ~bool}](x, y T) bool { return x /* ERROR cannot compare */ < y }
61+
func _[T any](x, y T) bool { return x /* ERROR type parameter T is not comparable */ < y }
62+
func _[T interface{~int | ~float32 | ~bool}](x, y T) bool { return x /* ERROR type parameter T is not comparable */ < y }
6363

64-
func _[T C1[T]](x, y T) bool { return x /* ERROR cannot compare */ < y }
64+
func _[T C1[T]](x, y T) bool { return x /* ERROR type parameter T is not comparable */ < y }
6565
func _[T C2[T]](x, y T) bool { return x < y }
6666

6767
type C1[T any] interface{}

src/internal/types/testdata/fixedbugs/issue43671.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type C4 interface{ chan int | chan<- int }
1212
type C5[T any] interface{ ~chan T | <-chan T }
1313

1414
func _[T any](ch T) {
15-
<-ch // ERROR cannot receive from ch .* no core type
15+
<-ch // ERROR cannot receive from ch .* \(no core type\)
1616
}
1717

1818
func _[T C0](ch T) {
@@ -28,7 +28,7 @@ func _[T C2](ch T) {
2828
}
2929

3030
func _[T C3](ch T) {
31-
<-ch // ERROR cannot receive from ch .* no core type
31+
<-ch // ERROR cannot receive from ch .* \(no core type\)
3232
}
3333

3434
func _[T C4](ch T) {

src/internal/types/testdata/fixedbugs/issue51335.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ type S1 struct{}
88
type S2 struct{}
99

1010
func _[P *S1|*S2]() {
11-
_= []P{{ /* ERROR invalid composite literal element type P: no core type */ }}
11+
_= []P{{ /* ERROR invalid composite literal element type P \(no core type\) */ }}
1212
}
1313

1414
func _[P *S1|S1]() {
15-
_= []P{{ /* ERROR invalid composite literal element type P: no core type */ }}
15+
_= []P{{ /* ERROR invalid composite literal element type P \(no core type\) */ }}
1616
}

src/internal/types/testdata/fixedbugs/issue51472.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ func _[T interface{comparable; ~int}](x T) {
2121
}
2222

2323
func _[T interface{comparable; ~[]byte}](x T) {
24-
_ = x /* ERROR cannot compare */ == x
24+
_ = x /* ERROR empty type set */ == x
2525
}
2626

2727
// TODO(gri) The error message here should be better. See issue #51525.
2828
func _[T interface{comparable; ~int; ~string}](x T) {
29-
_ = x /* ERROR cannot compare */ == x
29+
_ = x /* ERROR empty type set */ == x
3030
}
3131

3232
// TODO(gri) The error message here should be better. See issue #51525.
3333
func _[T interface{~int; ~string}](x T) {
34-
_ = x /* ERROR cannot compare */ == x
34+
_ = x /* ERROR empty type set */ == x
3535
}
3636

3737
func _[T interface{comparable; interface{~int}; interface{int|float64}}](x T) {
3838
_ = x == x
3939
}
4040

4141
func _[T interface{interface{comparable; ~int}; interface{~float64; comparable; m()}}](x T) {
42-
_ = x /* ERROR cannot compare */ == x
42+
_ = x /* ERROR empty type set */ == x
4343
}
4444

4545
// test case from issue

0 commit comments

Comments
 (0)