go/scanner: clear all fields after Scanner reuse

We were missing s.nlPos = token.NoPos in Init, but while we are here
let's make it less likely to hit this it in future.

Change-Id: Ief4c0ba2cf97bc556d901eabc8e172406a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/738680
Reviewed-by: Carlos Amedee <carlos@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Mateusz Poliwczak
2026-01-23 09:52:03 +01:00
committed by Alan Donovan
parent 1bd5dbfc41
commit f809faeb8e
2 changed files with 37 additions and 11 deletions

View File

@@ -144,18 +144,18 @@ func (s *Scanner) Init(file *token.File, src []byte, err ErrorHandler, mode Mode
if file.Size() != len(src) {
panic(fmt.Sprintf("file size (%d) does not match src len (%d)", file.Size(), len(src)))
}
s.file = file
s.dir, _ = filepath.Split(file.Name())
s.src = src
s.err = err
s.mode = mode
s.ch = ' '
s.offset = 0
s.rdOffset = 0
s.lineOffset = 0
s.insertSemi = false
s.ErrorCount = 0
dir, _ := filepath.Split(file.Name())
*s = Scanner{
file: file,
dir: dir,
src: src,
err: err,
mode: mode,
ch: ' ',
}
s.next()
if s.ch == bom {

View File

@@ -1153,3 +1153,29 @@ func TestNumbers(t *testing.T) {
}
}
}
func TestScanReuseSemiInNewlineComment(t *testing.T) {
fset := token.NewFileSet()
const src = "identifier /*a\nb*/ + other"
var s Scanner
s.Init(fset.AddFile("test.go", -1, len(src)), []byte(src), func(pos token.Position, msg string) {
t.Fatal(msg)
}, ScanComments)
s.Scan() // IDENT(identifier)
_, tok, _ := s.Scan() // COMMENT(/*a\nb*/)
if tok != token.COMMENT {
t.Fatalf("tok = %v; want = token.SEMICOLON", tok)
}
s.Init(fset.AddFile("test.go", -1, len(src)), []byte(src), func(pos token.Position, msg string) {
t.Fatal(msg)
}, ScanComments)
_, tok, _ = s.Scan()
if tok != token.IDENT {
t.Fatalf("tok = %v; want = token.IDENT", tok)
}
}