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

cmd/compile: print better error message when assigning to keywords #16521

Open
dsnet opened this issue Jul 28, 2016 · 1 comment
Open

cmd/compile: print better error message when assigning to keywords #16521

dsnet opened this issue Jul 28, 2016 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Jul 28, 2016

I saw a new programmer struggle with the following:

func main() {
    map := "f"
    fmt.Println(map)
}

which is an invalid program since map is a keyword.

However, the error being printed is currently:

prog.go:6: syntax error: unexpected :=, expecting [
prog.go:7: syntax error: unexpected ), expecting [
prog.go:8: syntax error: unexpected }, expecting ]

It should really print a more helpful error message:

prog.go:6: identifier cannot be a keyword: map

/cc @griesemer

@dsnet dsnet added this to the Unplanned milestone Jul 28, 2016
@griesemer
Copy link
Contributor

Doing this well requires some care and is not trivial - it may not be worth the effort. For instance, if the code would have been

func main() {
   for := "f"; g(); {}
   // ...
}

it's not even clear what the error should be: When the compiler sees the for it assumes a for loop, and the error might be that there's a missing identifier after the for. Or it might be that for was taken as an identifier.

Given that Go has only 25 identifiers, it's not unreasonable to assume that Go programmers actually know them all.

Leaving open for now, in case somebody wants to play with this.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants