1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package conversions
6
7 import "unsafe"
8
9 // constant conversions
10
11 func _[T ~byte]() T { return 255 }
12 func _[T ~byte]() T { return 256 /* ERROR cannot use 256 .* as T value */ }
13
14 func _[T ~byte]() {
15 const _ = T /* ERROR T\(0\) .* is not constant */ (0)
16 var _ T = 255
17 var _ T = 256 // ERROR cannot use 256 .* as T value
18 }
19
20 func _[T ~string]() T { return T('a') }
21 func _[T ~int | ~string]() T { return T('a') }
22 func _[T ~byte | ~int | ~string]() T { return T(256 /* ERROR cannot convert 256 .* to T */ ) }
23
24 // implicit conversions never convert to string
25 func _[T ~string]() {
26 var _ string = 0 // ERROR cannot use .* as string value
27 var _ T = 0 // ERROR cannot use .* as T value
28 }
29
30 // failing const conversions of constants to type parameters report a cause
31 func _[
32 T1 any,
33 T2 interface{ m() },
34 T3 ~int | ~float64 | ~bool,
35 T4 ~int | ~string,
36 ]() {
37 // TODO(rfindley): align the error formatting here with types2.
38 _ = T1(0 /* ERROR cannot convert 0 .* to T1.*T1 does not contain specific types */ )
39 _ = T2(1 /* ERROR cannot convert 1 .* to T2.*T2 does not contain specific types */ )
40 _ = T3(2 /* ERROR cannot convert 2 .* to T3.*cannot convert 2 .* to bool \(in T3\) */ )
41 _ = T4(3.14 /* ERROR cannot convert 3.14 .* to T4.*cannot convert 3.14 .* to int \(in T4\) */ )
42 }
43
44 // "x is assignable to T"
45 // - tested via assignability tests
46
47 // "x's type and T have identical underlying types if tags are ignored"
48
49 func _[X ~int, T ~int](x X) T { return T(x) }
50 func _[X struct{f int "foo"}, T struct{f int "bar"}](x X) T { return T(x) }
51
52 type Foo struct{f int "foo"}
53 type Bar struct{f int "bar"}
54 type Far struct{f float64 }
55
56 func _[X Foo, T Bar](x X) T { return T(x) }
57 func _[X Foo|Bar, T Bar](x X) T { return T(x) }
58 func _[X Foo, T Foo|Bar](x X) T { return T(x) }
59 func _[X Foo, T Far](x X) T { return T(x /* ERROR cannot convert x \(variable of type X constrained by Foo\) to T.*cannot convert Foo \(in X\) to Far \(in T\) */ ) }
60
61 // "x's type and T are unnamed pointer types and their pointer base types
62 // have identical underlying types if tags are ignored"
63
64 func _[X ~*Foo, T ~*Bar](x X) T { return T(x) }
65 func _[X ~*Foo|~*Bar, T ~*Bar](x X) T { return T(x) }
66 func _[X ~*Foo, T ~*Foo|~*Bar](x X) T { return T(x) }
67 func _[X ~*Foo, T ~*Far](x X) T { return T(x /* ERROR cannot convert x \(variable of type X constrained by ~\*Foo\) to T.*cannot convert \*Foo \(in X\) to \*Far \(in T\) */ ) }
68
69 // Verify that the defined types in constraints are considered for the rule above.
70
71 type (
72 B int
73 C int
74 X0 *B
75 T0 *C
76 )
77
78 func _(x X0) T0 { return T0(x /* ERROR cannot convert */ ) } // non-generic reference
79 func _[X X0, T T0](x X) T { return T(x /* ERROR cannot convert */ ) }
80 func _[T T0](x X0) T { return T(x /* ERROR cannot convert */ ) }
81 func _[X X0](x X) T0 { return T0(x /* ERROR cannot convert */ ) }
82
83 // "x's type and T are both integer or floating point types"
84
85 func _[X Integer, T Integer](x X) T { return T(x) }
86 func _[X Unsigned, T Integer](x X) T { return T(x) }
87 func _[X Float, T Integer](x X) T { return T(x) }
88
89 func _[X Integer, T Unsigned](x X) T { return T(x) }
90 func _[X Unsigned, T Unsigned](x X) T { return T(x) }
91 func _[X Float, T Unsigned](x X) T { return T(x) }
92
93 func _[X Integer, T Float](x X) T { return T(x) }
94 func _[X Unsigned, T Float](x X) T { return T(x) }
95 func _[X Float, T Float](x X) T { return T(x) }
96
97 func _[X, T Integer|Unsigned|Float](x X) T { return T(x) }
98 func _[X, T Integer|~string](x X) T { return T(x /* ERROR cannot convert x \(variable of type X constrained by Integer\|~string\) to T.*cannot convert string \(in X\) to int \(in T\) */ ) }
99
100 // "x's type and T are both complex types"
101
102 func _[X, T Complex](x X) T { return T(x) }
103 func _[X, T Float|Complex](x X) T { return T(x /* ERROR cannot convert x \(variable of type X constrained by Float\|Complex\) to T.*cannot convert float32 \(in X\) to complex64 \(in T\) */ ) }
104
105 // "x is an integer or a slice of bytes or runes and T is a string type"
106
107 type myInt int
108 type myString string
109
110 func _[T ~string](x int) T { return T(x) }
111 func _[T ~string](x myInt) T { return T(x) }
112 func _[X Integer](x X) string { return string(x) }
113 func _[X Integer](x X) myString { return myString(x) }
114 func _[X Integer](x X) *string { return (*string)(x /* ERROR cannot convert x \(variable of type X constrained by Integer\) to \*string.*cannot convert int \(in X\) to \*string */ ) }
115
116 func _[T ~string](x []byte) T { return T(x) }
117 func _[T ~string](x []rune) T { return T(x) }
118 func _[X ~[]byte, T ~string](x X) T { return T(x) }
119 func _[X ~[]rune, T ~string](x X) T { return T(x) }
120 func _[X Integer|~[]byte|~[]rune, T ~string](x X) T { return T(x) }
121 func _[X Integer|~[]byte|~[]rune, T ~*string](x X) T { return T(x /* ERROR cannot convert x \(variable of type X constrained by Integer\|~\[\]byte\|~\[\]rune\) to T.*cannot convert int \(in X\) to \*string \(in T\) */ ) }
122
123 // "x is a string and T is a slice of bytes or runes"
124
125 func _[T ~[]byte](x string) T { return T(x) }
126 func _[T ~[]rune](x string) T { return T(x) }
127 func _[T ~[]rune](x *string) T { return T(x /* ERROR cannot convert x \(variable of type \*string\) to T.*cannot convert \*string to \[\]rune \(in T\) */ ) }
128
129 func _[X ~string, T ~[]byte](x X) T { return T(x) }
130 func _[X ~string, T ~[]rune](x X) T { return T(x) }
131 func _[X ~string, T ~[]byte|~[]rune](x X) T { return T(x) }
132 func _[X ~*string, T ~[]byte|~[]rune](x X) T { return T(x /* ERROR cannot convert x \(variable of type X constrained by ~\*string\) to T.*cannot convert \*string \(in X\) to \[\]byte \(in T\) */ ) }
133
134 // package unsafe:
135 // "any pointer or value of underlying type uintptr can be converted into a unsafe.Pointer"
136
137 type myUintptr uintptr
138
139 func _[X ~uintptr](x X) unsafe.Pointer { return unsafe.Pointer(x) }
140 func _[T unsafe.Pointer](x myUintptr) T { return T(x) }
141 func _[T unsafe.Pointer](x int64) T { return T(x /* ERROR cannot convert x \(variable of type int64\) to T.*cannot convert int64 to unsafe\.Pointer \(in T\) */ ) }
142
143 // "and vice versa"
144
145 func _[T ~uintptr](x unsafe.Pointer) T { return T(x) }
146 func _[X unsafe.Pointer](x X) uintptr { return uintptr(x) }
147 func _[X unsafe.Pointer](x X) myUintptr { return myUintptr(x) }
148 func _[X unsafe.Pointer](x X) int64 { return int64(x /* ERROR cannot convert x \(variable of type X constrained by unsafe\.Pointer\) to int64.*cannot convert unsafe\.Pointer \(in X\) to int64 */ ) }
149
150 // "x is a slice, T is a pointer-to-array type,
151 // and the slice and array types have identical element types."
152
153 func _[X ~[]E, T ~*[10]E, E any](x X) T { return T(x) }
154 func _[X ~[]E, T ~[10]E, E any](x X) T { return T(x /* ERROR cannot convert x \(variable of type X constrained by ~\[\]E\) to T.*cannot convert \[\]E \(in X\) to \[10\]E \(in T\) */ ) }
155
156 // ----------------------------------------------------------------------------
157 // The following declarations can be replaced by the exported types of the
158 // constraints package once all builders support importing interfaces with
159 // type constraints.
160
161 type Signed interface {
162 ~int | ~int8 | ~int16 | ~int32 | ~int64
163 }
164
165 type Unsigned interface {
166 ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
167 }
168
169 type Integer interface {
170 Signed | Unsigned
171 }
172
173 type Float interface {
174 ~float32 | ~float64
175 }
176
177 type Complex interface {
178 ~complex64 | ~complex128
179 }
180
View as plain text