Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/parser: files parsed with mode 'parser.ImportsOnly|parser.ParseComments' can contain a comment after import declaration #47778

Open
ShoshinNikita opened this issue Aug 18, 2021 · 4 comments
Labels
NeedsInvestigation
Milestone

Comments

@ShoshinNikita
Copy link

@ShoshinNikita ShoshinNikita commented Aug 18, 2021

What version of Go are you using (go version)?

$ go version
go version go1.17 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

  1. Parse the following source code with mode parser.ImportsOnly|parser.ParseComments:
// Package main
package main

import (
	// fmt import
	_ "fmt"
	_ "github.com/pkg/errors" // errors import
)

// F func
func F() {}

// B func
func B() {}

// Hello const
const Hello = "word"

func main() {
}
  1. Print *ast.File.Comments

The Go Playground link: https://play.golang.org/p/w0yK1K_q4tr

What did you expect to see?

3 comments:

"Package main\n"
"fmt import\n"
"errors import\n"

What did you see instead?

4 comments:

"Package main\n"
"fmt import\n"
"errors import\n"
"F func\n"

"F func\n" is an extra comment because the documentation for parser.ImportsOnly says stop parsing after import declarations. However, if an import declaration is followed by any code without comments, there will be no extra comments. For example:

// Package main
package main

import (
	_ "fmt" // fmt import
)

var A = 1

// F func
func F() {}

This bug was originally discovered in #47436 (comment)

@mknyszek
Copy link
Contributor

@mknyszek mknyszek commented Aug 18, 2021

@mknyszek mknyszek added the NeedsInvestigation label Aug 18, 2021
@mknyszek
Copy link
Contributor

@mknyszek mknyszek commented Aug 18, 2021

@ShoshinNikita is this new in 1.17? Putting in backlog for now assuming it isn't.

@mknyszek mknyszek added this to the Backlog milestone Aug 18, 2021
@ShoshinNikita
Copy link
Author

@ShoshinNikita ShoshinNikita commented Aug 18, 2021

@mknyszek I discovered this bug before 1.17 release

@gopherbot
Copy link

@gopherbot gopherbot commented Aug 18, 2021

Change https://golang.org/cl/343369 mentions this issue: go/parser: fix incorrect comment parsing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

3 participants