]> Cypherpunks.ru repositories - gostls13.git/blob - test/const1.go
gc: fix error for floating-point constant %
[gostls13.git] / test / const1.go
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
10 package main
11
12 type I interface{}
13
14 const (
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
30 var (
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         b6        = ^uint8(0)             // OK
47         b7        = uint8(Minus1)         // ERROR "overflow"
48         b8        = uint8(int8(-1))       // ERROR "overflow"
49         b8a       = uint8(-1)             // ERROR "overflow"
50         b9   byte = (1 << 10) >> 8        // OK
51         b10  byte = (1 << 10)             // ERROR "overflow"
52         b11  byte = (byte(1) << 10) >> 8  // ERROR "overflow"
53         b12  byte = 1000                  // ERROR "overflow"
54         b13  byte = byte(1000)            // ERROR "overflow"
55         b14  byte = byte(100) * byte(100) // ERROR "overflow"
56         b15  byte = byte(100) * 100       // ERROR "overflow"
57         b16  byte = byte(0) * 1000        // ERROR "overflow"
58         b16a byte = 0 * 1000              // OK
59         b17  byte = byte(0) * byte(1000)  // ERROR "overflow"
60         b18  byte = Uint8 / 0             // ERROR "division by zero"
61
62         c1 float64 = Big
63         c2 float64 = Big * Big          // ERROR "overflow"
64         c3 float64 = float64(Big) * Big // ERROR "overflow"
65         c4         = Big * Big          // ERROR "overflow"
66         c5         = Big / 0            // ERROR "division by zero"
67         c6         = 1000 % 1e3         // ERROR "floating-point % operation"
68 )
69
70 func f(int)
71
72 func main() {
73         f(Int8)             // ERROR "convert|wrong type|cannot"
74         f(Minus1)           // ERROR "convert|wrong type|cannot"
75         f(Uint8)            // ERROR "convert|wrong type|cannot"
76         f(Const)            // OK
77         f(Float32)          // ERROR "convert|wrong type|cannot"
78         f(Float64)          // ERROR "convert|wrong type|cannot"
79         f(ConstFloat)       // ERROR "truncate"
80         f(ConstFloat - 0.5) // OK
81         f(Big)              // ERROR "convert|wrong type|cannot"
82         f(String)           // ERROR "convert|wrong type|cannot|incompatible"
83         f(Bool)             // ERROR "convert|wrong type|cannot|incompatible"
84 }
85
86 const ptr = nil // ERROR "const.*nil"