Anton Carver | e342c61 | 2015-10-15 19:13:47 +0100 | [diff] [blame^] | 1 | // Copyright 2013 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 constdecl |
| 6 | |
| 7 | import "math" |
| 8 | |
| 9 | var v int |
| 10 | |
| 11 | // Const decls must be initialized by constants. |
| 12 | const _ = v /* ERROR "not constant" */ |
| 13 | const _ = math /* ERROR "not constant" */ .Sin(0) |
| 14 | const _ = int /* ERROR "not an expression" */ |
| 15 | |
| 16 | func _() { |
| 17 | const _ = v /* ERROR "not constant" */ |
| 18 | const _ = math /* ERROR "not constant" */ .Sin(0) |
| 19 | const _ = int /* ERROR "not an expression" */ |
| 20 | } |
| 21 | |
| 22 | // Identifier and expression arity must match. |
| 23 | // The first error message is produced by the parser. |
| 24 | // In a real-world scenario, the type-checker would not be run |
| 25 | // in this case and the 2nd error message would not appear. |
| 26 | const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ |
| 27 | const _ = 1, 2 /* ERROR "extra init expr 2" */ |
| 28 | |
| 29 | const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int |
| 30 | const _ int = 1, 2 /* ERROR "extra init expr 2" */ |
| 31 | |
| 32 | const ( |
| 33 | _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ |
| 34 | _ = 1, 2 /* ERROR "extra init expr 2" */ |
| 35 | |
| 36 | _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int |
| 37 | _ int = 1, 2 /* ERROR "extra init expr 2" */ |
| 38 | ) |
| 39 | |
| 40 | const ( |
| 41 | _ = 1 |
| 42 | _ |
| 43 | _, _ /* ERROR "missing init expr for _" */ |
| 44 | _ |
| 45 | ) |
| 46 | |
| 47 | const ( |
| 48 | _, _ = 1, 2 |
| 49 | _, _ |
| 50 | _ /* ERROR "extra init expr at" */ |
| 51 | _, _ |
| 52 | _, _, _ /* ERROR "missing init expr for _" */ |
| 53 | _, _ |
| 54 | ) |
| 55 | |
| 56 | func _() { |
| 57 | const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ |
| 58 | const _ = 1, 2 /* ERROR "extra init expr 2" */ |
| 59 | |
| 60 | const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int |
| 61 | const _ int = 1, 2 /* ERROR "extra init expr 2" */ |
| 62 | |
| 63 | const ( |
| 64 | _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ |
| 65 | _ = 1, 2 /* ERROR "extra init expr 2" */ |
| 66 | |
| 67 | _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int |
| 68 | _ int = 1, 2 /* ERROR "extra init expr 2" */ |
| 69 | ) |
| 70 | |
| 71 | const ( |
| 72 | _ = 1 |
| 73 | _ |
| 74 | _, _ /* ERROR "missing init expr for _" */ |
| 75 | _ |
| 76 | ) |
| 77 | |
| 78 | const ( |
| 79 | _, _ = 1, 2 |
| 80 | _, _ |
| 81 | _ /* ERROR "extra init expr at" */ |
| 82 | _, _ |
| 83 | _, _, _ /* ERROR "missing init expr for _" */ |
| 84 | _, _ |
| 85 | ) |
| 86 | } |
| 87 | |
| 88 | // Test case for constant with invalid initialization. |
| 89 | // Caused panic because the constant value was not set up (gri - 7/8/2014). |
| 90 | func _() { |
| 91 | const ( |
| 92 | x string = missing /* ERROR "undeclared name" */ |
| 93 | y = x + "" |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | // TODO(gri) move extra tests from testdata/const0.src into here |