From d7bd7773ebe0ffe7d7fae92acb77e10ad0539dd3 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Mon, 4 Aug 2025 17:25:37 +0200 Subject: [PATCH] go/parser: remove safePos The logic in safePos is wrong, since (*token.File).Offset does not panic, so this function was basically a noop (since CL 559436). To work properly it would have to be: return p.file.Pos(p.file.Offset(pos)) Since it effectively acts as a no-op and hasn't been noticed since, let's go ahead and remove it. Change-Id: I00a1bcc5af6a996c63de3f1175c15062e85cf89b Reviewed-on: https://go-review.googlesource.com/c/go/+/692955 LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan Reviewed-by: Robert Findley --- src/go/parser/parser.go | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go index 8a2f95976f..9ee1576a99 100644 --- a/src/go/parser/parser.go +++ b/src/go/parser/parser.go @@ -455,25 +455,6 @@ var exprEnd = map[token.Token]bool{ token.RBRACE: true, } -// safePos returns a valid file position for a given position: If pos -// is valid to begin with, safePos returns pos. If pos is out-of-range, -// safePos returns the EOF position. -// -// This is hack to work around "artificial" end positions in the AST which -// are computed by adding 1 to (presumably valid) token positions. If the -// token positions are invalid due to parse errors, the resulting end position -// may be past the file's EOF position, which would lead to panics if used -// later on. -func (p *parser) safePos(pos token.Pos) (res token.Pos) { - defer func() { - if recover() != nil { - res = token.Pos(p.file.Base() + p.file.Size()) // EOF position - } - }() - _ = p.file.Offset(pos) // trigger a panic if position is out-of-range - return pos -} - // ---------------------------------------------------------------------------- // Identifiers @@ -2022,7 +2003,7 @@ func (p *parser) parseCallExpr(callType string) *ast.CallExpr { } if _, isBad := x.(*ast.BadExpr); !isBad { // only report error if it's a new one - p.error(p.safePos(x.End()), fmt.Sprintf("expression in %s must be function call", callType)) + p.error(x.End(), fmt.Sprintf("expression in %s must be function call", callType)) } return nil } @@ -2100,7 +2081,7 @@ func (p *parser) makeExpr(s ast.Stmt, want string) ast.Expr { found = "assignment" } p.error(s.Pos(), fmt.Sprintf("expected %s, found %s (missing parentheses around composite literal?)", want, found)) - return &ast.BadExpr{From: s.Pos(), To: p.safePos(s.End())} + return &ast.BadExpr{From: s.Pos(), To: s.End()} } // parseIfHeader is an adjusted version of parser.header @@ -2423,7 +2404,7 @@ func (p *parser) parseForStmt() ast.Stmt { key, value = as.Lhs[0], as.Lhs[1] default: p.errorExpected(as.Lhs[len(as.Lhs)-1].Pos(), "at most 2 expressions") - return &ast.BadStmt{From: pos, To: p.safePos(body.End())} + return &ast.BadStmt{From: pos, To: body.End()} } // parseSimpleStmt returned a right-hand side that // is a single unary expression of the form "range x" -- 2.51.0