Source file src/internal/types/testdata/check/issues0.go
1 // -lang=go1.17 2 3 // Copyright 2014 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 package p // don't permit non-interface elements in interfaces 8 9 import ( 10 "fmt" 11 syn "regexp/syntax" 12 t1 "text/template" 13 t2 "html/template" 14 ) 15 16 func issue7035() { 17 type T struct{ X int } 18 _ = func() { 19 fmt.Println() // must refer to imported fmt rather than the fmt below 20 } 21 fmt := new(T) 22 _ = fmt.X 23 } 24 25 func issue8066() { 26 const ( 27 _ = float32(340282356779733661637539395458142568447) 28 _ = float32(340282356779733661637539395458142568448 /* ERROR "cannot convert" */ ) 29 ) 30 } 31 32 // Check that a missing identifier doesn't lead to a spurious error cascade. 33 func issue8799a() { 34 x, ok := missing /* ERROR "undefined" */ () 35 _ = !ok 36 _ = x 37 } 38 39 func issue8799b(x int, ok bool) { 40 x, ok = missing /* ERROR "undefined" */ () 41 _ = !ok 42 _ = x 43 } 44 45 func issue9182() { 46 type Point C /* ERROR "undefined" */ .Point 47 // no error for composite literal based on unknown type 48 _ = Point{x: 1, y: 2} 49 } 50 51 func f0() (a []int) { return } 52 func f1() (a []int, b int) { return } 53 func f2() (a, b []int) { return } 54 55 func append_([]int, ...int) {} 56 57 func issue9473(a []int, b ...int) { 58 // variadic builtin function 59 _ = append(f0()) 60 _ = append(f0(), f0()...) 61 _ = append(f1()) 62 _ = append(f2 /* ERRORx `cannot use .* in argument` */ ()) 63 _ = append(f2()... /* ERROR "cannot use ..." */ ) 64 _ = append(f0(), f1 /* ERROR "multiple-value f1" */ ()) 65 _ = append(f0(), f2 /* ERROR "multiple-value f2" */ ()) 66 _ = append(f0(), f1 /* ERROR "multiple-value f1" */ ()...) 67 _ = append(f0(), f2 /* ERROR "multiple-value f2" */ ()...) 68 69 // variadic user-defined function 70 append_(f0()) 71 append_(f0(), f0()...) 72 append_(f1()) 73 append_(f2 /* ERRORx `cannot use .* in argument` */ ()) 74 append_(f2()... /* ERROR "cannot use ..." */ ) 75 append_(f0(), f1 /* ERROR "multiple-value f1" */ ()) 76 append_(f0(), f2 /* ERROR "multiple-value f2" */ ()) 77 append_(f0(), f1 /* ERROR "multiple-value f1" */ ()...) 78 append_(f0(), f2 /* ERROR "multiple-value f2" */ ()...) 79 } 80 81 // Check that embedding a non-interface type in an interface results in a good error message. 82 func issue10979() { 83 type _ interface { 84 int /* ERROR "non-interface type int" */ 85 } 86 type T struct{} 87 type _ interface { 88 T /* ERROR "non-interface type T" */ 89 } 90 type _ interface { 91 nosuchtype /* ERROR "undefined: nosuchtype" */ 92 } 93 type _ interface { 94 fmt.Nosuchtype /* ERROR "undefined: fmt.Nosuchtype" */ 95 } 96 type _ interface { 97 nosuchpkg /* ERROR "undefined: nosuchpkg" */ .Nosuchtype 98 } 99 type I interface { 100 I.m /* ERROR "I.m is not a type" */ 101 m() 102 } 103 } 104 105 // issue11347 106 // These should not crash. 107 var a1, b1, c1 /* ERROR "cycle" */ b1 /* ERROR "b1 is not a type" */ = 0 > 0<<""[""[c1]]>c1 108 var a2, b2 /* ERROR "cycle" */ = 0 /* ERROR "assignment mismatch" */ /* ERROR "assignment mismatch" */ > 0<<""[b2] 109 var a3, b3 /* ERROR "cycle" */ = int /* ERROR "assignment mismatch" */ /* ERROR "assignment mismatch" */ (1<<""[b3]) 110 111 // issue10260 112 // Check that error messages explain reason for interface assignment failures. 113 type ( 114 I0 interface{} 115 I1 interface{ foo() } 116 I2 interface{ foo(x int) } 117 T0 struct{} 118 T1 struct{} 119 T2 struct{} 120 ) 121 122 func (*T1) foo() {} 123 func (*T2) foo(x int) {} 124 125 func issue10260() { 126 var ( 127 i0 I0 128 i1 I1 129 i2 I2 130 t0 *T0 131 t1 *T1 132 t2 *T2 133 ) 134 135 var x I1 136 x = T1 /* ERRORx `cannot use T1{} .* as I1 value in assignment: T1 does not implement I1 \(method foo has pointer receiver\)` */ {} 137 _ = x /* ERROR "impossible type assertion: x.(T1)\n\tT1 does not implement I1 (method foo has pointer receiver)" */ .(T1) 138 139 T1{}.foo /* ERROR "cannot call pointer method foo on T1" */ () 140 x.Foo /* ERROR "x.Foo undefined (type I1 has no field or method Foo, but does have method foo)" */ () 141 142 _ = i2 /* ERROR "impossible type assertion: i2.(*T1)\n\t*T1 does not implement I2 (wrong type for method foo)\n\t\thave foo()\n\t\twant foo(int)" */ .(*T1) 143 144 i1 = i0 /* ERRORx `cannot use i0 .* as I1 value in assignment: I0 does not implement I1 \(missing method foo\)` */ 145 i1 = t0 /* ERRORx `.* t0 .* as I1 .*: \*T0 does not implement I1 \(missing method foo\)` */ 146 i1 = i2 /* ERRORx `.* i2 .* as I1 .*: I2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(int\)\n\t\twant foo\(\)` */ 147 i1 = t2 /* ERRORx `.* t2 .* as I1 .*: \*T2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(int\)\n\t\twant foo\(\)` */ 148 i2 = i1 /* ERRORx `.* i1 .* as I2 .*: I1 does not implement I2 \(wrong type for method foo\)\n\t\thave foo\(\)\n\t\twant foo\(int\)` */ 149 i2 = t1 /* ERRORx `.* t1 .* as I2 .*: \*T1 does not implement I2 \(wrong type for method foo\)\n\t\thave foo\(\)\n\t\twant foo\(int\)` */ 150 151 _ = func() I1 { return i0 /* ERRORx `cannot use i0 .* as I1 value in return statement: I0 does not implement I1 \(missing method foo\)` */ } 152 _ = func() I1 { return t0 /* ERRORx `.* t0 .* as I1 .*: \*T0 does not implement I1 \(missing method foo\)` */ } 153 _ = func() I1 { return i2 /* ERRORx `.* i2 .* as I1 .*: I2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(int\)\n\t\twant foo\(\)` */ } 154 _ = func() I1 { return t2 /* ERRORx `.* t2 .* as I1 .*: \*T2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(int\)\n\t\twant foo\(\)` */ } 155 _ = func() I2 { return i1 /* ERRORx `.* i1 .* as I2 .*: I1 does not implement I2 \(wrong type for method foo\)\n\t\thave foo\(\)\n\t\twant foo\(int\)` */ } 156 _ = func() I2 { return t1 /* ERRORx `.* t1 .* as I2 .*: \*T1 does not implement I2 \(wrong type for method foo\)\n\t\thave foo\(\)\n\t\twant foo\(int\)` */ } 157 158 // a few more - less exhaustive now 159 160 f := func(I1, I2){} 161 f(i0 /* ERROR "missing method foo" */ , i1 /* ERROR "wrong type for method foo" */ ) 162 163 _ = [...]I1{i0 /* ERRORx `cannot use i0 .* as I1 value in array or slice literal: I0 does not implement I1 \(missing method foo\)` */ } 164 _ = [...]I1{i2 /* ERRORx `cannot use i2 .* as I1 value in array or slice literal: I2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(int\)\n\t\twant foo\(\)` */ } 165 _ = []I1{i0 /* ERROR "missing method foo" */ } 166 _ = []I1{i2 /* ERROR "wrong type for method foo" */ } 167 _ = map[int]I1{0: i0 /* ERROR "missing method foo" */ } 168 _ = map[int]I1{0: i2 /* ERROR "wrong type for method foo" */ } 169 170 make(chan I1) <- i0 /* ERROR "missing method foo" */ 171 make(chan I1) <- i2 /* ERROR "wrong type for method foo" */ 172 } 173 174 // Check that constants representable as integers are in integer form 175 // before being used in operations that are only defined on integers. 176 func issue14229() { 177 // from the issue 178 const _ = int64(-1<<63) % 1e6 179 180 // related 181 const ( 182 a int = 3 183 b = 4.0 184 _ = a / b 185 _ = a % b 186 _ = b / a 187 _ = b % a 188 ) 189 } 190 191 // Check that in a n:1 variable declaration with type and initialization 192 // expression the type is distributed to all variables of the lhs before 193 // the initialization expression assignment is checked. 194 func issue15755() { 195 // from issue 196 var i interface{} 197 type b bool 198 var x, y b = i.(b) 199 _ = x == y 200 201 // related: we should see an error since the result of f1 is ([]int, int) 202 var u, v []int = f1 /* ERROR "cannot use f1" */ () 203 _ = u 204 _ = v 205 } 206 207 // Test that we don't get "declared and not used" 208 // errors in the context of invalid/C objects. 209 func issue20358() { 210 var F C /* ERROR "undefined" */ .F 211 var A C /* ERROR "undefined" */ .A 212 var S C /* ERROR "undefined" */ .S 213 type T C /* ERROR "undefined" */ .T 214 type P C /* ERROR "undefined" */ .P 215 216 // these variables must be "used" even though 217 // the LHS expressions/types below in which 218 // context they are used are unknown/invalid 219 var f, a, s1, s2, s3, t, p int 220 221 _ = F(f) 222 _ = A[a] 223 _ = S[s1:s2:s3] 224 _ = T{t} 225 _ = P{f: p} 226 } 227 228 // Test that we don't declare lhs variables in short variable 229 // declarations before we type-check function literals on the 230 // rhs. 231 func issue24026() { 232 f := func() int { f(0) /* must refer to outer f */; return 0 } 233 _ = f 234 235 _ = func() { 236 f := func() { _ = f() /* must refer to outer f */ } 237 _ = f 238 } 239 240 // b and c must not be visible inside function literal 241 a := 0 242 a, b, c := func() (int, int, int) { 243 return a, b /* ERROR "undefined" */ , c /* ERROR "undefined" */ 244 }() 245 _, _ = b, c 246 } 247 248 func f(int) {} // for issue24026 249 250 // Test that we don't report a "missing return statement" error 251 // (due to incorrect context when type-checking interfaces). 252 func issue24140(x interface{}) int { 253 switch x.(type) { 254 case interface{}: 255 return 0 256 default: 257 panic(0) 258 } 259 } 260 261 // Test that we don't crash when the 'if' condition is missing. 262 func issue25438() { 263 if { /* ERROR "missing condition" */ } 264 if x := 0; /* ERROR "missing condition" */ { _ = x } 265 if 266 { /* ERROR "missing condition" */ } 267 } 268 269 // Test that we can embed alias type names in interfaces. 270 type issue25301 interface { 271 E 272 } 273 274 type E = interface { 275 m() 276 } 277 278 // Test case from issue. 279 // cmd/compile reports a cycle as well. 280 type issue25301b /* ERROR "invalid recursive type" */ = interface { 281 m() interface{ issue25301b } 282 } 283 284 type issue25301c interface { 285 notE // ERRORx "non-interface type (struct{}|notE)" 286 } 287 288 type notE = struct{} 289 290 // Test that method declarations don't introduce artificial cycles 291 // (issue #26124). 292 const CC TT = 1 293 type TT int 294 func (TT) MM() [CC]TT 295 296 // Reduced test case from issue #26124. 297 const preloadLimit LNumber = 128 298 type LNumber float64 299 func (LNumber) assertFunction() *LFunction 300 type LFunction struct { 301 GFunction LGFunction 302 } 303 type LGFunction func(*LState) 304 type LState struct { 305 reg *registry 306 } 307 type registry struct { 308 alloc *allocator 309 } 310 type allocator struct { 311 _ [int(preloadLimit)]int 312 } 313 314 // Test that we don't crash when type-checking composite literals 315 // containing errors in the type. 316 var issue27346 = [][n /* ERROR "undefined" */ ]int{ 317 0: {}, 318 } 319 320 var issue22467 = map[int][... /* ERROR "invalid use of [...] array" */ ]int{0: {}} 321 322 // Test that invalid use of ... in parameter lists is recognized 323 // (issue #28281). 324 func issue28281a(int, int, ...int) 325 func issue28281b(a, b int, c ...int) 326 func issue28281c(a, b, c ... /* ERROR "can only use ... with final parameter" */ int) 327 func issue28281d(... /* ERROR "can only use ... with final parameter" */ int, int) 328 func issue28281e(a, b, c ... /* ERROR "can only use ... with final parameter" */ int, d int) 329 func issue28281f(... /* ERROR "can only use ... with final parameter" */ int, ... /* ERROR "can only use ... with final parameter" */ int, int) 330 func (... /* ERROR "can only use ... with final parameter" */ TT) f() 331 func issue28281g() (... /* ERROR "can only use ... with final parameter" */ TT) 332 333 // Issue #26234: Make various field/method lookup errors easier to read by matching cmd/compile's output 334 func issue26234a(f *syn.Prog) { 335 // The error message below should refer to the actual package name (syntax) 336 // not the local package name (syn). 337 f.foo /* ERROR "f.foo undefined (type *syntax.Prog has no field or method foo)" */ 338 } 339 340 type T struct { 341 x int 342 E1 343 E2 344 } 345 346 type E1 struct{ f int } 347 type E2 struct{ f int } 348 349 func issue26234b(x T) { 350 _ = x.f /* ERROR "ambiguous selector x.f" */ 351 } 352 353 func issue26234c() { 354 T.x /* ERROR "T.x undefined (type T has no method x)" */ () 355 } 356 357 func issue35895() { 358 // T is defined in this package, don't qualify its name with the package name. 359 var _ T = 0 // ERROR "cannot use 0 (untyped int constant) as T" 360 361 // There is only one package with name syntax imported, only use the (global) package name in error messages. 362 var _ *syn.Prog = 0 // ERROR "cannot use 0 (untyped int constant) as *syntax.Prog" 363 364 // Because both t1 and t2 have the same global package name (template), 365 // qualify packages with full path name in this case. 366 var _ t1.Template = t2 /* ERRORx `cannot use .* \(value of type .html/template.\.Template\) as .text/template.\.Template` */ .Template{} 367 } 368 369 func issue42989(s uint) { 370 var m map[int]string 371 delete(m, 1<<s) 372 delete(m, 1.<<s) 373 } 374