Skip to content

Commit 8cd75f3

Browse files
committed
token: more descriptive panics
Currently, there are several panics in token that simply say "illegal!". This CL adds the values. This is valuable when the token call is wrapped under several layers and you can't easily see which value is being passed to token. Change-Id: Ib04b55cafcd9b9ec6820dcf416fc4d49afaea15f Reviewed-on: https://go-review.googlesource.com/c/go/+/262017 Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Trust: Robert Griesemer <gri@golang.org> Trust: Jean de Klerk <deklerk@google.com> TryBot-Result: Go Bot <gobot@golang.org>
1 parent e4ec309 commit 8cd75f3

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/go/token/position.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ func (f *File) AddLine(offset int) {
150150
//
151151
func (f *File) MergeLine(line int) {
152152
if line < 1 {
153-
panic("illegal line number (line numbering starts at 1)")
153+
panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line))
154154
}
155155
f.mutex.Lock()
156156
defer f.mutex.Unlock()
157157
if line >= len(f.lines) {
158-
panic("illegal line number")
158+
panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines)))
159159
}
160160
// To merge the line numbered <line> with the line numbered <line+1>,
161161
// we need to remove the entry in lines corresponding to the line
@@ -217,12 +217,12 @@ func (f *File) SetLinesForContent(content []byte) {
217217
// LineStart panics if the 1-based line number is invalid.
218218
func (f *File) LineStart(line int) Pos {
219219
if line < 1 {
220-
panic("illegal line number (line numbering starts at 1)")
220+
panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line))
221221
}
222222
f.mutex.Lock()
223223
defer f.mutex.Unlock()
224224
if line > len(f.lines) {
225-
panic("illegal line number")
225+
panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines)))
226226
}
227227
return Pos(f.base + f.lines[line-1])
228228
}
@@ -267,7 +267,7 @@ func (f *File) AddLineColumnInfo(offset int, filename string, line, column int)
267267
//
268268
func (f *File) Pos(offset int) Pos {
269269
if offset > f.size {
270-
panic("illegal file offset")
270+
panic(fmt.Sprintf("invalid file offset %d (should be <= %d)", offset, f.size))
271271
}
272272
return Pos(f.base + offset)
273273
}
@@ -278,7 +278,7 @@ func (f *File) Pos(offset int) Pos {
278278
//
279279
func (f *File) Offset(p Pos) int {
280280
if int(p) < f.base || int(p) > f.base+f.size {
281-
panic("illegal Pos value")
281+
panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d[)", p, f.base, f.base+f.size))
282282
}
283283
return int(p) - f.base
284284
}
@@ -346,7 +346,7 @@ func (f *File) position(p Pos, adjusted bool) (pos Position) {
346346
func (f *File) PositionFor(p Pos, adjusted bool) (pos Position) {
347347
if p != NoPos {
348348
if int(p) < f.base || int(p) > f.base+f.size {
349-
panic("illegal Pos value")
349+
panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d[)", p, f.base, f.base+f.size))
350350
}
351351
pos = f.position(p, adjusted)
352352
}
@@ -430,8 +430,11 @@ func (s *FileSet) AddFile(filename string, base, size int) *File {
430430
if base < 0 {
431431
base = s.base
432432
}
433-
if base < s.base || size < 0 {
434-
panic("illegal base or size")
433+
if base < s.base {
434+
panic(fmt.Sprintf("invalid base %d (should be >= %d)", base, s.base))
435+
}
436+
if size < 0 {
437+
panic(fmt.Sprintf("invalid size %d (should be >= 0)", size))
435438
}
436439
// base >= s.base && size >= 0
437440
f := &File{set: s, name: filename, base: base, size: size, lines: []int{0}}

0 commit comments

Comments
 (0)