golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.69k stars 17.62k forks source link

cmd/compile: more clever syntax error reporting #5129

Open btracey opened 11 years ago

btracey commented 11 years ago
Using the go 1.0.3 compiler, the code at http://play.golang.org/p/mSSyjMd_MU gives a
syntax error on line 15 that there is an unexpected ), while the actual error is that
there is a missing } on line 10. In this specific case, hypothetically the compiler
could realize the error is before line 15, because there are two return statements (line
10 and 12) otherwise. 

I imagine finding the "true" location of the syntax error in the general case
is hard to do, but I figured I would document the behavior in case something could be
done.
bradfitz commented 11 years ago

Comment 2:

Labels changed: removed priority-triage.

Status changed to Accepted.

btracey commented 11 years ago

Comment 3:

Here is a similar example of the same problem. It's a little contrived because there are
errors because my example uses an outside package and so there are more errors, however:
http://play.golang.org/p/CDR-Lpn2Wi
We see that the error is on line 114: syntax error: unexpected ). The real issue is that
Optimize on line 13 does not have a closed }. In between there are a constant, type, and
variable declarations, none of which would be acceptable in a function before it finally
triggers an error for unexpected ). It would be awesome if this error could be more
localized, (closer to line 13), or at least it would look at the whole code to see if
there's ever a balanced closed bracket, and then throw an error that there is no
matching closed bracket.
rsc commented 11 years ago

Comment 4:

Labels changed: added priority-later, go1.2maybe.

robpike commented 11 years ago

Comment 5:

Unlikely to happen in 1.2. Low priority.

Labels changed: added go1.3maybe, removed go1.2maybe.

robpike commented 11 years ago

Comment 6:

Labels changed: removed go1.3maybe.

btracey commented 11 years ago

Comment 7:

Another example it would be nice to change
http://play.golang.org/p/Q7-VnoRnEH
The error in Main() is reported before the error about Foo. In my actual code, Foo was
called a bunch so the error to Foo got buried
btracey commented 11 years ago

Comment 8:

Another:
http://play.golang.org/p/OE16PMdDTY
The actual error is typing "type" instead of "func" for what is supposed to be the error
method call. Saying something like "expected semicolon or newline after type declaration
block" would trigger "Huh? That's not a type declaration block... oh wait"
rsc commented 10 years ago

Comment 9:

Labels changed: added go1.3maybe.

rsc commented 10 years ago

Comment 10:

Labels changed: added release-none, removed go1.3maybe.

rsc commented 10 years ago

Comment 11:

Labels changed: added repo-main.