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: save error info when 'parseFile' occur invalid package declaration #63553

Closed
dugenkui03 opened this issue Oct 15, 2023 · 2 comments
Closed

Comments

@dugenkui03
Copy link

I notice that invalid import declaration will introduce an error element in parser.errors. https://github.com/golang/go/blob/master/src/go/parser/parser.go#L2846

image

I think it would be better if an invalid package name declaration appear.

  • invalid package name
image
  • error info
image
  • the pr code maybe like that
image
@snprajwal
Copy link

This particular error wouldn't belong in the import parsing section. Firstly, the error is raised on the line with the import, not on the line with the package declaration. Secondly, it is not necessary that the offending token is a package declaration. The below snippet just as well triggers this error:

package main

import "fmt"

func main() {}

import "net/http"

Regarding the snippet you have shared, it raises the error "non-declaration statement outside function body" on the line with the duplicate package declaration. That is not handled as a part of the import parsing, and is a completely different error.

@dugenkui03
Copy link
Author

@snprajwal I found that duplicate package declaration will also add an error element to parser.errors, the error info like blow:
image

The error you mentioned "non-declaration statement outside function body" seem like another tool:
https://github.com/golang/go/blob/master/src/cmd/compile/README.md#:~:text=the%20second%20phase.-,Note,-that%20the%20go

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

No branches or pull requests

2 participants