~baidu-picture-scope-team/baidu-picture-scope/trunk

« back to all changes in this revision

Viewing changes to src/go/src/code.google.com/p/go.tools/go/types/testdata/vardecl.src

  • Committer: Zhang Enwei
  • Date: 2016-08-26 02:22:48 UTC
  • Revision ID: enwei.zhang@canonical.com-20160826022248-mf89jsetpcc2hg9e
Intial release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 vardecl
 
6
 
 
7
// Prerequisites.
 
8
func f() {}
 
9
func g() (x, y int) { return }
 
10
var m map[string]int
 
11
 
 
12
// Var decls must have a type or an initializer.
 
13
var _ int
 
14
var _, _ int
 
15
 
 
16
var _ /* ERROR "missing type or init expr" */
 
17
var _ /* ERROR "missing type or init expr" */, _
 
18
var _ /* ERROR "missing type or init expr" */, _, _
 
19
 
 
20
// The initializer must be an expression.
 
21
var _ = int /* ERROR "not an expression" */
 
22
var _ = f /* ERROR "used as value" */ ()
 
23
 
 
24
// Identifier and expression arity must match.
 
25
var _, _ = 1, 2
 
26
var _ = 1, 2 /* ERROR "extra init expr 2" */
 
27
var _, _ = 1 /* ERROR "assignment count mismatch" */
 
28
var _, _, _ /* ERROR "missing init expr for _" */ = 1, 2
 
29
 
 
30
var _ = g /* ERROR "2-valued expr" */ ()
 
31
var _, _ = g()
 
32
var _, _, _ = g /* ERROR "assignment count mismatch" */ ()
 
33
 
 
34
var _ = m["foo"]
 
35
var _, _ = m["foo"]
 
36
var _, _, _ = m  /* ERROR "assignment count mismatch" */ ["foo"]
 
37
 
 
38
var _, _ int = 1, 2
 
39
var _ int = 1, 2 /* ERROR "extra init expr 2" */
 
40
var _, _ int = 1 /* ERROR "assignment count mismatch" */
 
41
var _, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
 
42
 
 
43
var (
 
44
        _, _ = 1, 2
 
45
        _ = 1, 2 /* ERROR "extra init expr 2" */
 
46
        _, _ = 1 /* ERROR "assignment count mismatch" */
 
47
        _, _, _ /* ERROR "missing init expr for _" */ = 1, 2
 
48
 
 
49
        _ = g /* ERROR "2-valued expr" */ ()
 
50
        _, _ = g()
 
51
        _, _, _ = g /* ERROR "assignment count mismatch" */ ()
 
52
 
 
53
        _ = m["foo"]
 
54
        _, _ = m["foo"]
 
55
        _, _, _ = m /* ERROR "assignment count mismatch" */ ["foo"]
 
56
 
 
57
        _, _ int = 1, 2
 
58
        _ int = 1, 2 /* ERROR "extra init expr 2" */
 
59
        _, _ int = 1 /* ERROR "assignment count mismatch" */
 
60
        _, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
 
61
)
 
62
 
 
63
// Variables declared in function bodies must be 'used'.
 
64
type T struct{}
 
65
func (r T) _(a, b, c int) (u, v, w int) {
 
66
        var x1 /* ERROR "declared but not used" */ int
 
67
        var x2 /* ERROR "declared but not used" */ int
 
68
        x1 = 1
 
69
        (x2) = 2
 
70
 
 
71
        y1 /* ERROR "declared but not used" */ := 1
 
72
        y2 /* ERROR "declared but not used" */ := 2
 
73
        y1 = 1
 
74
        (y1) = 2
 
75
 
 
76
        {
 
77
                var x1 /* ERROR "declared but not used" */ int
 
78
                var x2 /* ERROR "declared but not used" */ int
 
79
                x1 = 1
 
80
                (x2) = 2
 
81
 
 
82
                y1 /* ERROR "declared but not used" */ := 1
 
83
                y2 /* ERROR "declared but not used" */ := 2
 
84
                y1 = 1
 
85
                (y1) = 2
 
86
        }
 
87
 
 
88
        if x /* ERROR "declared but not used" */ := 0; a < b {}
 
89
 
 
90
        switch x /* ERROR "declared but not used" */, y := 0, 1; a {
 
91
        case 0:
 
92
                _ = y
 
93
        case 1:
 
94
                x /* ERROR "declared but not used" */ := 0
 
95
        }
 
96
 
 
97
        var t interface{}
 
98
        switch t /* ERROR "declared but not used" */ := t.(type) {}
 
99
 
 
100
        switch t /* ERROR "declared but not used" */ := t.(type) {
 
101
        case int:
 
102
        }
 
103
 
 
104
        switch t /* ERROR "declared but not used" */ := t.(type) {
 
105
        case int:
 
106
        case float32, complex64:
 
107
                t = nil
 
108
        }
 
109
 
 
110
        switch t := t.(type) {
 
111
        case int:
 
112
        case float32, complex64:
 
113
                _ = t
 
114
        }
 
115
 
 
116
        switch t := t.(type) {
 
117
        case int:
 
118
        case float32:
 
119
        case string:
 
120
                _ = func() string {
 
121
                        return t
 
122
                }
 
123
        }
 
124
 
 
125
        switch t := t; t /* ERROR "declared but not used" */ := t.(type) {}
 
126
 
 
127
        var z1 /* ERROR "declared but not used" */ int
 
128
        var z2 int
 
129
        _ = func(a, b, c int) (u, v, w int) {
 
130
                z1 = a
 
131
                (z1) = b
 
132
                a = z2
 
133
                return
 
134
        }
 
135
 
 
136
        var s []int
 
137
        var i /* ERROR "declared but not used" */ , j int
 
138
        for i, j = range s {
 
139
                _ = j
 
140
        }
 
141
 
 
142
        for i, j /* ERROR "declared but not used" */ := range s {
 
143
                _ = func() int {
 
144
                        return i
 
145
                }
 
146
        }
 
147
        return
 
148
}
 
149
 
 
150
// Short variable declarations must declare at least one new non-blank variable.
 
151
func _() {
 
152
        _ := /* ERROR no new variables */ 0
 
153
        _, a := 0, 1
 
154
        _, a := /* ERROR no new variables */ 0, 1
 
155
        _, a, b := 0, 1, 2
 
156
        _, _, _ := /* ERROR no new variables */ 0, 1, 2
 
157
 
 
158
        _ = a
 
159
        _ = b
 
160
}
 
161
 
 
162
// TODO(gri) consolidate other var decl checks in this file
 
 
b'\\ No newline at end of file'