| 1 |
// Copyright 2012 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 |
// conversions |
| 6 |
|
| 7 |
package conversions |
| 8 |
|
| 9 |
import "unsafe" |
| 10 |
|
| 11 |
// argument count |
| 12 |
var ( |
| 13 |
_ = int() /* ERROR "missing argument" */ |
| 14 |
_ = int(1, 2 /* ERROR "too many arguments" */ ) |
| 15 |
) |
| 16 |
|
| 17 |
// numeric constant conversions are in const1.src. |
| 18 |
|
| 19 |
func string_conversions() { |
| 20 |
const A = string(65) |
| 21 |
assert(A == "A") |
| 22 |
const E = string(-1) |
| 23 |
assert(E == "\uFFFD") |
| 24 |
assert(E == string(1234567890)) |
| 25 |
|
| 26 |
type myint int |
| 27 |
assert(A == string(myint(65))) |
| 28 |
|
| 29 |
type mystring string |
| 30 |
const _ mystring = mystring("foo") |
| 31 |
|
| 32 |
const _ = string(true /* ERROR "cannot convert" */ ) |
| 33 |
const _ = string(1.2 /* ERROR "cannot convert" */ ) |
| 34 |
const _ = string(nil /* ERROR "cannot convert" */ ) |
| 35 |
|
| 36 |
// issues 11357, 11353: argument must be of integer type |
| 37 |
_ = string(0.0 /* ERROR "cannot convert" */ ) |
| 38 |
_ = string(0i /* ERROR "cannot convert" */ ) |
| 39 |
_ = string(1 /* ERROR "cannot convert" */ + 2i) |
| 40 |
} |
| 41 |
|
| 42 |
func interface_conversions() { |
| 43 |
type E interface{} |
| 44 |
|
| 45 |
type I1 interface{ |
| 46 |
m1() |
| 47 |
} |
| 48 |
|
| 49 |
type I2 interface{ |
| 50 |
m1() |
| 51 |
m2(x int) |
| 52 |
} |
| 53 |
|
| 54 |
type I3 interface{ |
| 55 |
m1() |
| 56 |
m2() int |
| 57 |
} |
| 58 |
|
| 59 |
var e E |
| 60 |
var i1 I1 |
| 61 |
var i2 I2 |
| 62 |
var i3 I3 |
| 63 |
|
| 64 |
_ = E(0) |
| 65 |
_ = E(nil) |
| 66 |
_ = E(e) |
| 67 |
_ = E(i1) |
| 68 |
_ = E(i2) |
| 69 |
|
| 70 |
_ = I1(0 /* ERROR "cannot convert" */ ) |
| 71 |
_ = I1(nil) |
| 72 |
_ = I1(i1) |
| 73 |
_ = I1(e /* ERROR "cannot convert" */ ) |
| 74 |
_ = I1(i2) |
| 75 |
|
| 76 |
_ = I2(nil) |
| 77 |
_ = I2(i1 /* ERROR "cannot convert" */ ) |
| 78 |
_ = I2(i2) |
| 79 |
_ = I2(i3 /* ERROR "cannot convert" */ ) |
| 80 |
|
| 81 |
_ = I3(nil) |
| 82 |
_ = I3(i1 /* ERROR "cannot convert" */ ) |
| 83 |
_ = I3(i2 /* ERROR "cannot convert" */ ) |
| 84 |
_ = I3(i3) |
| 85 |
|
| 86 |
// TODO(gri) add more tests, improve error message |
| 87 |
} |
| 88 |
|
| 89 |
func issue6326() { |
| 90 |
type T unsafe.Pointer |
| 91 |
var x T |
| 92 |
_ = uintptr(x) // see issue 6326 |
| 93 |
} |