PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/libgo/go/exp/types/testdata/expr3.src

https://bitbucket.org/bluezoo/gcc
Unknown | 349 lines | 301 code | 48 blank | 0 comment | 0 complexity | 1de73e6a60540535764a0201fd5c24d7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0, GPL-3.0, BSD-3-Clause, LGPL-2.0
  1. // Copyright 2012 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. // various expressions
  5. package expr3
  6. func shifts1() {
  7. var (
  8. i0 int
  9. u0 uint
  10. )
  11. var (
  12. v0 = 1<<0
  13. v1 = 1<<i0 /* ERROR "must be unsigned" */
  14. v2 = 1<<u0
  15. v3 = 1<<"foo" /* ERROR "must be unsigned" */
  16. v4 = 1<<- /* ERROR "stupid shift" */ 1
  17. v5 = 1<<1025 /* ERROR "stupid shift" */
  18. v6 = 1 /* ERROR "overflows" */ <<100
  19. v10 uint = 1 << 0
  20. v11 uint = 1 << u0
  21. v12 float32 = 1 /* ERROR "must be integer" */ << u0
  22. )
  23. }
  24. func shifts2() {
  25. // TODO(gri) enable commented out tests below.
  26. var (
  27. s uint = 33
  28. i = 1<<s // 1 has type int
  29. j int32 = 1<<s // 1 has type int32; j == 0
  30. k = uint64(1<<s) // 1 has type uint64; k == 1<<33
  31. m int = 1.0<<s // 1.0 has type int
  32. // n = 1.0<<s != 0 // 1.0 has type int; n == false if ints are 32bits in size
  33. o = 1<<s == 2<<s // 1 and 2 have type int; o == true if ints are 32bits in size
  34. // p = 1<<s == 1 /* ERROR "overflows" */ <<33 // illegal if ints are 32bits in size: 1 has type int, but 1<<33 overflows int
  35. u = 1.0 /* ERROR "must be integer" */ <<s // illegal: 1.0 has type float64, cannot shift
  36. v float32 = 1 /* ERROR "must be integer" */ <<s // illegal: 1 has type float32, cannot shift
  37. w int64 = 1.0<<33 // 1.0<<33 is a constant shift expression
  38. )
  39. }
  40. // TODO(gri) The error messages below depond on adjusting the spec
  41. // to reflect what gc is doing at the moment (the spec
  42. // asks for run-time errors at the moment - see issue 4231).
  43. //
  44. func indexes() {
  45. _ = 1 /* ERROR "cannot index" */ [0]
  46. _ = indexes /* ERROR "cannot index" */ [0]
  47. _ = ( /* ERROR "cannot slice" */ 12 + 3)[1:2]
  48. var a [10]int
  49. _ = a[true /* ERROR "must be integer" */ ]
  50. _ = a["foo" /* ERROR "must be integer" */ ]
  51. _ = a[1.1 /* ERROR "must be integer" */ ]
  52. _ = a[1.0]
  53. _ = a[- /* ERROR "index .* negative" */ 1]
  54. _ = a[- /* ERROR "index .* negative" */ 1 :]
  55. _ = a[: - /* ERROR "index .* negative" */ 1]
  56. var a0 int
  57. a0 = a[0]
  58. var a1 int32
  59. a1 = a /* ERROR "cannot assign" */ [1]
  60. _ = a[9]
  61. _ = a[10 /* ERROR "index .* out of bounds" */ ]
  62. _ = a[1 /* ERROR "stupid index" */ <<100]
  63. _ = a[10:]
  64. _ = a[:10]
  65. _ = a[10:10]
  66. _ = a[11 /* ERROR "index .* out of bounds" */ :]
  67. _ = a[: 11 /* ERROR "index .* out of bounds" */ ]
  68. _ = a[: 1 /* ERROR "stupid index" */ <<100]
  69. pa := &a
  70. _ = pa[9]
  71. _ = pa[10 /* ERROR "index .* out of bounds" */ ]
  72. _ = pa[1 /* ERROR "stupid index" */ <<100]
  73. _ = pa[10:]
  74. _ = pa[:10]
  75. _ = pa[10:10]
  76. _ = pa[11 /* ERROR "index .* out of bounds" */ :]
  77. _ = pa[: 11 /* ERROR "index .* out of bounds" */ ]
  78. _ = pa[: 1 /* ERROR "stupid index" */ <<100]
  79. var b [0]int
  80. _ = b[0 /* ERROR "index .* out of bounds" */ ]
  81. _ = b[:]
  82. _ = b[0:]
  83. _ = b[:0]
  84. _ = b[0:0]
  85. var s []int
  86. _ = s[- /* ERROR "index .* negative" */ 1]
  87. _ = s[- /* ERROR "index .* negative" */ 1 :]
  88. _ = s[: - /* ERROR "index .* negative" */ 1]
  89. _ = s[0]
  90. _ = s[1 : 2]
  91. _ = s[2 /* ERROR "inverted slice range" */ : 1]
  92. _ = s[2 :]
  93. _ = s[: 1 /* ERROR "stupid index" */ <<100]
  94. _ = s[1 /* ERROR "stupid index" */ <<100 :]
  95. _ = s[1 /* ERROR "stupid index" */ <<100 : 1 /* ERROR "stupid index" */ <<100]
  96. var t string
  97. _ = t[- /* ERROR "index .* negative" */ 1]
  98. _ = t[- /* ERROR "index .* negative" */ 1 :]
  99. _ = t[: - /* ERROR "index .* negative" */ 1]
  100. var t0 byte
  101. t0 = t[0]
  102. var t1 rune
  103. t1 = t /* ERROR "cannot assign" */ [2]
  104. _ = ("foo" + "bar")[5]
  105. _ = ("foo" + "bar")[6 /* ERROR "index .* out of bounds" */ ]
  106. const c = "foo"
  107. _ = c[- /* ERROR "index .* negative" */ 1]
  108. _ = c[- /* ERROR "index .* negative" */ 1 :]
  109. _ = c[: - /* ERROR "index .* negative" */ 1]
  110. var c0 byte
  111. c0 = c[0]
  112. var c2 float32
  113. c2 = c /* ERROR "cannot assign" */ [2]
  114. _ = c[3 /* ERROR "index .* out of bounds" */ ]
  115. _ = ""[0 /* ERROR "index .* out of bounds" */ ]
  116. _ = s[1<<30] // no compile-time error here
  117. }
  118. type T struct {
  119. x int
  120. }
  121. func (*T) m() {}
  122. func method_expressions() {
  123. _ = T /* ERROR "no single field or method" */ .a
  124. _ = T /* ERROR "has no method" */ .x
  125. _ = T.m
  126. var f func(*T) = (*T).m
  127. var g func(*T) = ( /* ERROR "cannot assign" */ T).m
  128. }
  129. func struct_literals() {
  130. type T0 struct {
  131. a, b, c int
  132. }
  133. type T1 struct {
  134. T0
  135. a, b int
  136. u float64
  137. s string
  138. }
  139. // keyed elements
  140. _ = T1{}
  141. _ = T1{a: 0, 1 /* ERROR "mixture of .* elements" */ }
  142. _ = T1{aa /* ERROR "unknown field" */ : 0}
  143. _ = T1{1 /* ERROR "invalid field name" */ : 0}
  144. _ = T1{a: 0, s: "foo", u: 0, a /* ERROR "duplicate field" */: 10}
  145. _ = T1{a: "foo" /* ERROR "cannot use" */ }
  146. _ = T1{c /* ERROR "unknown field" */ : 0}
  147. _ = T1{T0: { /* ERROR "missing type" */ }}
  148. _ = T1{T0: T0{}}
  149. _ = T1{T0 /* ERROR "invalid field name" */ .a: 0}
  150. // unkeyed elements
  151. _ = T0{1, 2, 3}
  152. _ = T0{1, b /* ERROR "mixture" */ : 2, 3}
  153. _ = T0{1, 2} /* ERROR "too few values" */
  154. _ = T0{1, 2, 3, 4 /* ERROR "too many values" */ }
  155. _ = T0{1, "foo" /* ERROR "cannot use" */, 3.4 /* ERROR "cannot use" */}
  156. }
  157. func array_literals() {
  158. type A0 [0]int
  159. _ = A0{}
  160. _ = A0{0 /* ERROR "index .* out of bounds" */}
  161. _ = A0{0 /* ERROR "index .* out of bounds" */ : 0}
  162. type A1 [10]int
  163. _ = A1{}
  164. _ = A1{0, 1, 2}
  165. _ = A1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  166. _ = A1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /* ERROR "index .* out of bounds" */ }
  167. _ = A1{- /* ERROR "index .* negative" */ 1: 0}
  168. _ = A1{8: 8, 9}
  169. _ = A1{8: 8, 9, 10 /* ERROR "index .* out of bounds" */ }
  170. _ = A1{0, 1, 2, 0 /* ERROR "duplicate index" */ : 0, 3: 3, 4}
  171. _ = A1{5: 5, 6, 7, 3: 3, 4}
  172. _ = A1{5: 5, 6, 7, 3: 3, 4, 5 /* ERROR "duplicate index" */ }
  173. _ = A1{10 /* ERROR "index .* out of bounds" */ : 10, 10 /* ERROR "index .* out of bounds" */ : 10}
  174. _ = A1{5: 5, 6, 7, 3: 3, 1 /* ERROR "stupid index" */ <<100: 4, 5 /* ERROR "duplicate index" */ }
  175. _ = A1{5: 5, 6, 7, 4: 4, 1 /* ERROR "stupid index" */ <<100: 4}
  176. _ = A1{2.0}
  177. _ = A1{2.1 /* ERROR "cannot use" */ }
  178. _ = A1{"foo" /* ERROR "cannot use" */ }
  179. a0 := [...]int{}
  180. assert(len(a0) == 0)
  181. a1 := [...]int{0, 1, 2}
  182. assert(len(a1) == 3)
  183. var a13 [3]int
  184. var a14 [4]int
  185. a13 = a1
  186. a14 = a1 /* ERROR "cannot assign" */
  187. a2 := [...]int{- /* ERROR "index .* negative" */ 1: 0}
  188. a3 := [...]int{0, 1, 2, 0 /* ERROR "duplicate index" */ : 0, 3: 3, 4}
  189. assert(len(a3) == 5) // somewhat arbitrary
  190. a4 := [...]complex128{0, 1, 2, 1<<10-2: -1i, 1i, 400: 10, 12, 14}
  191. assert(len(a4) == 1024)
  192. // from the spec
  193. type Point struct { x, y float32 }
  194. _ = [...]Point{Point{1.5, -3.5}, Point{0, 0}}
  195. _ = [...]Point{{1.5, -3.5}, {0, 0}}
  196. _ = [][]int{[]int{1, 2, 3}, []int{4, 5}}
  197. _ = [][]int{{1, 2, 3}, {4, 5}}
  198. _ = [...]*Point{&Point{1.5, -3.5}, &Point{0, 0}}
  199. _ = [...]*Point{{1.5, -3.5}, {0, 0}}
  200. }
  201. func slice_literals() {
  202. type S0 []int
  203. _ = S0{}
  204. _ = S0{0, 1, 2}
  205. _ = S0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  206. _ = S0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  207. _ = S0{- /* ERROR "index .* negative" */ 1: 0}
  208. _ = S0{8: 8, 9}
  209. _ = S0{8: 8, 9, 10}
  210. _ = S0{0, 1, 2, 0 /* ERROR "duplicate index" */ : 0, 3: 3, 4}
  211. _ = S0{5: 5, 6, 7, 3: 3, 4}
  212. _ = S0{5: 5, 6, 7, 3: 3, 4, 5 /* ERROR "duplicate index" */ }
  213. _ = S0{10: 10, 10 /* ERROR "duplicate index" */ : 10}
  214. _ = S0{5: 5, 6, 7, 3: 3, 1 /* ERROR "stupid index" */ <<100: 4, 5 /* ERROR "duplicate index" */ }
  215. _ = S0{5: 5, 6, 7, 4: 4, 1 /* ERROR "stupid index" */ <<100: 4}
  216. _ = S0{2.0}
  217. _ = S0{2.1 /* ERROR "cannot use" */ }
  218. _ = S0{"foo" /* ERROR "cannot use" */ }
  219. }
  220. func map_literals() {
  221. type M0 map[string]int
  222. _ = M0{}
  223. _ = M0{1 /* ERROR "missing key" */ }
  224. _ = M0{1 /* ERROR "cannot use .* as string key" */ : 2}
  225. _ = M0{"foo": "bar" /* ERROR "cannot use .* as int value" */ }
  226. _ = M0{"foo": 1, "bar": 2, "foo" /* ERROR "duplicate key" */ : 3 }
  227. }
  228. type I interface {
  229. m()
  230. }
  231. type I2 interface {
  232. m(int)
  233. }
  234. type T1 struct{}
  235. type T2 struct{}
  236. func (T2) m(int) {}
  237. func type_asserts() {
  238. var x int
  239. _ = x /* ERROR "not an interface" */ .(int)
  240. var e interface{}
  241. var ok bool
  242. x, ok = e.(int)
  243. var t I
  244. _ = t /* ERROR "use of .* outside type switch" */ .(type)
  245. _ = t.(T)
  246. _ = t.(T1 /* ERROR "missing method m" */ )
  247. _ = t.(T2 /* ERROR "wrong type for method m" */ )
  248. _ = t.(I2 /* ERROR "wrong type for method m" */ )
  249. }
  250. func f0() {}
  251. func f1(x int) {}
  252. func f2(u float32, s string) {}
  253. func fs(s []byte) {}
  254. func fv(x ...int) {}
  255. func fi(x ... interface{}) {}
  256. func g0() {}
  257. func g1() int { return 0}
  258. func g2() (u float32, s string) { return }
  259. func gs() []byte { return nil }
  260. func _calls() {
  261. var x int
  262. var y float32
  263. var s []int
  264. f0()
  265. _ = f0 /* ERROR "used as value" */ ()
  266. f0(g0 /* ERROR "too many arguments" */ )
  267. f1(0)
  268. f1(x)
  269. f1(10.0)
  270. f1 /* ERROR "too few arguments" */ ()
  271. f1(x, y /* ERROR "too many arguments" */ )
  272. f1(s /* ERROR "cannot assign" */ )
  273. f1(x ... /* ERROR "cannot use ..." */ )
  274. f1(g0 /* ERROR "used as value" */ ())
  275. f1(g1())
  276. // f1(g2()) // TODO(gri) missing position in error message
  277. f2 /* ERROR "too few arguments" */ ()
  278. f2 /* ERROR "too few arguments" */ (3.14)
  279. f2(3.14, "foo")
  280. f2(x /* ERROR "cannot assign" */ , "foo")
  281. f2(g0 /* ERROR "used as value" */ ())
  282. f2 /* ERROR "too few arguments" */ (g1 /* ERROR "cannot assign" */ ())
  283. f2(g2())
  284. fs /* ERROR "too few arguments" */ ()
  285. fs(g0 /* ERROR "used as value" */ ())
  286. fs(g1 /* ERROR "cannot assign" */ ())
  287. // fs(g2()) // TODO(gri) missing position in error message
  288. fs(gs())
  289. fv()
  290. fv(1, 2.0, x)
  291. fv(s /* ERROR "cannot assign" */ )
  292. fv(s...)
  293. fv(1, s /* ERROR "can only use ... with matching parameter" */ ...)
  294. fv(gs /* ERROR "cannot assign" */ ())
  295. fv(gs /* ERROR "cannot assign" */ ()...)
  296. fi()
  297. fi(1, 2.0, x, 3.14, "foo")
  298. fi(g2())
  299. fi(0, g2)
  300. fi(0, g2 /* ERROR "2-valued expression" */ ())
  301. }