blob: 8a6145d2a0bed8cc6623f5f79aa773df26a16f6c [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// errorcheck
Russ Cox1163b1d2008-10-16 15:59:31 -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 that illegal assignments with both explicit and implicit conversions of literals are detected.
8// Does not compile.
9
Russ Cox1163b1d2008-10-16 15:59:31 -070010package main
11
Rob Pikefc0dc042012-02-19 13:19:43 +110012// explicit conversion of constants
Rob Pike4f61fc92010-09-04 10:36:13 +100013var x1 = string(1)
14var x2 string = string(1)
Russ Coxf2b5a072011-01-19 23:09:00 -050015var x3 = int(1.5) // ERROR "convert|truncate"
16var x4 int = int(1.5) // ERROR "convert|truncate"
Rob Pike4f61fc92010-09-04 10:36:13 +100017var x5 = "a" + string(1)
Russ Coxf2b5a072011-01-19 23:09:00 -050018var x6 = int(1e100) // ERROR "overflow"
19var x7 = float32(1e1000) // ERROR "overflow"
Russ Cox1163b1d2008-10-16 15:59:31 -070020
21// implicit conversions merit scrutiny
Rob Pike4f61fc92010-09-04 10:36:13 +100022var s string
Russ Coxf2b5a072011-01-19 23:09:00 -050023var bad1 string = 1 // ERROR "conver|incompatible|invalid|cannot"
24var bad2 = s + 1 // ERROR "conver|incompatible|invalid"
25var bad3 = s + 'a' // ERROR "conver|incompatible|invalid"
26var bad4 = "a" + 1 // ERROR "literals|incompatible|convert|invalid"
27var bad5 = "a" + 'a' // ERROR "literals|incompatible|convert|invalid"
Russ Cox1163b1d2008-10-16 15:59:31 -070028
Russ Coxf2b5a072011-01-19 23:09:00 -050029var bad6 int = 1.5 // ERROR "convert|truncate"
30var bad7 int = 1e100 // ERROR "overflow"
31var bad8 float32 = 1e200 // ERROR "overflow"
Russ Cox1163b1d2008-10-16 15:59:31 -070032
33// but these implicit conversions are okay
Rob Pike4f61fc92010-09-04 10:36:13 +100034var good1 string = "a"
35var good2 int = 1.0
36var good3 int = 1e9
Russ Coxf2b5a072011-01-19 23:09:00 -050037var good4 float64 = 1e20
Russ Cox1163b1d2008-10-16 15:59:31 -070038
Russ Cox39101612010-02-25 15:11:07 -080039// explicit conversion of string is okay
Russ Coxdb339592011-10-25 22:20:02 -070040var _ = []rune("abc")
Russ Cox39101612010-02-25 15:11:07 -080041var _ = []byte("abc")
42
43// implicit is not
Russ Coxf2b5a072011-01-19 23:09:00 -050044var _ []int = "abc" // ERROR "cannot use|incompatible|invalid"
45var _ []byte = "abc" // ERROR "cannot use|incompatible|invalid"
Russ Cox39101612010-02-25 15:11:07 -080046
47// named string is okay
48type Tstring string
Russ Coxf2b5a072011-01-19 23:09:00 -050049
Russ Cox39101612010-02-25 15:11:07 -080050var ss Tstring = "abc"
Russ Coxdb339592011-10-25 22:20:02 -070051var _ = []rune(ss)
Russ Cox39101612010-02-25 15:11:07 -080052var _ = []byte(ss)
53
54// implicit is still not
Russ Coxdb339592011-10-25 22:20:02 -070055var _ []rune = ss // ERROR "cannot use|incompatible|invalid"
Russ Coxf2b5a072011-01-19 23:09:00 -050056var _ []byte = ss // ERROR "cannot use|incompatible|invalid"
Russ Cox39101612010-02-25 15:11:07 -080057
Russ Cox6e3e3802011-11-22 12:30:02 -050058// named slice is now ok
Russ Coxdb339592011-10-25 22:20:02 -070059type Trune []rune
Russ Cox39101612010-02-25 15:11:07 -080060type Tbyte []byte
Russ Coxf2b5a072011-01-19 23:09:00 -050061
Russ Cox6e3e3802011-11-22 12:30:02 -050062var _ = Trune("abc") // ok
63var _ = Tbyte("abc") // ok
Russ Cox39101612010-02-25 15:11:07 -080064
65// implicit is still not
Russ Coxdb339592011-10-25 22:20:02 -070066var _ Trune = "abc" // ERROR "cannot use|incompatible|invalid"
Russ Coxf2b5a072011-01-19 23:09:00 -050067var _ Tbyte = "abc" // ERROR "cannot use|incompatible|invalid"