From 66e8a77e30babe052c023320d85af4c126413da8 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Sun, 12 Apr 2026 22:27:14 +0000 Subject: 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 LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com Reviewed-by: David Chase --- src/go/types/stmt.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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") -- cgit v1.3