Syntax errors involving compound literals

77 views
Skip to first unread message

sb...@google.com

unread,
Aug 25, 2016, 12:21:30 AM8/25/16
to golang-nuts
Hello group,

Why are the following snippets errors?

type S struct {
 i
int
}

 if S{i: 0} == S{} {
 
}
syntax error: unexpected == at end of statement

 switch S{i: 0} == S{} {
 
}
syntax error: unexpected i, expecting case or default or }

It works without error if you surround each literal with ( ), or the entire comparison expression with ( ).
The following also works without error:
 _ = S{i: 0} == S{}

Does the language spec state those are errors, or is this a bug in the parser?

Thanks!

Ian Lance Taylor

unread,
Aug 25, 2016, 1:22:22 AM8/25/16
to sb...@google.com, golang-nuts
Search for "parsing ambiguity" in
https://golang.org/ref/spec#Composite_literals .

Ian

sb...@google.com

unread,
Aug 25, 2016, 9:40:04 PM8/25/16
to golang-nuts, sb...@google.com
Why hadn't I found that? Thanks for the pointer!

I guess this has to do with the fact that the parser doesn't care whether S is a type name or not.
Reply all
Reply to author
Forward
0 new messages