blob: 67f36e4fdc94816f61947e8e0d2959546a778501 [file] [log] [blame]
Russ Cox8f194bf2009-03-12 19:04:38 -07001// 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
7package main
8
Russ Coxf2b5a072011-01-19 23:09:00 -05009type I interface{}
10
Russ Cox8f194bf2009-03-12 19:04:38 -070011const (
12 // assume all types behave similarly to int8/uint8
Russ Coxf2b5a072011-01-19 23:09:00 -050013 Int8 int8 = 101
14 Minus1 int8 = -1
15 Uint8 uint8 = 102
16 Const = 103
Russ Cox8f194bf2009-03-12 19:04:38 -070017
Russ Coxf2b5a072011-01-19 23:09:00 -050018 Float32 float32 = 104.5
19 Float64 float64 = 105.5
Rob Pike4f61fc92010-09-04 10:36:13 +100020 ConstFloat = 106.5
Russ Coxf2b5a072011-01-19 23:09:00 -050021 Big float64 = 1e300
Russ Cox8f194bf2009-03-12 19:04:38 -070022
Rob Pike4f61fc92010-09-04 10:36:13 +100023 String = "abc"
Russ Coxf2b5a072011-01-19 23:09:00 -050024 Bool = true
Russ Cox8f194bf2009-03-12 19:04:38 -070025)
26
27var (
Russ Coxf2b5a072011-01-19 23:09:00 -050028 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 Cox8f194bf2009-03-12 19:04:38 -070037
Russ Coxf2b5a072011-01-19 23:09:00 -050038 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 Cox8f194bf2009-03-12 19:04:38 -070058
Russ Coxf2b5a072011-01-19 23:09:00 -050059 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 Cox8f194bf2009-03-12 19:04:38 -070064)
65
Rob Pike4f61fc92010-09-04 10:36:13 +100066func f(int)
Russ Cox8f194bf2009-03-12 19:04:38 -070067
68func main() {
Russ Coxf2b5a072011-01-19 23:09:00 -050069 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 Cox8f194bf2009-03-12 19:04:38 -070080}
Russ Cox0acb6372010-09-11 15:47:56 -040081
Russ Coxf2b5a072011-01-19 23:09:00 -050082const ptr = nil // ERROR "const.*nil"