Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // errorcheck |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 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 | |
Rob Pike | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 7 | // Verify overflow is detected when using numeric constants. |
| 8 | // Does not compile. |
| 9 | |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 10 | package main |
| 11 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 12 | type I interface{} |
| 13 | |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 14 | const ( |
| 15 | // assume all types behave similarly to int8/uint8 |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 16 | Int8 int8 = 101 |
| 17 | Minus1 int8 = -1 |
| 18 | Uint8 uint8 = 102 |
Russ Cox | 83feedf | 2012-02-19 00:12:31 -0500 | [diff] [blame] | 19 | Const = 103 |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 20 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 21 | Float32 float32 = 104.5 |
| 22 | Float64 float64 = 105.5 |
Russ Cox | 83feedf | 2012-02-19 00:12:31 -0500 | [diff] [blame] | 23 | ConstFloat = 106.5 |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 24 | Big float64 = 1e300 |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 25 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 26 | String = "abc" |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 27 | Bool = true |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | var ( |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 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 |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 40 | |
Russ Cox | 83feedf | 2012-02-19 00:12:31 -0500 | [diff] [blame] | 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" |
Ian Lance Taylor | f0886ab | 2012-02-29 17:39:02 -0800 | [diff] [blame^] | 46 | b5a = int64(^0) // OK |
Russ Cox | 83feedf | 2012-02-19 00:12:31 -0500 | [diff] [blame] | 47 | b6 = ^uint8(0) // OK |
Ian Lance Taylor | f0886ab | 2012-02-29 17:39:02 -0800 | [diff] [blame^] | 48 | b6a = ^int64(0) // OK |
Russ Cox | 83feedf | 2012-02-19 00:12:31 -0500 | [diff] [blame] | 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" |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 63 | |
Russ Cox | 83feedf | 2012-02-19 00:12:31 -0500 | [diff] [blame] | 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" |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 70 | ) |
| 71 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 72 | func f(int) |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 73 | |
| 74 | func main() { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 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" |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 86 | } |
Russ Cox | 0acb637 | 2010-09-11 15:47:56 -0400 | [diff] [blame] | 87 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 88 | const ptr = nil // ERROR "const.*nil" |