Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // errorcheck |
Russ Cox | 1163b1d | 2008-10-16 15:59:31 -0700 | [diff] [blame] | 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 | |
Rob Pike | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 7 | // Verify that illegal assignments with both explicit and implicit conversions of literals are detected. |
| 8 | // Does not compile. |
| 9 | |
Russ Cox | 1163b1d | 2008-10-16 15:59:31 -0700 | [diff] [blame] | 10 | package main |
| 11 | |
Josh Bleecher Snyder | a3dfcf5 | 2015-05-01 19:50:27 -0700 | [diff] [blame] | 12 | import "unsafe" |
| 13 | |
Rob Pike | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 14 | // explicit conversion of constants |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 15 | var x1 = string(1) |
| 16 | var x2 string = string(1) |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 17 | var x3 = int(1.5) // ERROR "convert|truncate" |
| 18 | var x4 int = int(1.5) // ERROR "convert|truncate" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 19 | var x5 = "a" + string(1) |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 20 | var x6 = int(1e100) // ERROR "overflow" |
| 21 | var x7 = float32(1e1000) // ERROR "overflow" |
Russ Cox | 1163b1d | 2008-10-16 15:59:31 -0700 | [diff] [blame] | 22 | |
Josh Bleecher Snyder | a3dfcf5 | 2015-05-01 19:50:27 -0700 | [diff] [blame] | 23 | // unsafe.Pointer can only convert to/from uintptr |
| 24 | var _ = string(unsafe.Pointer(uintptr(65))) // ERROR "convert" |
| 25 | var _ = float64(unsafe.Pointer(uintptr(65))) // ERROR "convert" |
| 26 | var _ = int(unsafe.Pointer(uintptr(65))) // ERROR "convert" |
| 27 | |
Russ Cox | 1163b1d | 2008-10-16 15:59:31 -0700 | [diff] [blame] | 28 | // implicit conversions merit scrutiny |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 29 | var s string |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 30 | var bad1 string = 1 // ERROR "conver|incompatible|invalid|cannot" |
| 31 | var bad2 = s + 1 // ERROR "conver|incompatible|invalid" |
| 32 | var bad3 = s + 'a' // ERROR "conver|incompatible|invalid" |
| 33 | var bad4 = "a" + 1 // ERROR "literals|incompatible|convert|invalid" |
| 34 | var bad5 = "a" + 'a' // ERROR "literals|incompatible|convert|invalid" |
Russ Cox | 1163b1d | 2008-10-16 15:59:31 -0700 | [diff] [blame] | 35 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 36 | var bad6 int = 1.5 // ERROR "convert|truncate" |
| 37 | var bad7 int = 1e100 // ERROR "overflow" |
| 38 | var bad8 float32 = 1e200 // ERROR "overflow" |
Russ Cox | 1163b1d | 2008-10-16 15:59:31 -0700 | [diff] [blame] | 39 | |
| 40 | // but these implicit conversions are okay |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 41 | var good1 string = "a" |
| 42 | var good2 int = 1.0 |
| 43 | var good3 int = 1e9 |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 44 | var good4 float64 = 1e20 |
Russ Cox | 1163b1d | 2008-10-16 15:59:31 -0700 | [diff] [blame] | 45 | |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 46 | // explicit conversion of string is okay |
Russ Cox | db33959 | 2011-10-25 22:20:02 -0700 | [diff] [blame] | 47 | var _ = []rune("abc") |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 48 | var _ = []byte("abc") |
| 49 | |
| 50 | // implicit is not |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 51 | var _ []int = "abc" // ERROR "cannot use|incompatible|invalid" |
| 52 | var _ []byte = "abc" // ERROR "cannot use|incompatible|invalid" |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 53 | |
| 54 | // named string is okay |
| 55 | type Tstring string |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 56 | |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 57 | var ss Tstring = "abc" |
Russ Cox | db33959 | 2011-10-25 22:20:02 -0700 | [diff] [blame] | 58 | var _ = []rune(ss) |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 59 | var _ = []byte(ss) |
| 60 | |
| 61 | // implicit is still not |
Russ Cox | db33959 | 2011-10-25 22:20:02 -0700 | [diff] [blame] | 62 | var _ []rune = ss // ERROR "cannot use|incompatible|invalid" |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 63 | var _ []byte = ss // ERROR "cannot use|incompatible|invalid" |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 64 | |
Russ Cox | 6e3e380 | 2011-11-22 12:30:02 -0500 | [diff] [blame] | 65 | // named slice is now ok |
Russ Cox | db33959 | 2011-10-25 22:20:02 -0700 | [diff] [blame] | 66 | type Trune []rune |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 67 | type Tbyte []byte |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 68 | |
Russ Cox | 6e3e380 | 2011-11-22 12:30:02 -0500 | [diff] [blame] | 69 | var _ = Trune("abc") // ok |
| 70 | var _ = Tbyte("abc") // ok |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 71 | |
| 72 | // implicit is still not |
Russ Cox | db33959 | 2011-10-25 22:20:02 -0700 | [diff] [blame] | 73 | var _ Trune = "abc" // ERROR "cannot use|incompatible|invalid" |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 74 | var _ Tbyte = "abc" // ERROR "cannot use|incompatible|invalid" |