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: misleading error when supplying a statement as a function parameter #15307

Closed
0xmohit opened this issue Apr 14, 2016 · 5 comments
Closed

Comments

@0xmohit
Copy link
Contributor

0xmohit commented Apr 14, 2016

$  go version
go version go1.6 linux/amd64
$  go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

Tried to compile the following:

package main

import "fmt"

func main() {
        i := 42
        fmt.Println((i++))
}
t.go:7: syntax error: unexpected ++, expecting )
t.go:8: syntax error: non-declaration statement outside function body

The second error should be suppressed. (It's not observed using 1.5.3 and earlier.)

@bradfitz bradfitz added this to the Unplanned milestone Apr 15, 2016
@ALTree
Copy link
Member

ALTree commented Aug 12, 2017

Not limited to function parameters:

package main

func main() {
	i := 42
	(i++)
}
$ go run prova.go
# command-line-arguments
./prova.go:5: syntax error: unexpected ++, expecting )
./prova.go:6: syntax error: non-declaration statement outside function body

Also

package main

func main() {
	i := 42
	(var int)
}

gives the same double error. Seems like the parser is confused by certain syntax issues when inside _{L,R}paren.

@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 12, 2017
@griesemer
Copy link
Contributor

griesemer commented Aug 14, 2017

Except for grouping of declarations (icl. parameter declarations), inside ()'s we can only have expressions. Possibly error recovery could be a bit stronger in that case.

@griesemer
Copy link
Contributor

It's tricky to come up with a good parser recovery strategy in cases like these. The culprit is the syntax.parser.want method:

 func (p *parser) want(tok token) {
        if !p.got(tok) {
                p.syntax_error("expecting " + tok.String())
                p.advance()
        }
 }

If a ++ (or any other token) is found instead of the closing token with tok == ), an error is reported and the offending token is consumed. This assumes that the expected token is simply missing. In these cases, the expected ) follows. So a simple way to work around this here is to change the want method as follows:

@@ -90,7 +90,8 @@ func (p *parser) got(tok token) bool {
 func (p *parser) want(tok token) {
        if !p.got(tok) {
                p.syntax_error("expecting " + tok.String())
-               p.advance()
+               p.advance(tok)
+               p.got(tok) // try once more
        }
 }

This will fix these issues, but cause others (run the tests).

Leaving open for further exploration. Not urgent.

@griesemer griesemer removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 14, 2017
@0xmohit
Copy link
Contributor Author

0xmohit commented Jan 29, 2018

This appears to be fixed in c37090f.

@0xmohit 0xmohit closed this as completed Jan 29, 2018
@griesemer
Copy link
Contributor

Confirming. Thanks for noticing this, @0xmohit .

@golang golang locked and limited conversation to collaborators Jan 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants