1// errorcheck
2
3// Copyright 2009 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// Verify overflow is detected when using numeric constants.
8// Does not compile.
9
10package main
11
12type I interface{}
13
14const (
15	// assume all types behave similarly to int8/uint8
16	Int8   int8  = 101
17	Minus1 int8  = -1
18	Uint8  uint8 = 102
19	Const        = 103
20
21	Float32    float32 = 104.5
22	Float64    float64 = 105.5
23	ConstFloat         = 106.5
24	Big        float64 = 1e300
25
26	String = "abc"
27	Bool   = true
28)
29
30var (
31	a1 = Int8 * 100              // ERROR "overflow"
32	a2 = Int8 * -1               // OK
33	a3 = Int8 * 1000             // ERROR "overflow"
34	a4 = Int8 * int8(1000)       // ERROR "overflow"
35	a5 = int8(Int8 * 1000)       // ERROR "overflow"
36	a6 = int8(Int8 * int8(1000)) // ERROR "overflow"
37	a7 = Int8 - 2*Int8 - 2*Int8  // ERROR "overflow"
38	a8 = Int8 * Const / 100      // ERROR "overflow"
39	a9 = Int8 * (Const / 100)    // OK
40
41	b1        = Uint8 * Uint8         // ERROR "overflow"
42	b2        = Uint8 * -1            // ERROR "overflow"
43	b3        = Uint8 - Uint8         // OK
44	b4        = Uint8 - Uint8 - Uint8 // ERROR "overflow"
45	b5        = uint8(^0)             // ERROR "overflow"
46	b5a       = int64(^0)             // OK
47	b6        = ^uint8(0)             // OK
48	b6a       = ^int64(0)             // OK
49	b7        = uint8(Minus1)         // ERROR "overflow"
50	b8        = uint8(int8(-1))       // ERROR "overflow"
51	b8a       = uint8(-1)             // ERROR "overflow"
52	b9   byte = (1 << 10) >> 8        // OK
53	b10  byte = (1 << 10)             // ERROR "overflow"
54	b11  byte = (byte(1) << 10) >> 8  // ERROR "overflow"
55	b12  byte = 1000                  // ERROR "overflow"
56	b13  byte = byte(1000)            // ERROR "overflow"
57	b14  byte = byte(100) * byte(100) // ERROR "overflow"
58	b15  byte = byte(100) * 100       // ERROR "overflow"
59	b16  byte = byte(0) * 1000        // ERROR "overflow"
60	b16a byte = 0 * 1000              // OK
61	b17  byte = byte(0) * byte(1000)  // ERROR "overflow"
62	b18  byte = Uint8 / 0             // ERROR "division by zero"
63
64	c1 float64 = Big
65	c2 float64 = Big * Big          // ERROR "overflow"
66	c3 float64 = float64(Big) * Big // ERROR "overflow"
67	c4         = Big * Big          // ERROR "overflow"
68	c5         = Big / 0            // ERROR "division by zero"
69	c6         = 1000 % 1e3         // ERROR "floating-point % operation|expected integer type"
70)
71
72func f(int)
73
74func main() {
75	f(Int8)             // ERROR "convert|wrong type|cannot"
76	f(Minus1)           // ERROR "convert|wrong type|cannot"
77	f(Uint8)            // ERROR "convert|wrong type|cannot"
78	f(Const)            // OK
79	f(Float32)          // ERROR "convert|wrong type|cannot"
80	f(Float64)          // ERROR "convert|wrong type|cannot"
81	f(ConstFloat)       // ERROR "truncate"
82	f(ConstFloat - 0.5) // OK
83	f(Big)              // ERROR "convert|wrong type|cannot"
84	f(String)           // ERROR "convert|wrong type|cannot|incompatible"
85	f(Bool)             // ERROR "convert|wrong type|cannot|incompatible"
86}
87
88const ptr = nil // ERROR "const.*nil"
89