blob: 904e1e63b138411c6969d76e7589da2a3bf28121 [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
Josh Bleecher Snydera3dfcf52015-05-01 19:50:27 -070012import "unsafe"
13
Rob Pikefc0dc042012-02-19 13:19:43 +110014// explicit conversion of constants
Rob Pike4f61fc92010-09-04 10:36:13 +100015var x1 = string(1)
16var x2 string = string(1)
Russ Coxf2b5a072011-01-19 23:09:00 -050017var x3 = int(1.5) // ERROR "convert|truncate"
18var x4 int = int(1.5) // ERROR "convert|truncate"
Rob Pike4f61fc92010-09-04 10:36:13 +100019var x5 = "a" + string(1)
Russ Coxf2b5a072011-01-19 23:09:00 -050020var x6 = int(1e100) // ERROR "overflow"
21var x7 = float32(1e1000) // ERROR "overflow"
Russ Cox1163b1d2008-10-16 15:59:31 -070022
Josh Bleecher Snydera3dfcf52015-05-01 19:50:27 -070023// unsafe.Pointer can only convert to/from uintptr
24var _ = string(unsafe.Pointer(uintptr(65))) // ERROR "convert"
25var _ = float64(unsafe.Pointer(uintptr(65))) // ERROR "convert"
26var _ = int(unsafe.Pointer(uintptr(65))) // ERROR "convert"
27
Russ Cox1163b1d2008-10-16 15:59:31 -070028// implicit conversions merit scrutiny
Rob Pike4f61fc92010-09-04 10:36:13 +100029var s string
Russ Coxf2b5a072011-01-19 23:09:00 -050030var bad1 string = 1 // ERROR "conver|incompatible|invalid|cannot"
31var bad2 = s + 1 // ERROR "conver|incompatible|invalid"
32var bad3 = s + 'a' // ERROR "conver|incompatible|invalid"
33var bad4 = "a" + 1 // ERROR "literals|incompatible|convert|invalid"
34var bad5 = "a" + 'a' // ERROR "literals|incompatible|convert|invalid"
Russ Cox1163b1d2008-10-16 15:59:31 -070035
Russ Coxf2b5a072011-01-19 23:09:00 -050036var bad6 int = 1.5 // ERROR "convert|truncate"
37var bad7 int = 1e100 // ERROR "overflow"
38var bad8 float32 = 1e200 // ERROR "overflow"
Russ Cox1163b1d2008-10-16 15:59:31 -070039
40// but these implicit conversions are okay
Rob Pike4f61fc92010-09-04 10:36:13 +100041var good1 string = "a"
42var good2 int = 1.0
43var good3 int = 1e9
Russ Coxf2b5a072011-01-19 23:09:00 -050044var good4 float64 = 1e20
Russ Cox1163b1d2008-10-16 15:59:31 -070045
Russ Cox39101612010-02-25 15:11:07 -080046// explicit conversion of string is okay
Russ Coxdb339592011-10-25 22:20:02 -070047var _ = []rune("abc")
Russ Cox39101612010-02-25 15:11:07 -080048var _ = []byte("abc")
49
50// implicit is not
Russ Coxf2b5a072011-01-19 23:09:00 -050051var _ []int = "abc" // ERROR "cannot use|incompatible|invalid"
52var _ []byte = "abc" // ERROR "cannot use|incompatible|invalid"
Russ Cox39101612010-02-25 15:11:07 -080053
54// named string is okay
55type Tstring string
Russ Coxf2b5a072011-01-19 23:09:00 -050056
Russ Cox39101612010-02-25 15:11:07 -080057var ss Tstring = "abc"
Russ Coxdb339592011-10-25 22:20:02 -070058var _ = []rune(ss)
Russ Cox39101612010-02-25 15:11:07 -080059var _ = []byte(ss)
60
61// implicit is still not
Russ Coxdb339592011-10-25 22:20:02 -070062var _ []rune = ss // ERROR "cannot use|incompatible|invalid"
Russ Coxf2b5a072011-01-19 23:09:00 -050063var _ []byte = ss // ERROR "cannot use|incompatible|invalid"
Russ Cox39101612010-02-25 15:11:07 -080064
Russ Cox6e3e3802011-11-22 12:30:02 -050065// named slice is now ok
Russ Coxdb339592011-10-25 22:20:02 -070066type Trune []rune
Russ Cox39101612010-02-25 15:11:07 -080067type Tbyte []byte
Russ Coxf2b5a072011-01-19 23:09:00 -050068
Russ Cox6e3e3802011-11-22 12:30:02 -050069var _ = Trune("abc") // ok
70var _ = Tbyte("abc") // ok
Russ Cox39101612010-02-25 15:11:07 -080071
72// implicit is still not
Russ Coxdb339592011-10-25 22:20:02 -070073var _ Trune = "abc" // ERROR "cannot use|incompatible|invalid"
Russ Coxf2b5a072011-01-19 23:09:00 -050074var _ Tbyte = "abc" // ERROR "cannot use|incompatible|invalid"