From: Alan Donovan Date: Wed, 12 Nov 2025 23:17:35 +0000 (-0500) Subject: go/token: add (*File).End method X-Git-Tag: go1.26rc1~263 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1297fae7081a3116d2097ce7cfcc0f89ba2cf0fc;p=gostls13.git go/token: add (*File).End method 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 Reviewed-by: Robert Griesemer --- diff --git a/api/next/75849.txt b/api/next/75849.txt new file mode 100644 index 0000000000..615d6158f1 --- /dev/null +++ b/api/next/75849.txt @@ -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 index 0000000000..4b8a79ff9f --- /dev/null +++ b/doc/next/6-stdlib/99-minor/go/token/75849.md @@ -0,0 +1 @@ +The new [File.End] convenience method returns the file's end position. diff --git a/src/go/ast/issues_test.go b/src/go/ast/issues_test.go index 28d6a30fbb..f5e26af462 100644 --- a/src/go/ast/issues_test.go +++ b/src/go/ast/issues_test.go @@ -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()) diff --git a/src/go/parser/interface.go b/src/go/parser/interface.go index a9a1cfb736..ec9cb469ed 100644 --- a/src/go/parser/interface.go +++ b/src/go/parser/interface.go @@ -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() diff --git a/src/go/token/position.go b/src/go/token/position.go index 39756f257d..37017d4374 100644 --- a/src/go/token/position.go +++ b/src/go/token/position.go @@ -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() diff --git a/src/go/token/position_test.go b/src/go/token/position_test.go index c588a34d3d..3d02068ebf 100644 --- a/src/go/token/position_test.go +++ b/src/go/token/position_test.go @@ -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) + } +} diff --git a/src/go/token/tree.go b/src/go/token/tree.go index 7ba927c606..b2ca09f2c0 100644 --- a/src/go/token/tree.go +++ b/src/go/token/tree.go @@ -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())) } } diff --git a/src/go/types/resolver.go b/src/go/types/resolver.go index a8d11c2aa5..2017b9c881 100644 --- a/src/go/types/resolver.go +++ b/src/go/types/resolver.go @@ -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