blob: 6de9b13d6ef3209d0f7ab71d5e6ee2328cec029b [file] [log] [blame]
Anton Carvere342c612015-10-15 19:13:47 +01001// 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
5package constdecl
6
7import "math"
8
9var v int
10
11// Const decls must be initialized by constants.
12const _ = v /* ERROR "not constant" */
13const _ = math /* ERROR "not constant" */ .Sin(0)
14const _ = int /* ERROR "not an expression" */
15
16func _() {
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.
26const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
27const _ = 1, 2 /* ERROR "extra init expr 2" */
28
29const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
30const _ int = 1, 2 /* ERROR "extra init expr 2" */
31
32const (
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
40const (
41 _ = 1
42 _
43 _, _ /* ERROR "missing init expr for _" */
44 _
45)
46
47const (
48 _, _ = 1, 2
49 _, _
50 _ /* ERROR "extra init expr at" */
51 _, _
52 _, _, _ /* ERROR "missing init expr for _" */
53 _, _
54)
55
56func _() {
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).
90func _() {
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