Fixes #50965.
Change-Id: I61a74bdb46cf5e72ab94dbe8bd114704282b6211
Reviewed-on: https://go-review.googlesource.com/c/go/+/382354
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
cause = check.sprintf("operator %s not defined on %s", op, check.kindString(errOp.typ)) // catch-all
}
}
- // For switches, report errors on the first (case) operand.
- // TODO(gri) adjust error message in that case
if switchCase {
- errOp = x
- }
- if check.conf.CompilerErrorMessages {
- check.errorf(errOp, invalidOp+"%s %s %s (%s)", x.expr, op, y.expr, cause)
+ check.errorf(x, "invalid case %s in switch on %s (%s)", x.expr, y.expr, cause) // error position always at 1st operand
} else {
- check.errorf(errOp, invalidOp+"cannot compare %s %s %s (%s)", x.expr, op, y.expr, cause)
+ if check.conf.CompilerErrorMessages {
+ check.errorf(errOp, invalidOp+"%s %s %s (%s)", x.expr, op, y.expr, cause)
+ } else {
+ check.errorf(errOp, invalidOp+"cannot compare %s %s %s (%s)", x.expr, op, y.expr, cause)
+ }
}
x.mode = invalid
}
switch int32(x) {
case 1, 2:
- case x /* ERROR "cannot compare" */ :
+ case x /* ERROR "invalid case x in switch on int32\(x\) \(mismatched types int and int32\)" */ :
}
switch x {
}
switch (func())(nil) {
- case f /* ERROR cannot compare */ :
+ case f /* ERROR invalid case f in switch on .* \(func can only be compared to nil\) */ :
}
switch nil /* ERROR use of untyped nil in switch expression */ {
--- /dev/null
+// Copyright 2022 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 _(x int, c string) {
+ switch x {
+ case c /* ERROR invalid case c in switch on x \(mismatched types string and int\) */ :
+ }
+}
+
+func _(x, c []int) {
+ switch x {
+ case c /* ERROR invalid case c in switch on x \(slice can only be compared to nil\) */ :
+ }
+}
cause = check.sprintf("operator %s not defined on %s", op, check.kindString(errOp.typ)) // catch-all
}
}
- // For switches, report errors on the first (case) operand.
- // TODO(gri) adjust error message in that case
if switchCase {
- errOp = x
- }
- if compilerErrorMessages {
- check.invalidOp(errOp, code, "%s %s %s (%s)", x.expr, op, y.expr, cause)
+ check.errorf(x, code, "invalid case %s in switch on %s (%s)", x.expr, y.expr, cause) // error position always at 1st operand
} else {
- check.invalidOp(errOp, code, "cannot compare %s %s %s (%s)", x.expr, op, y.expr, cause)
+ 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)
+ }
}
x.mode = invalid
}
switch int32(x) {
case 1, 2:
- case x /* ERROR "cannot compare" */ :
+ case x /* ERROR "invalid case x in switch on int32\(x\) \(mismatched types int and int32\)" */ :
}
switch x {
}
switch (func())(nil) {
- case f /* ERROR cannot compare */ :
+ case f /* ERROR invalid case f in switch on .* \(func can only be compared to nil\) */ :
}
switch nil /* ERROR use of untyped nil in switch expression */ {
--- /dev/null
+// Copyright 2022 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 _(x int, c string) {
+ switch x {
+ case c /* ERROR invalid case c in switch on x \(mismatched types string and int\) */ :
+ }
+}
+
+func _(x, c []int) {
+ switch x {
+ case c /* ERROR invalid case c in switch on x \(slice can only be compared to nil\) */ :
+ }
+}