]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: refactor Checker.rangeStmt for clarity
authorRobert Griesemer <gri@golang.org>
Thu, 25 Apr 2024 16:21:14 +0000 (09:21 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 25 Apr 2024 17:49:48 +0000 (17:49 +0000)
Change-Id: I0c2f921389416ab222b84f77699fd4b3246ef0e0
Reviewed-on: https://go-review.googlesource.com/c/go/+/581776
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/stmt.go
src/go/types/stmt.go

index 7fd7009e132515c5bd182967b7c98c6e409f7912..1984777008916b10f0e3f28a118af9eb4e6558b9 100644 (file)
@@ -923,19 +923,26 @@ func (check *Checker) rangeStmt(inner stmtContext, s *syntax.ForStmt, rclause *s
                                check.errorf(lhs, InvalidSyntaxTree, "cannot declare %s", lhs)
                                obj = NewVar(lhs.Pos(), check.pkg, "_", nil) // dummy variable
                        }
+                       assert(obj.typ == nil)
+
+                       // initialize lhs iteration variable, if any
+                       typ := rhs[i]
+                       if typ == nil {
+                               obj.typ = Typ[Invalid]
+                               obj.used = true // don't complain about unused variable
+                               continue
+                       }
 
                        // initialize lhs variable
                        if constIntRange {
                                check.initVar(obj, &x, "range clause")
-                       } else if typ := rhs[i]; typ != nil {
+                       } else {
                                x.mode = value
                                x.expr = lhs // we don't have a better rhs expression to use here
                                x.typ = typ
                                check.initVar(obj, &x, "assignment") // error is on variable, use "assignment" not "range clause"
-                       } else {
-                               obj.typ = Typ[Invalid]
-                               obj.used = true // don't complain about unused variable
                        }
+                       assert(obj.typ != nil)
                }
 
                // declare variables
@@ -954,9 +961,15 @@ func (check *Checker) rangeStmt(inner stmtContext, s *syntax.ForStmt, rclause *s
                                continue
                        }
 
+                       // assign to lhs iteration variable, if any
+                       typ := rhs[i]
+                       if typ == nil {
+                               continue
+                       }
+
                        if constIntRange {
                                check.assignVar(lhs, nil, &x, "range clause")
-                       } else if typ := rhs[i]; typ != nil {
+                       } else {
                                x.mode = value
                                x.expr = lhs // we don't have a better rhs expression to use here
                                x.typ = typ
index 30b49482160b276c338d5cfb76b27cdb06a10927..bfb51fd2e51aca0b823588ea8b73d2e2c6cca0b2 100644 (file)
@@ -923,19 +923,26 @@ func (check *Checker) rangeStmt(inner stmtContext, s *ast.RangeStmt) {
                                check.errorf(lhs, InvalidSyntaxTree, "cannot declare %s", lhs)
                                obj = NewVar(lhs.Pos(), check.pkg, "_", nil) // dummy variable
                        }
+                       assert(obj.typ == nil)
+
+                       // initialize lhs iteration variable, if any
+                       typ := rhs[i]
+                       if typ == nil {
+                               obj.typ = Typ[Invalid]
+                               obj.used = true // don't complain about unused variable
+                               continue
+                       }
 
                        // initialize lhs variable
                        if constIntRange {
                                check.initVar(obj, &x, "range clause")
-                       } else if typ := rhs[i]; typ != nil {
+                       } else {
                                x.mode = value
                                x.expr = lhs // we don't have a better rhs expression to use here
                                x.typ = typ
                                check.initVar(obj, &x, "assignment") // error is on variable, use "assignment" not "range clause"
-                       } else {
-                               obj.typ = Typ[Invalid]
-                               obj.used = true // don't complain about unused variable
                        }
+                       assert(obj.typ != nil)
                }
 
                // declare variables
@@ -954,9 +961,15 @@ func (check *Checker) rangeStmt(inner stmtContext, s *ast.RangeStmt) {
                                continue
                        }
 
+                       // assign to lhs iteration variable, if any
+                       typ := rhs[i]
+                       if typ == nil {
+                               continue
+                       }
+
                        if constIntRange {
                                check.assignVar(lhs, nil, &x, "range clause")
-                       } else if typ := rhs[i]; typ != nil {
+                       } else {
                                x.mode = value
                                x.expr = lhs // we don't have a better rhs expression to use here
                                x.typ = typ