Debian Codegraph @

src/go/types/range.go

raw
1 // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
2 // Source: ../../cmd/compile/internal/types2/range.go
3
4 // Copyright 2025 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 // This file implements typechecking of range statements.
9
10 package types
11
12 import (
13 "go/ast"
14 "go/constant"
15 . "internal/types/errors"
16 )
17
18 // rangeStmt type-checks a range statement of form
19 //
20 // for sKey, sValue = range rangeVar { ... }
21 //
22 // where sKey, sValue, sExtra may be nil. isDef indicates whether these
23 // variables are assigned to only (=) or whether there is a short variable
24 // declaration (:=). If the latter and there are no variables, an error is
25 // reported at noNewVarPos.
26 func (check *Checker) rangeStmt(inner stmtContext, rangeStmt *ast.RangeStmt, noNewVarPos positioner, sKey, sValue, sExtra, rangeVar ast.Expr, isDef bool) {
27 // check expression to iterate over
28 var x operand
29
30 // From the spec:
31 // The range expression x is evaluated before beginning the loop,
32 // with one exception: if at most one iteration variable is present
33 // and x or len(x) is constant, the range expression is not evaluated.
34 // So we have to be careful not to evaluate the arg in the
35 // described situation.
36
37 check.hasCallOrRecv = false
38 check.expr(nil, &x, rangeVar)
39
40 if isTypes2 && x.mode != invalid && sValue == nil && !check.hasCallOrRecv {
41 if t, ok := arrayPtrDeref(x.typ.Underlying()).(*Array); ok {
42 for {
43 // Put constant info on the thing inside parentheses.
44 // That's where (*../noder/writer).expr expects it.
45 // See issue 73476.
46 p, ok := rangeVar.(*ast.ParenExpr)
47 if !ok {
48 break
49 }
50 rangeVar = p.X
51 }
52 // Override type of rangeVar to be a constant
53 // (and thus side-effects will not be computed
54 // by the backend).
55 check.record(&operand{
56 mode: constant_,
57 expr: rangeVar,
58 typ: Typ[Int],
59 val: constant.MakeInt64(t.len),
60 id: x.id,
61 })
62 }
63 }
64
65 // determine key/value types
66 var key, val Type
67 if x.mode != invalid {
68 k, v, cause, ok := rangeKeyVal(check, x.typ, func(v goVersion) bool {
69 return check.allowVersion(v)
70 })
71 switch {
72 case !ok && cause != "":
73 check.softErrorf(&x, InvalidRangeExpr, "cannot range over %s: %s", &x, cause)
74 case !ok:
75 check.softErrorf(&x, InvalidRangeExpr, "cannot range over %s", &x)
76 case k == nil && sKey != nil:
77 check.softErrorf(sKey, InvalidIterVar, "range over %s permits no iteration variables", &x)
78 case v == nil && sValue != nil:
79 check.softErrorf(sValue, InvalidIterVar, "range over %s permits only one iteration variable", &x)
80 case sExtra != nil:
81 check.softErrorf(sExtra, InvalidIterVar, "range clause permits at most two iteration variables")
82 }
83 key, val = k, v
84 }
85
86 // Open the for-statement block scope now, after the range clause.
87 // Iteration variables declared with := need to go in this scope (was go.dev/issue/51437).
88 check.openScope(rangeStmt, "range")
89 defer check.closeScope()
90
91 // check assignment to/declaration of iteration variables
92 // (irregular assignment, cannot easily map to existing assignment checks)
93
94 // lhs expressions and initialization value (rhs) types
95 lhs := [2]ast.Expr{sKey, sValue} // sKey, sValue may be nil
96 rhs := [2]Type{key, val} // key, val may be nil
97
98 rangeOverInt := isInteger(x.typ)
99
100 if isDef {
101 // short variable declaration
102 var vars []*Var
103 for i, lhs := range lhs {
104 if lhs == nil {
105 continue
106 }
107
108 // determine lhs variable
109 var obj *Var
110 if ident, _ := lhs.(*ast.Ident); ident != nil {
111 // declare new variable
112 name := ident.Name
113 obj = newVar(LocalVar, ident.Pos(), check.pkg, name, nil)
114 check.recordDef(ident, obj)
115 // _ variables don't count as new variables
116 if name != "_" {
117 vars = append(vars, obj)
118 }
119 } else {
120 check.errorf(lhs, InvalidSyntaxTree, "cannot declare %s", lhs)
121 obj = newVar(LocalVar, lhs.Pos(), check.pkg, "_", nil) // dummy variable
122 }
123 assert(obj.typ == nil)
124
125 // initialize lhs iteration variable, if any
126 typ := rhs[i]
127 if typ == nil || typ == Typ[Invalid] {
128 // typ == Typ[Invalid] can happen if allowVersion fails.
129 obj.typ = Typ[Invalid]
130 check.usedVars[obj] = true // don't complain about unused variable
131 continue
132 }
133
134 if rangeOverInt {
135 assert(i == 0) // at most one iteration variable (rhs[1] == nil or Typ[Invalid] for rangeOverInt)
136 check.initVar(obj, &x, "range clause")
137 } else {
138 var y operand
139 y.mode = value
140 y.expr = lhs // we don't have a better rhs expression to use here
141 y.typ = typ
142 check.initVar(obj, &y, "assignment") // error is on variable, use "assignment" not "range clause"
143 }
144 assert(obj.typ != nil)
145 }
146
147 // declare variables
148 if len(vars) > 0 {
149 scopePos := rangeStmt.Body.Pos()
150 for _, obj := range vars {
151 check.declare(check.scope, nil /* recordDef already called */, obj, scopePos)
152 }
153 } else {
154 check.error(noNewVarPos, NoNewVar, "no new variables on left side of :=")
155 }
156 } else if sKey != nil /* lhs[0] != nil */ {
157 // ordinary assignment
158 for i, lhs := range lhs {
159 if lhs == nil {
160 continue
161 }
162
163 // assign to lhs iteration variable, if any
164 typ := rhs[i]
165 if typ == nil || typ == Typ[Invalid] {
166 continue
167 }
168
169 if rangeOverInt {
170 assert(i == 0) // at most one iteration variable (rhs[1] == nil or Typ[Invalid] for rangeOverInt)
171 check.assignVar(lhs, nil, &x, "range clause")
172 // If the assignment succeeded, if x was untyped before, it now
173 // has a type inferred via the assignment. It must be an integer.
174 // (go.dev/issues/67027)
175 if x.mode != invalid && !isInteger(x.typ) {
176 check.softErrorf(lhs, InvalidRangeExpr, "cannot use iteration variable of type %s", x.typ)
177 }
178 } else {
179 var y operand
180 y.mode = value
181 y.expr = lhs // we don't have a better rhs expression to use here
182 y.typ = typ
183 check.assignVar(lhs, nil, &y, "assignment") // error is on variable, use "assignment" not "range clause"
184 }
185 }
186 } else if rangeOverInt {
187 // If we don't have any iteration variables, we still need to
188 // check that a (possibly untyped) integer range expression x
189 // is valid.
190 // We do this by checking the assignment _ = x. This ensures
191 // that an untyped x can be converted to a value of its default
192 // type (rune or int).
193 check.assignment(&x, nil, "range clause")
194 }
195
196 check.stmt(inner, rangeStmt.Body)
197 }
198
199 // rangeKeyVal returns the key and value type produced by a range clause
200 // over an expression of type orig.
201 // If allowVersion != nil, it is used to check the required language version.
202 // If the range clause is not permitted, rangeKeyVal returns ok = false.
203 // When ok = false, rangeKeyVal may also return a reason in cause.
204 // The check parameter is only used in case of an error; it may be nil.
205 func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (key, val Type, cause string, ok bool) {
206 bad := func(cause string) (Type, Type, string, bool) {
207 return Typ[Invalid], Typ[Invalid], cause, false
208 }
209
210 rtyp, err := commonUnder(orig, func(t, u Type) *typeError {
211 // A channel must permit receive operations.
212 if ch, _ := u.(*Chan); ch != nil && ch.dir == SendOnly {
213 return typeErrorf("receive from send-only channel %s", t)
214 }
215 return nil
216 })
217 if rtyp == nil {
218 return bad(err.format(check))
219 }
220
221 switch typ := arrayPtrDeref(rtyp).(type) {
222 case *Basic:
223 if isString(typ) {
224 return Typ[Int], universeRune, "", true // use 'rune' name
225 }
226 if isInteger(typ) {
227 if allowVersion != nil && !allowVersion(go1_22) {
228 return bad("requires go1.22 or later")
229 }
230 return orig, nil, "", true
231 }
232 case *Array:
233 return Typ[Int], typ.elem, "", true
234 case *Slice:
235 return Typ[Int], typ.elem, "", true
236 case *Map:
237 return typ.key, typ.elem, "", true
238 case *Chan:
239 assert(typ.dir != SendOnly)
240 return typ.elem, nil, "", true
241 case *Signature:
242 if allowVersion != nil && !allowVersion(go1_23) {
243 return bad("requires go1.23 or later")
244 }
245 // check iterator arity
246 switch {
247 case typ.Params().Len() != 1:
248 return bad("func must be func(yield func(...) bool): wrong argument count")
249 case typ.Results().Len() != 0:
250 return bad("func must be func(yield func(...) bool): unexpected results")
251 }
252 assert(typ.Recv() == nil)
253 // check iterator argument type
254 u, err := commonUnder(typ.Params().At(0).Type(), nil)
255 cb, _ := u.(*Signature)
256 switch {
257 case cb == nil:
258 if err != nil {
259 return bad(check.sprintf("func must be func(yield func(...) bool): in yield type, %s", err.format(check)))
260 } else {
261 return bad("func must be func(yield func(...) bool): argument is not func")
262 }
263 case cb.Params().Len() > 2:
264 return bad("func must be func(yield func(...) bool): yield func has too many parameters")
265 case cb.Results().Len() != 1 || !Identical(cb.Results().At(0).Type(), universeBool):
266 // see go.dev/issues/71131, go.dev/issues/71164
267 if cb.Results().Len() == 1 && isBoolean(cb.Results().At(0).Type()) {
268 return bad("func must be func(yield func(...) bool): yield func returns user-defined boolean, not bool")
269 } else {
270 return bad("func must be func(yield func(...) bool): yield func does not return bool")
271 }
272 }
273 assert(cb.Recv() == nil)
274 // determine key and value types, if any
275 if cb.Params().Len() >= 1 {
276 key = cb.Params().At(0).Type()
277 }
278 if cb.Params().Len() >= 2 {
279 val = cb.Params().At(1).Type()
280 }
281 return key, val, "", true
282 }
283 return
284 }