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

Comments

Projects
None yet
2 participants
@dsnet
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

This comment has been minimized.

Contributor

griesemer commented Aug 11, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment