mirror of
https://github.com/golang/go
synced 2024-09-29 02:14:29 -06:00
go/types, types2: better error messages for expression switches
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>
This commit is contained in:
parent
e052044d6b
commit
63e833154c
@ -878,15 +878,14 @@ Error:
|
||||
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
|
||||
}
|
||||
|
@ -429,7 +429,7 @@ func switches0() {
|
||||
|
||||
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 {
|
||||
|
@ -30,7 +30,7 @@ func _() {
|
||||
}
|
||||
|
||||
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 */ {
|
||||
|
17
src/cmd/compile/internal/types2/testdata/fixedbugs/issue50965.go
vendored
Normal file
17
src/cmd/compile/internal/types2/testdata/fixedbugs/issue50965.go
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
// 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\) */ :
|
||||
}
|
||||
}
|
@ -838,15 +838,14 @@ Error:
|
||||
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
|
||||
}
|
||||
|
2
src/go/types/testdata/check/stmt0.src
vendored
2
src/go/types/testdata/check/stmt0.src
vendored
@ -429,7 +429,7 @@ func switches0() {
|
||||
|
||||
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 {
|
||||
|
@ -30,7 +30,7 @@ func _() {
|
||||
}
|
||||
|
||||
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 */ {
|
||||
|
17
src/go/types/testdata/fixedbugs/issue50965.go
vendored
Normal file
17
src/go/types/testdata/fixedbugs/issue50965.go
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
// 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\) */ :
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user