From: Robert Griesemer Date: Thu, 14 May 2015 18:20:08 +0000 (-0700) Subject: go/scanner: don't return previous comment as literal value if none is expected X-Git-Tag: go1.5beta1~594 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d2130573e871dc7437b14713c2a7107a20bb0390;p=gostls13.git go/scanner: don't return previous comment as literal value if none is expected Fixes #10213. Change-Id: Ia587dd51eea702058da926717ad305792c9fc42b Reviewed-on: https://go-review.googlesource.com/10081 Reviewed-by: Alan Donovan --- diff --git a/src/go/scanner/scanner.go b/src/go/scanner/scanner.go index cec82ea10e..e9476c4dee 100644 --- a/src/go/scanner/scanner.go +++ b/src/go/scanner/scanner.go @@ -706,13 +706,14 @@ scanAgain: s.insertSemi = false // newline consumed return pos, token.SEMICOLON, "\n" } - lit = s.scanComment() + comment := s.scanComment() if s.mode&ScanComments == 0 { // skip comment s.insertSemi = false // newline consumed goto scanAgain } tok = token.COMMENT + lit = comment } else { tok = s.switch2(token.QUO, token.QUO_ASSIGN) } diff --git a/src/go/scanner/scanner_test.go b/src/go/scanner/scanner_test.go index fc450d8a6e..0d21905166 100644 --- a/src/go/scanner/scanner_test.go +++ b/src/go/scanner/scanner_test.go @@ -734,6 +734,41 @@ func TestScanErrors(t *testing.T) { } } +// Verify that no comments show up as literal values when skipping comments. +func TestIssue10213(t *testing.T) { + var src = ` + var ( + A = 1 // foo + ) + + var ( + B = 2 + // foo + ) + + var C = 3 // foo + + var D = 4 + // foo + + func anycode() { + // foo + } + ` + var s Scanner + s.Init(fset.AddFile("", fset.Base(), len(src)), []byte(src), nil, 0) + for { + pos, tok, lit := s.Scan() + class := tokenclass(tok) + if lit != "" && class != keyword && class != literal && tok != token.SEMICOLON { + t.Errorf("%s: tok = %s, lit = %q", fset.Position(pos), tok, lit) + } + if tok <= token.EOF { + break + } + } +} + func BenchmarkScan(b *testing.B) { b.StopTimer() fset := token.NewFileSet()