-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: better error message for ambiguous parse #70828
Comments
Probably the same as #37043 (comment) ? |
This is well known; see https://golang.org/ref/spec#Composite_literals: A parsing ambiguity arises when a composite literal using the TypeName form of the LiteralType appears as an operand between the keyword and the opening brace of the block of an "if", "for", or "switch" statement, and the composite literal is not enclosed in parentheses, square brackets, or curly braces. In this rare case, the opening brace of the literal is erroneously parsed as the one introducing the block of statements. To resolve the ambiguity, the composite literal must appear within parentheses. |
closing this |
Reopening because I think we can produce a better error message. package p
type S struct {
F int
}
func F() {
if s := S{}; s.F != 0 {
}
} The gc compiler says:
The gccgo compiler says
CC @griesemer |
Go version
go version 1.23
Output of
go env
in your module/workspace:What did you do?
I expected
to be parse correctly. See https://go.dev/play/p/XHkampOkowJ for a reproduction case.
Actual result of that playground snippet was:
./prog.go:23:10: syntax error: cannot use mErr := &MyError as value
./prog.go:26:1: syntax error: non-declaration statement outside function body
Changing the line
into
or into
makes the code work, but I would expect the first variant to work just as well.
What did you see happen?
./prog.go:23:10: syntax error: cannot use mErr := &MyError as value
./prog.go:26:1: syntax error: non-declaration statement outside function body
What did you expect to see?
A compiled program
The text was updated successfully, but these errors were encountered: