blob: 1e82d1f2f561ebc60ac903066a4fb87795df8381 [file] [log] [blame]
Russ Cox1163b1d2008-10-16 15:59:31 -07001// errchk $G -e $D/$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 Cox21c9f822009-05-05 11:23:39 -07009// explicit conversion of constants is work in progress.
10// the ERRORs in this block are debatable, but they're what
11// the language spec says for now.
Rob Pike4f61fc92010-09-04 10:36:13 +100012var x1 = string(1)
13var x2 string = string(1)
Russ Coxf2b5a072011-01-19 23:09:00 -050014var x3 = int(1.5) // ERROR "convert|truncate"
15var x4 int = int(1.5) // ERROR "convert|truncate"
Rob Pike4f61fc92010-09-04 10:36:13 +100016var x5 = "a" + string(1)
Russ Coxf2b5a072011-01-19 23:09:00 -050017var x6 = int(1e100) // ERROR "overflow"
18var x7 = float32(1e1000) // ERROR "overflow"
Russ Cox1163b1d2008-10-16 15:59:31 -070019
20// implicit conversions merit scrutiny
Rob Pike4f61fc92010-09-04 10:36:13 +100021var s string
Russ Coxf2b5a072011-01-19 23:09:00 -050022var bad1 string = 1 // ERROR "conver|incompatible|invalid|cannot"
23var bad2 = s + 1 // ERROR "conver|incompatible|invalid"
24var bad3 = s + 'a' // ERROR "conver|incompatible|invalid"
25var bad4 = "a" + 1 // ERROR "literals|incompatible|convert|invalid"
26var bad5 = "a" + 'a' // ERROR "literals|incompatible|convert|invalid"
Russ Cox1163b1d2008-10-16 15:59:31 -070027
Russ Coxf2b5a072011-01-19 23:09:00 -050028var bad6 int = 1.5 // ERROR "convert|truncate"
29var bad7 int = 1e100 // ERROR "overflow"
30var bad8 float32 = 1e200 // ERROR "overflow"
Russ Cox1163b1d2008-10-16 15:59:31 -070031
32// but these implicit conversions are okay
Rob Pike4f61fc92010-09-04 10:36:13 +100033var good1 string = "a"
34var good2 int = 1.0
35var good3 int = 1e9
Russ Coxf2b5a072011-01-19 23:09:00 -050036var good4 float64 = 1e20
Russ Cox1163b1d2008-10-16 15:59:31 -070037
Russ Cox39101612010-02-25 15:11:07 -080038// explicit conversion of string is okay
Russ Coxdb339592011-10-25 22:20:02 -070039var _ = []rune("abc")
Russ Cox39101612010-02-25 15:11:07 -080040var _ = []byte("abc")
41
42// implicit is not
Russ Coxf2b5a072011-01-19 23:09:00 -050043var _ []int = "abc" // ERROR "cannot use|incompatible|invalid"
44var _ []byte = "abc" // ERROR "cannot use|incompatible|invalid"
Russ Cox39101612010-02-25 15:11:07 -080045
46// named string is okay
47type Tstring string
Russ Coxf2b5a072011-01-19 23:09:00 -050048
Russ Cox39101612010-02-25 15:11:07 -080049var ss Tstring = "abc"
Russ Coxdb339592011-10-25 22:20:02 -070050var _ = []rune(ss)
Russ Cox39101612010-02-25 15:11:07 -080051var _ = []byte(ss)
52
53// implicit is still not
Russ Coxdb339592011-10-25 22:20:02 -070054var _ []rune = ss // ERROR "cannot use|incompatible|invalid"
Russ Coxf2b5a072011-01-19 23:09:00 -050055var _ []byte = ss // ERROR "cannot use|incompatible|invalid"
Russ Cox39101612010-02-25 15:11:07 -080056
Russ Cox6e3e3802011-11-22 12:30:02 -050057// named slice is now ok
Russ Coxdb339592011-10-25 22:20:02 -070058type Trune []rune
Russ Cox39101612010-02-25 15:11:07 -080059type Tbyte []byte
Russ Coxf2b5a072011-01-19 23:09:00 -050060
Russ Cox6e3e3802011-11-22 12:30:02 -050061var _ = Trune("abc") // ok
62var _ = Tbyte("abc") // ok
Russ Cox39101612010-02-25 15:11:07 -080063
64// implicit is still not
Russ Coxdb339592011-10-25 22:20:02 -070065var _ Trune = "abc" // ERROR "cannot use|incompatible|invalid"
Russ Coxf2b5a072011-01-19 23:09:00 -050066var _ Tbyte = "abc" // ERROR "cannot use|incompatible|invalid"