Source file
src/go/types/range.go
1
2
3
4
5
6
7
8
9
10 package types
11
12 import (
13 "go/ast"
14 "go/constant"
15 "internal/buildcfg"
16 . "internal/types/errors"
17 )
18
19
20
21
22
23
24
25
26
27 func (check *Checker) rangeStmt(inner stmtContext, rangeStmt *ast.RangeStmt, noNewVarPos positioner, sKey, sValue, sExtra, rangeVar ast.Expr, isDef bool) {
28
29 var x operand
30
31
32
33
34
35
36
37
38 check.hasCallOrRecv = false
39 check.expr(nil, &x, rangeVar)
40
41 if isTypes2 && x.mode != invalid && sValue == nil && !check.hasCallOrRecv {
42 if t, ok := arrayPtrDeref(under(x.typ)).(*Array); ok {
43 for {
44
45
46
47 p, ok := rangeVar.(*ast.ParenExpr)
48 if !ok {
49 break
50 }
51 rangeVar = p.X
52 }
53
54
55
56 check.record(&operand{
57 mode: constant_,
58 expr: rangeVar,
59 typ: Typ[Int],
60 val: constant.MakeInt64(t.len),
61 id: x.id,
62 })
63 }
64 }
65
66
67 var key, val Type
68 if x.mode != invalid {
69 k, v, cause, ok := rangeKeyVal(check, x.typ, func(v goVersion) bool {
70 return check.allowVersion(v)
71 })
72 switch {
73 case !ok && cause != "":
74 check.softErrorf(&x, InvalidRangeExpr, "cannot range over %s: %s", &x, cause)
75 case !ok:
76 check.softErrorf(&x, InvalidRangeExpr, "cannot range over %s", &x)
77 case k == nil && sKey != nil:
78 check.softErrorf(sKey, InvalidIterVar, "range over %s permits no iteration variables", &x)
79 case v == nil && sValue != nil:
80 check.softErrorf(sValue, InvalidIterVar, "range over %s permits only one iteration variable", &x)
81 case sExtra != nil:
82 check.softErrorf(sExtra, InvalidIterVar, "range clause permits at most two iteration variables")
83 }
84 key, val = k, v
85 }
86
87
88
89 check.openScope(rangeStmt, "range")
90 defer check.closeScope()
91
92
93
94
95
96 lhs := [2]ast.Expr{sKey, sValue}
97 rhs := [2]Type{key, val}
98
99 rangeOverInt := isInteger(x.typ)
100
101 if isDef {
102
103 var vars []*Var
104 for i, lhs := range lhs {
105 if lhs == nil {
106 continue
107 }
108
109
110 var obj *Var
111 if ident, _ := lhs.(*ast.Ident); ident != nil {
112
113 name := ident.Name
114 obj = newVar(LocalVar, ident.Pos(), check.pkg, name, nil)
115 check.recordDef(ident, obj)
116
117 if name != "_" {
118 vars = append(vars, obj)
119 }
120 } else {
121 check.errorf(lhs, InvalidSyntaxTree, "cannot declare %s", lhs)
122 obj = newVar(LocalVar, lhs.Pos(), check.pkg, "_", nil)
123 }
124 assert(obj.typ == nil)
125
126
127 typ := rhs[i]
128 if typ == nil || typ == Typ[Invalid] {
129
130 obj.typ = Typ[Invalid]
131 check.usedVars[obj] = true
132 continue
133 }
134
135 if rangeOverInt {
136 assert(i == 0)
137 check.initVar(obj, &x, "range clause")
138 } else {
139 var y operand
140 y.mode = value
141 y.expr = lhs
142 y.typ = typ
143 check.initVar(obj, &y, "assignment")
144 }
145 assert(obj.typ != nil)
146 }
147
148
149 if len(vars) > 0 {
150 scopePos := rangeStmt.Body.Pos()
151 for _, obj := range vars {
152 check.declare(check.scope, nil , obj, scopePos)
153 }
154 } else {
155 check.error(noNewVarPos, NoNewVar, "no new variables on left side of :=")
156 }
157 } else if sKey != nil {
158
159 for i, lhs := range lhs {
160 if lhs == nil {
161 continue
162 }
163
164
165 typ := rhs[i]
166 if typ == nil || typ == Typ[Invalid] {
167 continue
168 }
169
170 if rangeOverInt {
171 assert(i == 0)
172 check.assignVar(lhs, nil, &x, "range clause")
173
174
175
176 if x.mode != invalid && !isInteger(x.typ) {
177 check.softErrorf(lhs, InvalidRangeExpr, "cannot use iteration variable of type %s", x.typ)
178 }
179 } else {
180 var y operand
181 y.mode = value
182 y.expr = lhs
183 y.typ = typ
184 check.assignVar(lhs, nil, &y, "assignment")
185 }
186 }
187 } else if rangeOverInt {
188
189
190
191
192
193
194 check.assignment(&x, nil, "range clause")
195 }
196
197 check.stmt(inner, rangeStmt.Body)
198 }
199
200
201
202
203
204
205
206 func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (key, val Type, cause string, ok bool) {
207 bad := func(cause string) (Type, Type, string, bool) {
208 return Typ[Invalid], Typ[Invalid], cause, false
209 }
210
211 rtyp, err := commonUnder(orig, func(t, u Type) *typeError {
212
213 if ch, _ := u.(*Chan); ch != nil && ch.dir == SendOnly {
214 return typeErrorf("receive from send-only channel %s", t)
215 }
216 return nil
217 })
218 if rtyp == nil {
219 return bad(err.format(check))
220 }
221
222 switch typ := arrayPtrDeref(rtyp).(type) {
223 case *Basic:
224 if isString(typ) {
225 return Typ[Int], universeRune, "", true
226 }
227 if isInteger(typ) {
228 if allowVersion != nil && !allowVersion(go1_22) {
229 return bad("requires go1.22 or later")
230 }
231 return orig, nil, "", true
232 }
233 case *Array:
234 return Typ[Int], typ.elem, "", true
235 case *Slice:
236 return Typ[Int], typ.elem, "", true
237 case *Map:
238 return typ.key, typ.elem, "", true
239 case *Chan:
240 assert(typ.dir != SendOnly)
241 return typ.elem, nil, "", true
242 case *Signature:
243 if !buildcfg.Experiment.RangeFunc && allowVersion != nil && !allowVersion(go1_23) {
244 return bad("requires go1.23 or later")
245 }
246
247 switch {
248 case typ.Params().Len() != 1:
249 return bad("func must be func(yield func(...) bool): wrong argument count")
250 case typ.Results().Len() != 0:
251 return bad("func must be func(yield func(...) bool): unexpected results")
252 }
253 assert(typ.Recv() == nil)
254
255 u, err := commonUnder(typ.Params().At(0).Type(), nil)
256 cb, _ := u.(*Signature)
257 switch {
258 case cb == nil:
259 if err != nil {
260 return bad(check.sprintf("func must be func(yield func(...) bool): in yield type, %s", err.format(check)))
261 } else {
262 return bad("func must be func(yield func(...) bool): argument is not func")
263 }
264 case cb.Params().Len() > 2:
265 return bad("func must be func(yield func(...) bool): yield func has too many parameters")
266 case cb.Results().Len() != 1 || !Identical(cb.Results().At(0).Type(), universeBool):
267
268 if cb.Results().Len() == 1 && isBoolean(cb.Results().At(0).Type()) {
269 return bad("func must be func(yield func(...) bool): yield func returns user-defined boolean, not bool")
270 } else {
271 return bad("func must be func(yield func(...) bool): yield func does not return bool")
272 }
273 }
274 assert(cb.Recv() == nil)
275
276 if cb.Params().Len() >= 1 {
277 key = cb.Params().At(0).Type()
278 }
279 if cb.Params().Len() >= 2 {
280 val = cb.Params().At(1).Type()
281 }
282 return key, val, "", true
283 }
284 return
285 }
286
View as plain text