aboutsummaryrefslogtreecommitdiff
path: root/test/switch7.go
diff options
context:
space:
mode:
authorDaniel Martí <mvdan@mvdan.cc>2017-04-26 12:04:08 +0100
committerMatthew Dempsky <mdempsky@google.com>2017-05-19 18:11:51 +0000
commit495f55d27d16b2b8deee4b7e79186b07336f6765 (patch)
tree906a7c101581e92d6dcb616b429af02b8b90c599 /test/switch7.go
parent4dcba023c62d7f7968abc54fa5d38d2bf11412ba (diff)
downloadgo-495f55d27d16b2b8deee4b7e79186b07336f6765.tar.gz
go-495f55d27d16b2b8deee4b7e79186b07336f6765.zip
cmd/compile: make duplicate expr cases readable
Instead of just printing the value, print the original node to make the error more human-friendly. Also print the value if its string form is different than the original node, to make sure it's obvious what value was duplicated. This means that "case '@', '@':", which used to print: duplicate case 64 in switch Will now print: duplicate case '@' (value 64) in switch Factor this logic out into its own function to reuse it in range cases and any other place where we might want to print a node and its value in the future. Also needed to split the errorcheck files because expression switch case duplicates are now detected earlier, so they stop the compiler before it gets to generating the AST and detecting the type switch case duplicates. Fixes #20112. Change-Id: I9009b50dec0d0e705e5de9c9ccb08f1dce8a5a99 Reviewed-on: https://go-review.googlesource.com/41852 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'test/switch7.go')
-rw-r--r--test/switch7.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/test/switch7.go b/test/switch7.go
new file mode 100644
index 0000000000..75060669b3
--- /dev/null
+++ b/test/switch7.go
@@ -0,0 +1,35 @@
+// errorcheck
+
+// Copyright 2016 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.
+
+// Verify that type switch statements with duplicate cases are detected
+// by the compiler.
+// Does not compile.
+
+package main
+
+import "fmt"
+
+func f4(e interface{}) {
+ switch e.(type) {
+ case int:
+ case int: // ERROR "duplicate case int in type switch"
+ case int64:
+ case error:
+ case error: // ERROR "duplicate case error in type switch"
+ case fmt.Stringer:
+ case fmt.Stringer: // ERROR "duplicate case fmt.Stringer in type switch"
+ case struct {
+ i int "tag1"
+ }:
+ case struct {
+ i int "tag2"
+ }:
+ case struct { // ERROR "duplicate case struct { i int .tag1. } in type switch"
+ i int "tag1"
+ }:
+ }
+}
+