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: add error if unsigned tested for negativity #22844

Closed
SMerrony opened this issue Nov 22, 2017 · 2 comments

Comments

Projects
None yet
4 participants
@SMerrony
Copy link

commented Nov 22, 2017

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

1.9,2

Does this issue reproduce with the latest release?

Yes

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

amd64/windows and also amd64/linux

What did you do?

Compiled this...

import "fmt"
import "time"
func main() {
	var uns uint32
	uns = uint32(time.Now().Weekday())
	if uns < 0 {
		fmt.Printf("Negative uns: %d\n", uns)
	}
}

What did you expect to see?

Error when unsigned is tested for negative.

What did you see instead?

Nothing

I believe it would be very useful if this error could be trapped at compile time.

@ALTree

This comment has been minimized.

Copy link
Member

commented Nov 22, 2017

This has been proposed (and rejected) in the past.

Ref: #8601.

Initially, the proposal said

the compiler should refuse to compile this statement.

but the commenters agreed that a vet check might be more appropriate (see the discussion on the linked issue for more details).

Someone sent a change to add a vet check, but it wasn't merged. Apparently, it didn't satisfy the 3 vet requirements (correctness, precision, frequency).

Anyway, that was about vet. It seems like everyone agreed not to make this a compile error.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Nov 22, 2017

Closing as dup.

@golang golang locked and limited conversation to collaborators Nov 22, 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.