]> Cypherpunks.ru repositories - gostls13.git/blob - test/convlit.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / test / convlit.go
1 // errorcheck
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
7 // Verify that illegal assignments with both explicit and implicit conversions of literals are detected.
8 // Does not compile.
9
10 package main
11
12 import "unsafe"
13
14 // explicit conversion of constants
15 var x1 = string(1)
16 var x2 string = string(1)
17 var x3 = int(1.5)     // ERROR "convert|truncate"
18 var x4 int = int(1.5) // ERROR "convert|truncate"
19 var x5 = "a" + string(1)
20 var x6 = int(1e100)      // ERROR "overflow|cannot convert"
21 var x7 = float32(1e1000) // ERROR "overflow|cannot convert"
22
23 // unsafe.Pointer can only convert to/from uintptr
24 var _ = string(unsafe.Pointer(uintptr(65)))  // ERROR "convert|conversion"
25 var _ = float64(unsafe.Pointer(uintptr(65))) // ERROR "convert|conversion"
26 var _ = int(unsafe.Pointer(uintptr(65)))     // ERROR "convert|conversion"
27
28 // implicit conversions merit scrutiny
29 var s string
30 var bad1 string = 1  // ERROR "conver|incompatible|invalid|cannot"
31 var bad2 = s + 1     // ERROR "conver|incompatible|invalid|cannot"
32 var bad3 = s + 'a'   // ERROR "conver|incompatible|invalid|cannot"
33 var bad4 = "a" + 1   // ERROR "literals|incompatible|convert|invalid"
34 var bad5 = "a" + 'a' // ERROR "literals|incompatible|convert|invalid"
35
36 var bad6 int = 1.5       // ERROR "convert|truncate"
37 var bad7 int = 1e100     // ERROR "overflow|truncated to int|truncated"
38 var bad8 float32 = 1e200 // ERROR "overflow"
39
40 // but these implicit conversions are okay
41 var good1 string = "a"
42 var good2 int = 1.0
43 var good3 int = 1e9
44 var good4 float64 = 1e20
45
46 // explicit conversion of string is okay
47 var _ = []rune("abc")
48 var _ = []byte("abc")
49
50 // implicit is not
51 var _ []int = "abc"  // ERROR "cannot use|incompatible|invalid|cannot convert"
52 var _ []byte = "abc" // ERROR "cannot use|incompatible|invalid|cannot convert"
53
54 // named string is okay
55 type Tstring string
56
57 var ss Tstring = "abc"
58 var _ = []rune(ss)
59 var _ = []byte(ss)
60
61 // implicit is still not
62 var _ []rune = ss // ERROR "cannot use|incompatible|invalid"
63 var _ []byte = ss // ERROR "cannot use|incompatible|invalid"
64
65 // named slice is now ok
66 type Trune []rune
67 type Tbyte []byte
68
69 var _ = Trune("abc") // ok
70 var _ = Tbyte("abc") // ok
71
72 // implicit is still not
73 var _ Trune = "abc" // ERROR "cannot use|incompatible|invalid|cannot convert"
74 var _ Tbyte = "abc" // ERROR "cannot use|incompatible|invalid|cannot convert"