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: confusing error message "shift of type float64" #19963

Closed
josharian opened this issue Apr 13, 2017 · 5 comments
Closed

cmd/compile: confusing error message "shift of type float64" #19963

josharian opened this issue Apr 13, 2017 · 5 comments

Comments

@josharian
Copy link
Contributor

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

package main

func main() {
	var b uint8
	var _ float64 = 1.5 * (1 << b)
}

generates the error message

main.go:5: invalid operation: 1 << b (shift of type float64)

But there's no shift of type float64. There's a shift of type uint8. The problem is that the result of that shift needs to be converted into a float64 before multiplying.

@josharian josharian added this to the Go1.9Maybe milestone Apr 13, 2017
@josharian
Copy link
Contributor Author

Should this code be valid?

	var b uint8
	_ = float64(1 << b)

Currently go/types and cmd/compile both reject it.

@ALTree
Copy link
Member

ALTree commented Apr 13, 2017

If the left operand of a non-constant shift expression is an untyped constant, it is first converted to the type it would assume if the shift expression were replaced by its left operand alone

So I believe the error "shift of type float64" is correct because what the spec says we do is

float64(float64(1) << b)

so it's true that you are shifting a float.

@josharian
Copy link
Contributor Author

Yeah. I guess you're right. It'd be nice to make the error message a bit clearer--it happened to me in the context of a much more complex expression, but I don't really see how to. Closing for now; thanks, @ALTree .

@griesemer
Copy link
Contributor

griesemer commented Apr 13, 2017

Yes, this is correct: The type of the 1 in (1 << b) is the type it would have if the shift wasn't there. Would be nice to have a clearer explanation in the error but I think that will require some work. Let's leave this for the "ponies" list once we have a compiler that's all neat and clean.

@josharian
Copy link
Contributor Author

A neat and clean compiler should show up somewhere around Go 1.x. Where I am assuming that this release will be called 1.9, the following one 1.a, then 1.b, and so on. :P

@golang golang locked and limited conversation to collaborators Apr 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants