| 1 |
// errorcheck |
| 2 |
|
| 3 |
// Copyright 2016 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 |
|
| 7 |
// Check the compiler's switch handling that happens |
| 8 |
// at typechecking time. |
| 9 |
// This must be separate from other checks, |
| 10 |
// because errors during typechecking |
| 11 |
// prevent other errors from being discovered. |
| 12 |
|
| 13 |
package main |
| 14 |
|
| 15 |
// Verify that type switch statements with impossible cases are detected by the compiler. |
| 16 |
func f0(e error) { |
| 17 |
switch e.(type) { |
| 18 |
case int: // ERROR "impossible type switch case: (int\n\t)?e \(.*type error\) cannot have dynamic type int \(missing method Error\)" |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
// Verify that the compiler rejects multiple default cases. |
| 23 |
func f1(e interface{}) { |
| 24 |
switch e { |
| 25 |
default: |
| 26 |
default: // ERROR "multiple defaults( in switch)?" |
| 27 |
} |
| 28 |
switch e.(type) { |
| 29 |
default: |
| 30 |
default: // ERROR "multiple defaults( in switch)?" |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
type I interface { |
| 35 |
Foo() |
| 36 |
} |
| 37 |
|
| 38 |
type X int |
| 39 |
|
| 40 |
func (*X) Foo() {} |
| 41 |
func f2() { |
| 42 |
var i I |
| 43 |
switch i.(type) { |
| 44 |
case X: // ERROR "impossible type switch case: (X\n\t)?i \(.*type I\) cannot have dynamic type X \(method Foo has pointer receiver\)" |
| 45 |
} |
| 46 |
} |