Commit 32543a8b authored by Hiroshi Ioka's avatar Hiroshi Ioka Committed by Robert Griesemer

go/parser: handle last line comments

Fixes #20636

Change-Id: Icea0012fecb73944c95f6037922505c63b57b245
Reviewed-on: https://go-review.googlesource.com/45295Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent b0d592c3
......@@ -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
......
......@@ -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")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment