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>
--- /dev/null
+pkg go/token, method (*File) End() Pos #75849
--- /dev/null
+The new [File.End] convenience method returns the file's end position.
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())
// 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()
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()
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
})
}
}
}
+
+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)
+ }
+}
}
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()))
}
}
// 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