]> Cypherpunks repositories - gostls13.git/commitdiff
go/token: add (*File).End method
authorAlan Donovan <adonovan@google.com>
Wed, 12 Nov 2025 23:17:35 +0000 (18:17 -0500)
committerAlan Donovan <adonovan@google.com>
Mon, 17 Nov 2025 17:47:47 +0000 (09:47 -0800)
Also, use it in a number of places.

+ test, api, relnote

Fixes #75849

Change-Id: I44acf5b8190b964fd3975009aa407d7c82cee19b
Reviewed-on: https://go-review.googlesource.com/c/go/+/720061
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
api/next/75849.txt [new file with mode: 0644]
doc/next/6-stdlib/99-minor/go/token/75849.md [new file with mode: 0644]
src/go/ast/issues_test.go
src/go/parser/interface.go
src/go/token/position.go
src/go/token/position_test.go
src/go/token/tree.go
src/go/types/resolver.go

diff --git a/api/next/75849.txt b/api/next/75849.txt
new file mode 100644 (file)
index 0000000..615d615
--- /dev/null
@@ -0,0 +1 @@
+pkg go/token, method (*File) End() Pos #75849
diff --git a/doc/next/6-stdlib/99-minor/go/token/75849.md b/doc/next/6-stdlib/99-minor/go/token/75849.md
new file mode 100644 (file)
index 0000000..4b8a79f
--- /dev/null
@@ -0,0 +1 @@
+The new [File.End] convenience method returns the file's end position.
index 28d6a30fbb328f579a1740ed698990b2ff473997..f5e26af462f55a60463736fad0be987406a876ec 100644 (file)
@@ -30,10 +30,10 @@ func TestIssue33649(t *testing.T) {
                        tf = f
                        return true
                })
-               tfEnd := tf.Base() + tf.Size()
+               tfEnd := tf.End()
 
                fd := f.Decls[len(f.Decls)-1].(*ast.FuncDecl)
-               fdEnd := int(fd.End())
+               fdEnd := fd.End()
 
                if fdEnd != tfEnd {
                        t.Errorf("%q: got fdEnd = %d; want %d (base = %d, size = %d)", src, fdEnd, tfEnd, tf.Base(), tf.Size())
index a9a1cfb736aa9af05866067971429437bddc34eb..ec9cb469edaefa8ca3d066cb3446dbbccd082425 100644 (file)
@@ -120,7 +120,7 @@ func ParseFile(fset *token.FileSet, filename string, src any, mode Mode) (f *ast
                // Ensure the start/end are consistent,
                // whether parsing succeeded or not.
                f.FileStart = token.Pos(file.Base())
-               f.FileEnd = token.Pos(file.Base() + file.Size())
+               f.FileEnd = file.End()
 
                p.errors.Sort()
                err = p.errors.Err()
index 39756f257d3b4becc93900bb0f5872d56d71029d..37017d43742b9414b6411c212cfff84913197419 100644 (file)
@@ -127,6 +127,11 @@ func (f *File) Size() int {
        return f.size
 }
 
+// End returns the end position of file f as registered with AddFile.
+func (f *File) End() Pos {
+       return Pos(f.base + f.size)
+}
+
 // LineCount returns the number of lines in file f.
 func (f *File) LineCount() int {
        f.mutex.Lock()
index c588a34d3df1e12398bf47a11ae484e06399290b..3d02068ebf46ca853cf7238dd3ff061efbb371e1 100644 (file)
@@ -572,7 +572,7 @@ func fsetString(fset *FileSet) string {
        buf.WriteRune('{')
        sep := ""
        fset.Iterate(func(f *File) bool {
-               fmt.Fprintf(&buf, "%s%s:%d-%d", sep, f.Name(), f.Base(), f.Base()+f.Size())
+               fmt.Fprintf(&buf, "%s%s:%d-%d", sep, f.Name(), f.Base(), f.End())
                sep = " "
                return true
        })
@@ -643,3 +643,11 @@ func TestRemovedFileFileReturnsNil(t *testing.T) {
                }
        }
 }
+
+func TestFile_End(t *testing.T) {
+       f := NewFileSet().AddFile("a.go", 100, 42)
+       got := fmt.Sprintf("%d, %d", f.Base(), f.End())
+       if want := "100, 142"; got != want {
+               t.Errorf("Base, End = %s, want %s", got, want)
+       }
+}
index 7ba927c6060e194bfda3fe313056035b5e367c54..b2ca09f2c089918652a3582ccb87142f1d5a29e9 100644 (file)
@@ -313,8 +313,8 @@ func (t *tree) add(file *File) {
        }
        if prev := (*pos).file; prev != file {
                panic(fmt.Sprintf("file %s (%d-%d) overlaps with file %s (%d-%d)",
-                       prev.Name(), prev.Base(), prev.Base()+prev.Size(),
-                       file.Name(), file.Base(), file.Base()+file.Size()))
+                       prev.Name(), prev.Base(), prev.End(),
+                       file.Name(), file.Base(), file.End()))
        }
 }
 
index a8d11c2aa52b9da382425e66b44409d35a266bff..2017b9c881379bf3e546d4d68d6b440a19c7c888 100644 (file)
@@ -247,7 +247,7 @@ func (check *Checker) collectObjects() {
                // Be conservative and use the *ast.File extent if we don't have a *token.File.
                pos, end := file.Pos(), file.End()
                if f := check.fset.File(file.Pos()); f != nil {
-                       pos, end = token.Pos(f.Base()), token.Pos(f.Base()+f.Size())
+                       pos, end = token.Pos(f.Base()), f.End()
                }
                fileScope := NewScope(pkg.scope, pos, end, check.filename(fileNo))
                fileScopes[fileNo] = fileScope