Fixes #20636
Change-Id: Icea0012fecb73944c95f6037922505c63b57b245
Reviewed-on: https://go-review.googlesource.com/45295
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
// 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
}
}
}
+
+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")
+ }
+}