Skip to content

Commit d7bd777

Browse files
mateusz834adonovan
authored andcommitted
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 <[email protected]> Commit-Queue: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 4b6cbc3 commit d7bd777

File tree

1 file changed

+3
-22
lines changed

1 file changed

+3
-22
lines changed

src/go/parser/parser.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -455,25 +455,6 @@ var exprEnd = map[token.Token]bool{
455455
token.RBRACE: true,
456456
}
457457

458-
// safePos returns a valid file position for a given position: If pos
459-
// is valid to begin with, safePos returns pos. If pos is out-of-range,
460-
// safePos returns the EOF position.
461-
//
462-
// This is hack to work around "artificial" end positions in the AST which
463-
// are computed by adding 1 to (presumably valid) token positions. If the
464-
// token positions are invalid due to parse errors, the resulting end position
465-
// may be past the file's EOF position, which would lead to panics if used
466-
// later on.
467-
func (p *parser) safePos(pos token.Pos) (res token.Pos) {
468-
defer func() {
469-
if recover() != nil {
470-
res = token.Pos(p.file.Base() + p.file.Size()) // EOF position
471-
}
472-
}()
473-
_ = p.file.Offset(pos) // trigger a panic if position is out-of-range
474-
return pos
475-
}
476-
477458
// ----------------------------------------------------------------------------
478459
// Identifiers
479460

@@ -2022,7 +2003,7 @@ func (p *parser) parseCallExpr(callType string) *ast.CallExpr {
20222003
}
20232004
if _, isBad := x.(*ast.BadExpr); !isBad {
20242005
// only report error if it's a new one
2025-
p.error(p.safePos(x.End()), fmt.Sprintf("expression in %s must be function call", callType))
2006+
p.error(x.End(), fmt.Sprintf("expression in %s must be function call", callType))
20262007
}
20272008
return nil
20282009
}
@@ -2100,7 +2081,7 @@ func (p *parser) makeExpr(s ast.Stmt, want string) ast.Expr {
21002081
found = "assignment"
21012082
}
21022083
p.error(s.Pos(), fmt.Sprintf("expected %s, found %s (missing parentheses around composite literal?)", want, found))
2103-
return &ast.BadExpr{From: s.Pos(), To: p.safePos(s.End())}
2084+
return &ast.BadExpr{From: s.Pos(), To: s.End()}
21042085
}
21052086

21062087
// parseIfHeader is an adjusted version of parser.header
@@ -2423,7 +2404,7 @@ func (p *parser) parseForStmt() ast.Stmt {
24232404
key, value = as.Lhs[0], as.Lhs[1]
24242405
default:
24252406
p.errorExpected(as.Lhs[len(as.Lhs)-1].Pos(), "at most 2 expressions")
2426-
return &ast.BadStmt{From: pos, To: p.safePos(body.End())}
2407+
return &ast.BadStmt{From: pos, To: body.End()}
24272408
}
24282409
// parseSimpleStmt returned a right-hand side that
24292410
// is a single unary expression of the form "range x"

0 commit comments

Comments
 (0)