blob: 0f417a33804e3db4e787b2065e308583d5bf32ba [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// errorcheck
Russ Cox6e3e3802011-11-22 12:30:02 -05002
3// Copyright 2011 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 that illegal conversions involving strings are detected.
8// Does not compile.
9
Russ Cox6e3e3802011-11-22 12:30:02 -050010package main
11
12type Tbyte []byte
13type Trune []rune
14type Tint64 []int64
15type Tstring string
16
17func main() {
18 s := "hello"
19 sb := []byte("hello")
20 sr := []rune("hello")
21 si := []int64{'h', 'e', 'l', 'l', 'o'}
22
23 ts := Tstring(s)
24 tsb := Tbyte(sb)
25 tsr := Trune(sr)
26 tsi := Tint64(si)
27
28 _ = string(s)
29 _ = []byte(s)
30 _ = []rune(s)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080031 _ = []int64(s) // ERROR "cannot convert.*\[\]int64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050032 _ = Tstring(s)
33 _ = Tbyte(s)
34 _ = Trune(s)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080035 _ = Tint64(s) // ERROR "cannot convert.*Tint64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050036
37 _ = string(sb)
38 _ = []byte(sb)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080039 _ = []rune(sb) // ERROR "cannot convert.*\[\]rune|invalid type conversion"
40 _ = []int64(sb) // ERROR "cannot convert.*\[\]int64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050041 _ = Tstring(sb)
42 _ = Tbyte(sb)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080043 _ = Trune(sb) // ERROR "cannot convert.*Trune|invalid type conversion"
44 _ = Tint64(sb) // ERROR "cannot convert.*Tint64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050045
46 _ = string(sr)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080047 _ = []byte(sr) // ERROR "cannot convert.*\[\]byte|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050048 _ = []rune(sr)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080049 _ = []int64(sr) // ERROR "cannot convert.*\[\]int64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050050 _ = Tstring(sr)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080051 _ = Tbyte(sr) // ERROR "cannot convert.*Tbyte|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050052 _ = Trune(sr)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080053 _ = Tint64(sr) // ERROR "cannot convert.*Tint64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050054
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080055 _ = string(si) // ERROR "cannot convert.* string|invalid type conversion"
56 _ = []byte(si) // ERROR "cannot convert.*\[\]byte|invalid type conversion"
57 _ = []rune(si) // ERROR "cannot convert.*\[\]rune|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050058 _ = []int64(si)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080059 _ = Tstring(si) // ERROR "cannot convert.*Tstring|invalid type conversion"
60 _ = Tbyte(si) // ERROR "cannot convert.*Tbyte|invalid type conversion"
61 _ = Trune(si) // ERROR "cannot convert.*Trune|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050062 _ = Tint64(si)
63
64 _ = string(ts)
65 _ = []byte(ts)
66 _ = []rune(ts)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080067 _ = []int64(ts) // ERROR "cannot convert.*\[\]int64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050068 _ = Tstring(ts)
69 _ = Tbyte(ts)
70 _ = Trune(ts)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080071 _ = Tint64(ts) // ERROR "cannot convert.*Tint64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050072
73 _ = string(tsb)
74 _ = []byte(tsb)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080075 _ = []rune(tsb) // ERROR "cannot convert.*\[\]rune|invalid type conversion"
76 _ = []int64(tsb) // ERROR "cannot convert.*\[\]int64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050077 _ = Tstring(tsb)
78 _ = Tbyte(tsb)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080079 _ = Trune(tsb) // ERROR "cannot convert.*Trune|invalid type conversion"
80 _ = Tint64(tsb) // ERROR "cannot convert.*Tint64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050081
82 _ = string(tsr)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080083 _ = []byte(tsr) // ERROR "cannot convert.*\[\]byte|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050084 _ = []rune(tsr)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080085 _ = []int64(tsr) // ERROR "cannot convert.*\[\]int64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050086 _ = Tstring(tsr)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080087 _ = Tbyte(tsr) // ERROR "cannot convert.*Tbyte|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050088 _ = Trune(tsr)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080089 _ = Tint64(tsr) // ERROR "cannot convert.*Tint64|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050090
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080091 _ = string(tsi) // ERROR "cannot convert.* string|invalid type conversion"
92 _ = []byte(tsi) // ERROR "cannot convert.*\[\]byte|invalid type conversion"
93 _ = []rune(tsi) // ERROR "cannot convert.*\[\]rune|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050094 _ = []int64(tsi)
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080095 _ = Tstring(tsi) // ERROR "cannot convert.*Tstring|invalid type conversion"
96 _ = Tbyte(tsi) // ERROR "cannot convert.*Tbyte|invalid type conversion"
97 _ = Trune(tsi) // ERROR "cannot convert.*Trune|invalid type conversion"
Russ Cox6e3e3802011-11-22 12:30:02 -050098 _ = Tint64(tsi)
99}