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: struct initialization in "if" doesn't compile #38036

Closed
menghanl opened this issue Mar 23, 2020 · 1 comment
Closed

cmd/compile: struct initialization in "if" doesn't compile #38036

menghanl opened this issue Mar 23, 2020 · 1 comment

Comments

@menghanl
Copy link

@menghanl menghanl commented Mar 23, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

https://play.golang.org/p/TACak6vfX1a

package main

import (
	"fmt"
)

type foo struct{}

func main() {
	u := foo{}

	if (u != foo{}) { // With () it works
		fmt.Println("OK")
	}
	
	if u != foo{} { // This doesn't compile
		fmt.Println("hello")
	}
}

What did you expect to see?

No compile error

What did you see instead?

if u != foo{} { does not compile:

./main.go:16:16: syntax error: unexpected { at end of statement
./main.go:19:1: syntax error: non-declaration statement outside function body

Adding parentheses works:

  • if (u != foo{}) {
  • if u != (foo{}) {
@menghanl menghanl changed the title cmd/compile: struct initialization in if doesn't compile cmd/compile: struct initialization in "if" doesn't compile Mar 23, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 23, 2020

This is expected and documented. Search for "ambiguity" in https://golang.org/ref/spec#Composite_literals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.