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: invalid literal causes errors in subsequent lines of valid code #15611

Closed
0xmohit opened this issue May 9, 2016 · 4 comments
Closed
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@0xmohit
Copy link
Contributor

0xmohit commented May 9, 2016

$ go version
go version go1.6.2 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 (https://play.golang.org/p/hbj73GGYMU):

$ cat -n t.go 
     1  package main
     2
     3  func main() {
     4          _ = append([]byte("hello "), byte('world')) // invalid
     5          _ = append([]byte("hello "), byte('a'))     // ok
     6  }
     7
$  go tool compile t.go 
t.go:4: missing '
t.go:4: syntax error: unexpected orld, expecting comma or )
t.go:4: missing '
t.go:5: syntax error: unexpected ), expecting }
t.go:6: syntax error: non-declaration statement outside function body

No errors should have been reported for lines 5 and 6. missing ' probably should have been reported just once.

@bradfitz bradfitz added this to the Go1.8 milestone May 9, 2016
@bradfitz
Copy link
Contributor

bradfitz commented May 9, 2016

This was fine in Go 1.4 and Go 1.5 and regressed in Go 1.6, not this cycle, so we can probably live with it for another cycle.

$ GOROOT=$HOME/go1.4 go build x.go
# command-line-arguments
./x.go:4: missing '
./x.go:4: syntax error: unexpected name, expecting )
./x.go:4: missing '

$ GOROOT=$HOME/go1.5 go build x.go
# command-line-arguments
./x.go:4: missing '
./x.go:4: syntax error: unexpected name, expecting )
./x.go:4: missing '

$ GOROOT=$HOME/go1.6 go build x.go
# command-line-arguments
./x.go:4: missing '
./x.go:4: syntax error: unexpected orld, expecting comma or )
./x.go:4: missing '
./x.go:5: syntax error: unexpected ), expecting }
./x.go:6: syntax error: non-declaration statement outside function body

$ GOROOT=$HOME/gotip go build x.go
# command-line-arguments
./x.go:4: missing '
./x.go:4: syntax error: unexpected orld, expecting comma or )
./x.go:4: missing '
./x.go:5: syntax error: unexpected ), expecting }
./x.go:6: syntax error: non-declaration statement outside function body

/cc @griesemer @mdempsky

@mdempsky
Copy link
Member

Worth looking into for 1.8 for any way we can improve this, but in general recovering from syntax errors is always going to be best effort.

The reason you get two missing ' errors is because after lexing 'w, we expect a ' to end the rune literal. When we don't see one, we emit the error, and then pretend we did get one. Moving along, we then see ') and think that's supposed to be another rune literal, but it's missing a ' too.

@griesemer griesemer modified the milestones: Go1.8Maybe, Go1.8 Oct 6, 2016
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 10, 2016
@rsc rsc modified the milestones: Go1.9, Go1.8Maybe Oct 20, 2016
@griesemer
Copy link
Contributor

For the reference, gotype simply reports for the example above:

x.go:4:43: illegal rune literal

which is probably the best error message in this case.

The reason is that the go/scanner accepts rune literals with multiple characters and complains if there's more than one. This seems to work better in this specific case.

@gopherbot
Copy link

CL https://golang.org/cl/37138 mentions this issue.

@golang golang locked and limited conversation to collaborators Feb 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants