blob: 58bddee7e072d3e6b4bb147c14e831b56a889af1 [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// errorcheck
Russ Cox8f194bf2009-03-12 19:04:38 -07002
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 Pikefc0dc042012-02-19 13:19:43 +11007// Verify overflow is detected when using numeric constants.
8// Does not compile.
9
Russ Cox8f194bf2009-03-12 19:04:38 -070010package main
11
Russ Cox89313062013-02-01 23:10:02 -050012import "unsafe"
13
Russ Coxf2b5a072011-01-19 23:09:00 -050014type I interface{}
15
Russ Cox8f194bf2009-03-12 19:04:38 -070016const (
17 // assume all types behave similarly to int8/uint8
Russ Coxf2b5a072011-01-19 23:09:00 -050018 Int8 int8 = 101
19 Minus1 int8 = -1
20 Uint8 uint8 = 102
Russ Cox83feedf2012-02-19 00:12:31 -050021 Const = 103
Russ Cox8f194bf2009-03-12 19:04:38 -070022
Russ Coxf2b5a072011-01-19 23:09:00 -050023 Float32 float32 = 104.5
24 Float64 float64 = 105.5
Russ Cox83feedf2012-02-19 00:12:31 -050025 ConstFloat = 106.5
Russ Coxf2b5a072011-01-19 23:09:00 -050026 Big float64 = 1e300
Russ Cox8f194bf2009-03-12 19:04:38 -070027
Rob Pike4f61fc92010-09-04 10:36:13 +100028 String = "abc"
Russ Coxf2b5a072011-01-19 23:09:00 -050029 Bool = true
Russ Cox8f194bf2009-03-12 19:04:38 -070030)
31
32var (
Russ Coxf2b5a072011-01-19 23:09:00 -050033 a1 = Int8 * 100 // ERROR "overflow"
34 a2 = Int8 * -1 // OK
35 a3 = Int8 * 1000 // ERROR "overflow"
36 a4 = Int8 * int8(1000) // ERROR "overflow"
37 a5 = int8(Int8 * 1000) // ERROR "overflow"
38 a6 = int8(Int8 * int8(1000)) // ERROR "overflow"
39 a7 = Int8 - 2*Int8 - 2*Int8 // ERROR "overflow"
40 a8 = Int8 * Const / 100 // ERROR "overflow"
41 a9 = Int8 * (Const / 100) // OK
Russ Cox8f194bf2009-03-12 19:04:38 -070042
Russ Cox83feedf2012-02-19 00:12:31 -050043 b1 = Uint8 * Uint8 // ERROR "overflow"
44 b2 = Uint8 * -1 // ERROR "overflow"
45 b3 = Uint8 - Uint8 // OK
46 b4 = Uint8 - Uint8 - Uint8 // ERROR "overflow"
47 b5 = uint8(^0) // ERROR "overflow"
Ian Lance Taylorf0886ab2012-02-29 17:39:02 -080048 b5a = int64(^0) // OK
Russ Cox83feedf2012-02-19 00:12:31 -050049 b6 = ^uint8(0) // OK
Ian Lance Taylorf0886ab2012-02-29 17:39:02 -080050 b6a = ^int64(0) // OK
Russ Cox83feedf2012-02-19 00:12:31 -050051 b7 = uint8(Minus1) // ERROR "overflow"
52 b8 = uint8(int8(-1)) // ERROR "overflow"
53 b8a = uint8(-1) // ERROR "overflow"
54 b9 byte = (1 << 10) >> 8 // OK
55 b10 byte = (1 << 10) // ERROR "overflow"
56 b11 byte = (byte(1) << 10) >> 8 // ERROR "overflow"
57 b12 byte = 1000 // ERROR "overflow"
58 b13 byte = byte(1000) // ERROR "overflow"
59 b14 byte = byte(100) * byte(100) // ERROR "overflow"
60 b15 byte = byte(100) * 100 // ERROR "overflow"
61 b16 byte = byte(0) * 1000 // ERROR "overflow"
62 b16a byte = 0 * 1000 // OK
63 b17 byte = byte(0) * byte(1000) // ERROR "overflow"
64 b18 byte = Uint8 / 0 // ERROR "division by zero"
Russ Cox8f194bf2009-03-12 19:04:38 -070065
Russ Cox83feedf2012-02-19 00:12:31 -050066 c1 float64 = Big
67 c2 float64 = Big * Big // ERROR "overflow"
68 c3 float64 = float64(Big) * Big // ERROR "overflow"
69 c4 = Big * Big // ERROR "overflow"
70 c5 = Big / 0 // ERROR "division by zero"
Ian Lance Taylor6ed800c2012-09-28 08:30:30 -070071 c6 = 1000 % 1e3 // ERROR "floating-point % operation|expected integer type"
Russ Cox8f194bf2009-03-12 19:04:38 -070072)
73
Rob Pike4f61fc92010-09-04 10:36:13 +100074func f(int)
Russ Cox8f194bf2009-03-12 19:04:38 -070075
76func main() {
Russ Coxf2b5a072011-01-19 23:09:00 -050077 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"
Russ Cox8f194bf2009-03-12 19:04:38 -070088}
Russ Cox0acb6372010-09-11 15:47:56 -040089
Russ Coxf2b5a072011-01-19 23:09:00 -050090const ptr = nil // ERROR "const.*nil"
Ian Lance Taylor161a45a2013-12-12 17:18:12 -080091const _ = string([]byte(nil)) // ERROR "is not a? ?constant"
92const _ = uintptr(unsafe.Pointer((*int)(nil))) // ERROR "is not a? ?constant"
93const _ = unsafe.Pointer((*int)(nil)) // ERROR "cannot be nil|invalid constant type"
94const _ = (*int)(nil) // ERROR "cannot be nil|invalid constant type"