github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/types/testdata/check/conversions1.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Test various valid and invalid struct assignments and conversions. 6 // Does not compile. 7 8 package conversions2 9 10 type I interface { 11 m() 12 } 13 14 // conversions between structs 15 16 func _() { 17 type S struct{} 18 type T struct{} 19 var s S 20 var t T 21 var u struct{} 22 s = s 23 s = t // ERROR "cannot use .* in assignment" 24 s = u 25 s = S(s) 26 s = S(t) 27 s = S(u) 28 t = u 29 t = T(u) 30 } 31 32 func _() { 33 type S struct{ x int } 34 type T struct { 35 x int "foo" 36 } 37 var s S 38 var t T 39 var u struct { 40 x int "bar" 41 } 42 s = s 43 s = t // ERROR "cannot use .* in assignment" 44 s = u // ERROR "cannot use .* in assignment" 45 s = S(s) 46 s = S(t) 47 s = S(u) 48 t = u // ERROR "cannot use .* in assignment" 49 t = T(u) 50 } 51 52 func _() { 53 type E struct{ x int } 54 type S struct{ x E } 55 type T struct { 56 x E "foo" 57 } 58 var s S 59 var t T 60 var u struct { 61 x E "bar" 62 } 63 s = s 64 s = t // ERROR "cannot use .* in assignment" 65 s = u // ERROR "cannot use .* in assignment" 66 s = S(s) 67 s = S(t) 68 s = S(u) 69 t = u // ERROR "cannot use .* in assignment" 70 t = T(u) 71 } 72 73 func _() { 74 type S struct { 75 x struct { 76 x int "foo" 77 } 78 } 79 type T struct { 80 x struct { 81 x int "bar" 82 } "foo" 83 } 84 var s S 85 var t T 86 var u struct { 87 x struct { 88 x int "bar" 89 } "bar" 90 } 91 s = s 92 s = t // ERROR "cannot use .* in assignment" 93 s = u // ERROR "cannot use .* in assignment" 94 s = S(s) 95 s = S(t) 96 s = S(u) 97 t = u // ERROR "cannot use .* in assignment" 98 t = T(u) 99 } 100 101 func _() { 102 type E1 struct { 103 x int "foo" 104 } 105 type E2 struct { 106 x int "bar" 107 } 108 type S struct{ x E1 } 109 type T struct { 110 x E2 "foo" 111 } 112 var s S 113 var t T 114 var u struct { 115 x E2 "bar" 116 } 117 s = s 118 s = t // ERROR "cannot use .* in assignment" 119 s = u // ERROR "cannot use .* in assignment" 120 s = S(s) 121 s = S(t /* ERROR "cannot convert" */ ) 122 s = S(u /* ERROR "cannot convert" */ ) 123 t = u // ERROR "cannot use .* in assignment" 124 t = T(u) 125 } 126 127 func _() { 128 type E struct{ x int } 129 type S struct { 130 f func(struct { 131 x int "foo" 132 }) 133 } 134 type T struct { 135 f func(struct { 136 x int "bar" 137 }) 138 } 139 var s S 140 var t T 141 var u struct{ f func(E) } 142 s = s 143 s = t // ERROR "cannot use .* in assignment" 144 s = u // ERROR "cannot use .* in assignment" 145 s = S(s) 146 s = S(t) 147 s = S(u /* ERROR "cannot convert" */ ) 148 t = u // ERROR "cannot use .* in assignment" 149 t = T(u /* ERROR "cannot convert" */ ) 150 } 151 152 // conversions between pointers to structs 153 154 func _() { 155 type S struct{} 156 type T struct{} 157 var s *S 158 var t *T 159 var u *struct{} 160 s = s 161 s = t // ERROR "cannot use .* in assignment" 162 s = u // ERROR "cannot use .* in assignment" 163 s = (*S)(s) 164 s = (*S)(t) 165 s = (*S)(u) 166 t = u // ERROR "cannot use .* in assignment" 167 t = (*T)(u) 168 } 169 170 func _() { 171 type S struct{ x int } 172 type T struct { 173 x int "foo" 174 } 175 var s *S 176 var t *T 177 var u *struct { 178 x int "bar" 179 } 180 s = s 181 s = t // ERROR "cannot use .* in assignment" 182 s = u // ERROR "cannot use .* in assignment" 183 s = (*S)(s) 184 s = (*S)(t) 185 s = (*S)(u) 186 t = u // ERROR "cannot use .* in assignment" 187 t = (*T)(u) 188 } 189 190 func _() { 191 type E struct{ x int } 192 type S struct{ x E } 193 type T struct { 194 x E "foo" 195 } 196 var s *S 197 var t *T 198 var u *struct { 199 x E "bar" 200 } 201 s = s 202 s = t // ERROR "cannot use .* in assignment" 203 s = u // ERROR "cannot use .* in assignment" 204 s = (*S)(s) 205 s = (*S)(t) 206 s = (*S)(u) 207 t = u // ERROR "cannot use .* in assignment" 208 t = (*T)(u) 209 } 210 211 func _() { 212 type S struct { 213 x struct { 214 x int "foo" 215 } 216 } 217 type T struct { 218 x struct { 219 x int "bar" 220 } "foo" 221 } 222 var s *S 223 var t *T 224 var u *struct { 225 x struct { 226 x int "bar" 227 } "bar" 228 } 229 s = s 230 s = t // ERROR "cannot use .* in assignment" 231 s = u // ERROR "cannot use .* in assignment" 232 s = (*S)(s) 233 s = (*S)(t) 234 s = (*S)(u) 235 t = u // ERROR "cannot use .* in assignment" 236 t = (*T)(u) 237 } 238 239 func _() { 240 type E1 struct { 241 x int "foo" 242 } 243 type E2 struct { 244 x int "bar" 245 } 246 type S struct{ x E1 } 247 type T struct { 248 x E2 "foo" 249 } 250 var s *S 251 var t *T 252 var u *struct { 253 x E2 "bar" 254 } 255 s = s 256 s = t // ERROR "cannot use .* in assignment" 257 s = u // ERROR "cannot use .* in assignment" 258 s = (*S)(s) 259 s = (*S)(t /* ERROR "cannot convert" */ ) 260 s = (*S)(u /* ERROR "cannot convert" */ ) 261 t = u // ERROR "cannot use .* in assignment" 262 t = (*T)(u) 263 } 264 265 func _() { 266 type E struct{ x int } 267 type S struct { 268 f func(struct { 269 x int "foo" 270 }) 271 } 272 type T struct { 273 f func(struct { 274 x int "bar" 275 }) 276 } 277 var s *S 278 var t *T 279 var u *struct{ f func(E) } 280 s = s 281 s = t // ERROR "cannot use .* in assignment" 282 s = u // ERROR "cannot use .* in assignment" 283 s = (*S)(s) 284 s = (*S)(t) 285 s = (*S)(u /* ERROR "cannot convert" */ ) 286 t = u // ERROR "cannot use .* in assignment" 287 t = (*T)(u /* ERROR "cannot convert" */ ) 288 } 289 290 func _() { 291 type E struct{ x int } 292 type S struct { 293 f func(*struct { 294 x int "foo" 295 }) 296 } 297 type T struct { 298 f func(*struct { 299 x int "bar" 300 }) 301 } 302 var s *S 303 var t *T 304 var u *struct{ f func(E) } 305 s = s 306 s = t // ERROR "cannot use .* in assignment" 307 s = u // ERROR "cannot use .* in assignment" 308 s = (*S)(s) 309 s = (*S)(t) 310 s = (*S)(u /* ERROR "cannot convert" */ ) 311 t = u // ERROR "cannot use .* in assignment" 312 t = (*T)(u /* ERROR "cannot convert" */ ) 313 }