]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: remove safePos
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Mon, 4 Aug 2025 15:25:37 +0000 (17:25 +0200)
committerAlan Donovan <adonovan@google.com>
Mon, 4 Aug 2025 16:33:59 +0000 (09:33 -0700)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
src/go/parser/parser.go

index 8a2f95976fc390dea9d7e9d23da1c0c7c5978516..9ee1576a99e85dd3ab49c70ccde8611e745b0f3d 100644 (file)
@@ -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"