PageRenderTime 56ms CodeModel.GetById 2ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 0ms

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