diff options
author | Jean de Klerk <deklerk@google.com> | 2020-10-13 16:16:24 -0600 |
---|---|---|
committer | Jean de Klerk <deklerk@google.com> | 2020-10-14 22:35:32 +0000 |
commit | 8cd75f3da094931c59636b85a87b4f680a208799 (patch) | |
tree | a9eaa048c48bcac7bb394c5c2f74da984a58c33e /src/go | |
parent | e4ec30965b9ca629922e83b8d335224ae4bdf062 (diff) | |
download | go-8cd75f3da094931c59636b85a87b4f680a208799.tar.gz go-8cd75f3da094931c59636b85a87b4f680a208799.zip |
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>
Diffstat (limited to 'src/go')
-rw-r--r-- | src/go/token/position.go | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/go/token/position.go b/src/go/token/position.go index d0dbc2998f..a21f5fd056 100644 --- a/src/go/token/position.go +++ b/src/go/token/position.go @@ -150,12 +150,12 @@ func (f *File) AddLine(offset int) { // func (f *File) MergeLine(line int) { if line < 1 { - panic("illegal line number (line numbering starts at 1)") + panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line)) } f.mutex.Lock() defer f.mutex.Unlock() if line >= len(f.lines) { - panic("illegal line number") + panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines))) } // To merge the line numbered <line> with the line numbered <line+1>, // we need to remove the entry in lines corresponding to the line @@ -217,12 +217,12 @@ func (f *File) SetLinesForContent(content []byte) { // LineStart panics if the 1-based line number is invalid. func (f *File) LineStart(line int) Pos { if line < 1 { - panic("illegal line number (line numbering starts at 1)") + panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line)) } f.mutex.Lock() defer f.mutex.Unlock() if line > len(f.lines) { - panic("illegal line number") + panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines))) } return Pos(f.base + f.lines[line-1]) } @@ -267,7 +267,7 @@ func (f *File) AddLineColumnInfo(offset int, filename string, line, column int) // func (f *File) Pos(offset int) Pos { if offset > f.size { - panic("illegal file offset") + panic(fmt.Sprintf("invalid file offset %d (should be <= %d)", offset, f.size)) } return Pos(f.base + offset) } @@ -278,7 +278,7 @@ func (f *File) Pos(offset int) Pos { // func (f *File) Offset(p Pos) int { if int(p) < f.base || int(p) > f.base+f.size { - panic("illegal Pos value") + panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d[)", p, f.base, f.base+f.size)) } return int(p) - f.base } @@ -346,7 +346,7 @@ func (f *File) position(p Pos, adjusted bool) (pos Position) { func (f *File) PositionFor(p Pos, adjusted bool) (pos Position) { if p != NoPos { if int(p) < f.base || int(p) > f.base+f.size { - panic("illegal Pos value") + panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d[)", p, f.base, f.base+f.size)) } pos = f.position(p, adjusted) } @@ -430,8 +430,11 @@ func (s *FileSet) AddFile(filename string, base, size int) *File { if base < 0 { base = s.base } - if base < s.base || size < 0 { - panic("illegal base or size") + if base < s.base { + panic(fmt.Sprintf("invalid base %d (should be >= %d)", base, s.base)) + } + if size < 0 { + panic(fmt.Sprintf("invalid size %d (should be >= 0)", size)) } // base >= s.base && size >= 0 f := &File{set: s, name: filename, base: base, size: size, lines: []int{0}} |