Debian Codegraph @

src/cmd/compile/internal/types2/range.go

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