Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 1 | // errchk $G -e $F.go |
| 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 | package main |
| 8 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 9 | type I interface{} |
| 10 | |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 11 | const ( |
| 12 | // assume all types behave similarly to int8/uint8 |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 13 | Int8 int8 = 101 |
| 14 | Minus1 int8 = -1 |
| 15 | Uint8 uint8 = 102 |
| 16 | Const = 103 |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 17 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 18 | Float32 float32 = 104.5 |
| 19 | Float64 float64 = 105.5 |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 20 | ConstFloat = 106.5 |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 21 | Big float64 = 1e300 |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 22 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 23 | String = "abc" |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 24 | Bool = true |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var ( |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 28 | a1 = Int8 * 100 // ERROR "overflow" |
| 29 | a2 = Int8 * -1 // OK |
| 30 | a3 = Int8 * 1000 // ERROR "overflow" |
| 31 | a4 = Int8 * int8(1000) // ERROR "overflow" |
| 32 | a5 = int8(Int8 * 1000) // ERROR "overflow" |
| 33 | a6 = int8(Int8 * int8(1000)) // ERROR "overflow" |
| 34 | a7 = Int8 - 2*Int8 - 2*Int8 // ERROR "overflow" |
| 35 | a8 = Int8 * Const / 100 // ERROR "overflow" |
| 36 | a9 = Int8 * (Const / 100) // OK |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 37 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 38 | b1 = Uint8 * Uint8 // ERROR "overflow" |
| 39 | b2 = Uint8 * -1 // ERROR "overflow" |
| 40 | b3 = Uint8 - Uint8 // OK |
| 41 | b4 = Uint8 - Uint8 - Uint8 // ERROR "overflow" |
| 42 | b5 = uint8(^0) // ERROR "overflow" |
| 43 | b6 = ^uint8(0) // OK |
| 44 | b7 = uint8(Minus1) // ERROR "overflow" |
| 45 | b8 = uint8(int8(-1)) // ERROR "overflow" |
| 46 | b8a = uint8(-1) // ERROR "overflow" |
| 47 | b9 byte = (1 << 10) >> 8 // OK |
| 48 | b10 byte = (1 << 10) // ERROR "overflow" |
| 49 | b11 byte = (byte(1) << 10) >> 8 // ERROR "overflow" |
| 50 | b12 byte = 1000 // ERROR "overflow" |
| 51 | b13 byte = byte(1000) // ERROR "overflow" |
| 52 | b14 byte = byte(100) * byte(100) // ERROR "overflow" |
| 53 | b15 byte = byte(100) * 100 // ERROR "overflow" |
| 54 | b16 byte = byte(0) * 1000 // ERROR "overflow" |
| 55 | b16a byte = 0 * 1000 // OK |
| 56 | b17 byte = byte(0) * byte(1000) // ERROR "overflow" |
| 57 | b18 byte = Uint8 / 0 // ERROR "division by zero" |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 58 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 59 | c1 float64 = Big |
| 60 | c2 float64 = Big * Big // ERROR "overflow" |
| 61 | c3 float64 = float64(Big) * Big // ERROR "overflow" |
| 62 | c4 = Big * Big // ERROR "overflow" |
| 63 | c5 = Big / 0 // ERROR "division by zero" |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 64 | ) |
| 65 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 66 | func f(int) |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 67 | |
| 68 | func main() { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 69 | f(Int8) // ERROR "convert|wrong type|cannot" |
| 70 | f(Minus1) // ERROR "convert|wrong type|cannot" |
| 71 | f(Uint8) // ERROR "convert|wrong type|cannot" |
| 72 | f(Const) // OK |
| 73 | f(Float32) // ERROR "convert|wrong type|cannot" |
| 74 | f(Float64) // ERROR "convert|wrong type|cannot" |
| 75 | f(ConstFloat) // ERROR "truncate" |
| 76 | f(ConstFloat - 0.5) // OK |
| 77 | f(Big) // ERROR "convert|wrong type|cannot" |
| 78 | f(String) // ERROR "convert|wrong type|cannot|incompatible" |
| 79 | f(Bool) // ERROR "convert|wrong type|cannot|incompatible" |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 80 | } |
Russ Cox | 0acb637 | 2010-09-11 15:47:56 -0400 | [diff] [blame] | 81 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 82 | const ptr = nil // ERROR "const.*nil" |