From 32543a8bf7c74320acb6bb147bc17bf0dd7df9bb Mon Sep 17 00:00:00 2001 From: Hiroshi Ioka Date: Sat, 10 Jun 2017 10:36:28 +0900 Subject: [PATCH] go/parser: handle last line comments Fixes #20636 Change-Id: Icea0012fecb73944c95f6037922505c63b57b245 Reviewed-on: https://go-review.googlesource.com/45295 Reviewed-by: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gobot Gobot --- src/go/parser/parser.go | 2 +- src/go/parser/parser_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go index 40c4a3e58d..1b4309b5da 100644 --- a/src/go/parser/parser.go +++ b/src/go/parser/parser.go @@ -327,7 +327,7 @@ func (p *parser) next() { // The comment is on same line as the previous token; it // cannot be a lead comment but may be a line comment. comment, endline = p.consumeCommentGroup(0) - if p.file.Line(p.pos) != endline { + if p.file.Line(p.pos) != endline || p.tok == token.EOF { // The next token is on a different line, thus // the last comment group is a line comment. p.lineComment = comment diff --git a/src/go/parser/parser_test.go b/src/go/parser/parser_test.go index c7bb36d789..fb35a88ba1 100644 --- a/src/go/parser/parser_test.go +++ b/src/go/parser/parser_test.go @@ -531,3 +531,18 @@ func TestIncompleteSelection(t *testing.T) { } } } + +func TestLastLineComment(t *testing.T) { + const src = `package main +type x int // comment +` + fset := token.NewFileSet() + f, err := ParseFile(fset, "", src, ParseComments) + if err != nil { + t.Fatal(err) + } + comment := f.Decls[0].(*ast.GenDecl).Specs[0].(*ast.TypeSpec).Comment.List[0].Text + if comment != "// comment" { + t.Errorf("got %q, want %q", comment, "// comment") + } +} -- 2.50.0