github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/test/fixedbugs/bug284.go (about) 1 // errorcheck 2 3 // Copyright 2010 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 // Test cases for revised conversion rules. 8 9 package main 10 11 func main() { 12 type NewInt int 13 i0 := 0 14 var i1 int = 1 15 var i2 NewInt = 1 16 i0 = i0 17 i0 = i1 18 i0 = int(i2) 19 i1 = i0 20 i1 = i1 21 i1 = int(i2) 22 i2 = NewInt(i0) 23 i2 = NewInt(i1) 24 i2 = i2 25 26 type A1 [3]int 27 type A2 [3]NewInt 28 var a0 [3]int 29 var a1 A1 30 var a2 A2 31 a0 = a0 32 a0 = a1 33 a0 = [3]int(a2) // ERROR "cannot|invalid" 34 a1 = a0 35 a1 = a1 36 a1 = A1(a2) // ERROR "cannot|invalid" 37 a2 = A2(a0) // ERROR "cannot|invalid" 38 a2 = A2(a1) // ERROR "cannot|invalid" 39 a2 = a2 40 41 type S1 struct { 42 x int 43 } 44 type S2 struct { 45 x NewInt 46 } 47 var s0 struct { 48 x int 49 } 50 var s1 S1 51 var s2 S2 52 s0 = s0 53 s0 = s1 54 s0 = struct { 55 x int 56 }(s2) // ERROR "cannot|invalid" 57 s1 = s0 58 s1 = s1 59 s1 = S1(s2) // ERROR "cannot|invalid" 60 s2 = S2(s0) // ERROR "cannot|invalid" 61 s2 = S2(s1) // ERROR "cannot|invalid" 62 s2 = s2 63 64 type P1 *int 65 type P2 *NewInt 66 var p0 *int 67 var p1 P1 68 var p2 P2 69 p0 = p0 70 p0 = p1 71 p0 = (*int)(p2) // ERROR "cannot|invalid" 72 p1 = p0 73 p1 = p1 74 p1 = P1(p2) // ERROR "cannot|invalid" 75 p2 = P2(p0) // ERROR "cannot|invalid" 76 p2 = P2(p1) // ERROR "cannot|invalid" 77 p2 = p2 78 79 type Q1 *struct { 80 x int 81 } 82 type Q2 *S1 83 var q0 *struct { 84 x int 85 } 86 var q1 Q1 87 var q2 Q2 88 var ps1 *S1 89 q0 = q0 90 q0 = q1 91 q0 = (*struct { 92 x int 93 })(ps1) // legal because of special conversion exception for pointers 94 q0 = (*struct { 95 x int 96 })(q2) // ERROR "cannot|invalid" 97 q1 = q0 98 q1 = q1 99 q1 = Q1(q2) // ERROR "cannot|invalid" 100 q2 = (*S1)(q0) // legal because of special conversion exception for pointers 101 q2 = Q2(q1) // ERROR "cannot|invalid" 102 q2 = q2 103 104 type F1 func(x NewInt) int 105 type F2 func(x int) NewInt 106 var f0 func(x NewInt) int 107 var f1 F1 108 var f2 F2 109 f0 = f0 110 f0 = f1 111 f0 = func(x NewInt) int(f2) // ERROR "cannot|invalid" 112 f1 = f0 113 f1 = f1 114 f1 = F1(f2) // ERROR "cannot|invalid" 115 f2 = F2(f0) // ERROR "cannot|invalid" 116 f2 = F2(f1) // ERROR "cannot|invalid" 117 f2 = f2 118 119 type X1 interface { 120 f() int 121 } 122 type X2 interface { 123 f() NewInt 124 } 125 var x0 interface { 126 f() int 127 } 128 var x1 X1 129 var x2 X2 130 x0 = x0 131 x0 = x1 132 x0 = interface { 133 f() int 134 }(x2) // ERROR "cannot|need type assertion|incompatible" 135 x1 = x0 136 x1 = x1 137 x1 = X1(x2) // ERROR "cannot|need type assertion|incompatible" 138 x2 = X2(x0) // ERROR "cannot|need type assertion|incompatible" 139 x2 = X2(x1) // ERROR "cannot|need type assertion|incompatible" 140 x2 = x2 141 142 type L1 []int 143 type L2 []NewInt 144 var l0 []int 145 var l1 L1 146 var l2 L2 147 l0 = l0 148 l0 = l1 149 l0 = []int(l2) // ERROR "cannot|invalid" 150 l1 = l0 151 l1 = l1 152 l1 = L1(l2) // ERROR "cannot|invalid" 153 l2 = L2(l0) // ERROR "cannot|invalid" 154 l2 = L2(l1) // ERROR "cannot|invalid" 155 l2 = l2 156 157 type M1 map[string]int 158 type M2 map[string]NewInt 159 var m0 []int 160 var m1 L1 161 var m2 L2 162 m0 = m0 163 m0 = m1 164 m0 = []int(m2) // ERROR "cannot|invalid" 165 m1 = m0 166 m1 = m1 167 m1 = L1(m2) // ERROR "cannot|invalid" 168 m2 = L2(m0) // ERROR "cannot|invalid" 169 m2 = L2(m1) // ERROR "cannot|invalid" 170 m2 = m2 171 172 type C1 chan int 173 type C2 chan NewInt 174 var c0 chan int 175 var c1 C1 176 var c2 C2 177 c0 = c0 178 c0 = c1 179 c0 = chan int(c2) // ERROR "cannot|invalid" 180 c1 = c0 181 c1 = c1 182 c1 = C1(c2) // ERROR "cannot|invalid" 183 c2 = C2(c0) // ERROR "cannot|invalid" 184 c2 = C2(c1) // ERROR "cannot|invalid" 185 c2 = c2 186 187 // internal compiler error (6g and gccgo) 188 type T interface{} 189 var _ T = 17 // assignment compatible 190 _ = T(17) // internal compiler error even though assignment compatible 191 }