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: spurious error when ~ is used as an infix operator #23587

Closed
davecheney opened this issue Jan 28, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@davecheney
Copy link
Contributor

commented Jan 28, 2018

Please answer these questions before submitting your issue. Thanks!

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

1.9.3

Does this issue reproduce with the latest release?

Unknown

What operating system and processor architecture are you using (go env)?

Go playground

What did you do?

package main

func main() {
	var a bool
	a = a ~ a
}

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

prog.go:5:8: bitwise complement operator is ^

What did you see instead?

prog.go:5:8: bitwise complement operator is ^
prog.go:5:8: invalid operation: a ^ a (operator ^ not defined on bool)

@odeke-em odeke-em changed the title Spurious error when ~ is used as an infix operator cmd/compile: spurious error when ~ is used as an infix operator Jan 28, 2018

@odeke-em

This comment has been minimized.

Copy link
Member

commented Jan 28, 2018

Thank you @davecheney for this report and it is great to see you here in the new year!

@griesemer I see that in the scanning phase we recognize that people commonly use the bitwise complement '~' in other languages, and perhaps intentionally fallthrough to continue lexical analysis as we are doing in the context of the proper operator in Go ^
https://github.com/golang/go/tree/master/src/cmd/compile/internal/syntax#L232

However, in this context I believe that the entire operation is invalid irrespective of the types of operands i.e, even if it were 2 ~ 2, this still doesn't make sense since ~ is a unary operator.
Perhaps we could improve the error and only fallthrough as we are doing, if we detect a single operand on the right of the ~?

@odeke-em odeke-em modified the milestones: Go1.11, Unreleased Jan 28, 2018

@griesemer

This comment has been minimized.

Copy link
Contributor

commented Jan 28, 2018

The scanner doesn't know about context (are there one or two operands) and thus cannot make that distinction. One could pass through the ~ and make the distinction in the parser, but that seems overkill: The reason for this error message is mostly historic. Go doesn't have a ~ symbol, so maybe it should just complain about an invalid character.

go/types says: illegal character U+007E '~'.

@griesemer griesemer self-assigned this Jan 28, 2018

@griesemer griesemer modified the milestones: Unreleased, Go1.11 Jan 28, 2018

@griesemer griesemer added the NeedsFix label Jan 28, 2018

@davecheney

This comment has been minimized.

Copy link
Contributor Author

commented Jan 28, 2018

Hey all, thanks for looking into this issue. I'm less interested in the first line, and more interested in the second

prog.go:5:8: invalid operation: a ^ a (operator ^ not defined on bool)

As the code sample never mentions ^

@griesemer

This comment has been minimized.

Copy link
Contributor

commented Jan 29, 2018

@davecheney Yes, thanks for pointing that out. During lexical analysis, the scanner assumes that if a user typed ~ they actually meant ^; so it complains but then quietly continues with ^. That works great if the assumption was correct, but it fails miserably otherwise. Which is why I am thinking that perhaps the compiler should just complain about ~ not being a valid token. The existing behavior was simply brought over from Ken Thompson's original compiler which used to be a C compiler. I suspect this may have been a common error for him to make, hence the current code.

@gopherbot

This comment has been minimized.

Copy link

commented Feb 15, 2018

Change https://golang.org/cl/94160 mentions this issue: cmd/compile/internal/syntax: don't assume (operator) ~ means operator ^

@gopherbot gopherbot closed this in 33eb063 Feb 15, 2018

@golang golang locked and limited conversation to collaborators Feb 15, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.