]> Cypherpunks.ru repositories - gostls13.git/blob - test/const1.go
cmd/compile/internal/inline: score call sites exposed by inlines
[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 import "unsafe"
13
14 type I interface{}
15
16 const (
17         // assume all types behave similarly to int8/uint8
18         Int8   int8  = 101
19         Minus1 int8  = -1
20         Uint8  uint8 = 102
21         Const        = 103
22
23         Float32    float32 = 104.5
24         Float64    float64 = 105.5
25         ConstFloat         = 106.5
26         Big        float64 = 1e300
27
28         String = "abc"
29         Bool   = true
30 )
31
32 var (
33         a1 = Int8 * 100              // ERROR "overflow|cannot convert"
34         a2 = Int8 * -1               // OK
35         a3 = Int8 * 1000             // ERROR "overflow|cannot convert"
36         a4 = Int8 * int8(1000)       // ERROR "overflow|cannot convert"
37         a5 = int8(Int8 * 1000)       // ERROR "overflow|cannot convert"
38         a6 = int8(Int8 * int8(1000)) // ERROR "overflow|cannot convert"
39         a7 = Int8 - 2*Int8 - 2*Int8  // ERROR "overflow|cannot convert"
40         a8 = Int8 * Const / 100      // ERROR "overflow|cannot convert"
41         a9 = Int8 * (Const / 100)    // OK
42
43         b1        = Uint8 * Uint8         // ERROR "overflow|cannot convert"
44         b2        = Uint8 * -1            // ERROR "overflow|cannot convert"
45         b3        = Uint8 - Uint8         // OK
46         b4        = Uint8 - Uint8 - Uint8 // ERROR "overflow|cannot convert"
47         b5        = uint8(^0)             // ERROR "overflow|cannot convert"
48         b5a       = int64(^0)             // OK
49         b6        = ^uint8(0)             // OK
50         b6a       = ^int64(0)             // OK
51         b7        = uint8(Minus1)         // ERROR "overflow|cannot convert"
52         b8        = uint8(int8(-1))       // ERROR "overflow|cannot convert"
53         b8a       = uint8(-1)             // ERROR "overflow|cannot convert"
54         b9   byte = (1 << 10) >> 8        // OK
55         b10  byte = (1 << 10)             // ERROR "overflow|cannot convert"
56         b11  byte = (byte(1) << 10) >> 8  // ERROR "overflow|cannot convert"
57         b12  byte = 1000                  // ERROR "overflow|cannot convert"
58         b13  byte = byte(1000)            // ERROR "overflow|cannot convert"
59         b14  byte = byte(100) * byte(100) // ERROR "overflow|cannot convert"
60         b15  byte = byte(100) * 100       // ERROR "overflow|cannot convert"
61         b16  byte = byte(0) * 1000        // ERROR "overflow|cannot convert"
62         b16a byte = 0 * 1000              // OK
63         b17  byte = byte(0) * byte(1000)  // ERROR "overflow|cannot convert"
64         b18  byte = Uint8 / 0             // ERROR "division by zero"
65
66         c1 float64 = Big
67         c2 float64 = Big * Big          // ERROR "overflow|cannot convert"
68         c3 float64 = float64(Big) * Big // ERROR "overflow|cannot convert"
69         c4         = Big * Big          // ERROR "overflow|cannot convert"
70         c5         = Big / 0            // ERROR "division by zero"
71         c6         = 1000 % 1e3         // ERROR "invalid operation|expected integer type"
72 )
73
74 func f(int)
75
76 func main() {
77         f(Int8)             // ERROR "convert|wrong type|cannot"
78         f(Minus1)           // ERROR "convert|wrong type|cannot"
79         f(Uint8)            // ERROR "convert|wrong type|cannot"
80         f(Const)            // OK
81         f(Float32)          // ERROR "convert|wrong type|cannot"
82         f(Float64)          // ERROR "convert|wrong type|cannot"
83         f(ConstFloat)       // ERROR "truncate"
84         f(ConstFloat - 0.5) // OK
85         f(Big)              // ERROR "convert|wrong type|cannot"
86         f(String)           // ERROR "convert|wrong type|cannot|incompatible"
87         f(Bool)             // ERROR "convert|wrong type|cannot|incompatible"
88 }
89
90 const ptr = nil // ERROR "const.*nil|not constant"
91 const _ = string([]byte(nil)) // ERROR "is not a? ?constant"
92 const _ = uintptr(unsafe.Pointer((*int)(nil))) // ERROR "is not a? ?constant"
93 const _ = unsafe.Pointer((*int)(nil)) // ERROR "cannot be nil|invalid constant type|is not a constant|not constant"
94 const _ = (*int)(nil) // ERROR "cannot be nil|invalid constant type|is not a constant|not constant"