From d68628a908ad1692a8bf5bfc0d2fb7e6aebffa6a Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 2 Jun 2022 11:27:52 -0400 Subject: [PATCH] go/ast/astutil: clarify PathEnclosingInterval Change-Id: I06c044df8f243cc5b849e680692c7a5e60ea4b0f Reviewed-on: https://go-review.googlesource.com/c/tools/+/410135 Run-TryBot: Alan Donovan Reviewed-by: Robert Findley --- go/ast/astutil/enclosing.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/go/ast/astutil/enclosing.go b/go/ast/astutil/enclosing.go index 1d8c401939..9fa5aa192c 100644 --- a/go/ast/astutil/enclosing.go +++ b/go/ast/astutil/enclosing.go @@ -54,11 +54,11 @@ import ( // interior whitespace of the assignment. E is considered interior // whitespace of the BlockStmt containing the assignment. // -// Precondition: [start, end) both lie within the same file as root. -// TODO(adonovan): return (nil, false) in this case and remove precond. -// Requires FileSet; see loader.tokenFileContainsPos. -// -// Postcondition: path is never nil; it always contains at least 'root'. +// The resulting path is never empty; it always contains at least the +// 'root' *ast.File. Ideally PathEnclosingInterval would reject +// intervals that lie wholly or partially outside the range of the +// file, but unfortunately ast.File records only the token.Pos of +// the 'package' keyword, but not of the start of the file itself. func PathEnclosingInterval(root *ast.File, start, end token.Pos) (path []ast.Node, exact bool) { // fmt.Printf("EnclosingInterval %d %d\n", start, end) // debugging @@ -134,6 +134,7 @@ func PathEnclosingInterval(root *ast.File, start, end token.Pos) (path []ast.Nod return false // inexact: overlaps multiple children } + // Ensure [start,end) is nondecreasing. if start > end { start, end = end, start }