diff options
| author | Rob Findley <rfindley@anthropic.com> | 2026-04-12 22:27:14 +0000 |
|---|---|---|
| committer | Robert Findley <rfindley@anthropic.com> | 2026-04-14 07:49:47 -0700 |
| commit | 66e8a77e30babe052c023320d85af4c126413da8 (patch) | |
| tree | c82ab9f27b8eefd7e1438a2c76d48832307c9faa | |
| parent | 3a528855af73ff1e3bd7f5ac8e9374e27f3144f8 (diff) | |
| download | go-66e8a77e30babe052c023320d85af4c126413da8.tar.xz | |
go/types: fix inNode debug assertion for "for range x {}"
For a RangeStmt with no range variables, s.TokPos is invalid (NoPos).
The inNode debug assertion (start <= pos && pos < end) fails on the
zero pos. The position is unused in that case (noNewVarPos is only
read when isDef is true), but inNode still asserts.
Use s.For as a fallback so the assertion holds. types2 is unaffected
because syntax.ForStmt does not have this shape.
Found while running go/types tests with debug=true.
Change-Id: I82ebffca98bc9f41cda4364d31d0845bac97b5ae
Reviewed-on: https://go-review.googlesource.com/c/go/+/766181
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
| -rw-r--r-- | src/go/types/stmt.go | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/src/go/types/stmt.go b/src/go/types/stmt.go index e856c5e712..fafeed95ce 100644 --- a/src/go/types/stmt.go +++ b/src/go/types/stmt.go @@ -855,7 +855,13 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) { case *ast.RangeStmt: inner |= breakOk | continueOk - check.rangeStmt(inner, s, inNode(s, s.TokPos), s.Key, s.Value, nil, s.X, s.Tok == token.DEFINE) + // s.TokPos is invalid when there are no range variables (for range x {}); + // noNewVarPos is unused in that case, but inNode asserts a valid pos. + tokPos := s.TokPos + if !tokPos.IsValid() { + tokPos = s.For + } + check.rangeStmt(inner, s, inNode(s, tokPos), s.Key, s.Value, nil, s.X, s.Tok == token.DEFINE) default: check.error(s, InvalidSyntaxTree, "invalid statement") |
