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

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

View as plain text