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

Comments

Projects
None yet
4 participants
@josharian
Copy link
Contributor

commented Apr 13, 2017

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

This comment has been minimized.

Copy link
Contributor Author

commented Apr 13, 2017

Should this code be valid?

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

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

@ALTree

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Contributor Author

commented Apr 13, 2017

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 .

@josharian josharian closed this Apr 13, 2017

@griesemer

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor Author

commented Apr 13, 2017

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.
You can’t perform that action at this time.