blob: 1160b62e140fb72511f7f45ff3d37f05639c07de [file] [log] [blame]
Russ Cox57eb06f2012-02-16 23:51:04 -05001// errorcheck
Russ Cox191263202010-01-25 18:23:20 -08002
3// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Rob Pike80a97832012-02-24 11:48:19 +11007// Verify that various erroneous type switches are caught be the compiler.
8// Does not compile.
9
Russ Cox191263202010-01-25 18:23:20 -080010package main
11
12import "io"
13
14func whatis(x interface{}) string {
15 switch x.(type) {
16 case int:
17 return "int"
18 case int: // ERROR "duplicate"
19 return "int8"
20 case io.Reader:
21 return "Reader1"
22 case io.Reader: // ERROR "duplicate"
23 return "Reader2"
Robert Griesemerfd897ff2011-08-19 09:31:50 -070024 case interface {
25 r()
26 w()
27 }:
Russ Cox191263202010-01-25 18:23:20 -080028 return "rw"
Robert Griesemer3fd31712017-02-09 16:00:23 -080029 case interface { // ERROR "duplicate"
Robert Griesemerfd897ff2011-08-19 09:31:50 -070030 w()
31 r()
Robert Griesemer3fd31712017-02-09 16:00:23 -080032 }:
Russ Cox191263202010-01-25 18:23:20 -080033 return "wr"
Robert Griesemerfd897ff2011-08-19 09:31:50 -070034
Russ Cox191263202010-01-25 18:23:20 -080035 }
36 return ""
37}
Robert Griesemerfd897ff2011-08-19 09:31:50 -070038
39func notused(x interface{}) {
40 // The first t is in a different scope than the 2nd t; it cannot
41 // be accessed (=> declared and not used error); but it is legal
42 // to declare it.
43 switch t := 0; t := x.(type) { // ERROR "declared and not used"
44 case int:
45 _ = t // this is using the t of "t := x.(type)"
46 }
47}