From: Josh Bleecher Snyder Date: Mon, 9 Mar 2020 13:37:49 +0000 (-0700) Subject: cmd/compile: use only bit patterns in isNonNegative X-Git-Tag: go1.15beta1~916 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=5fac45a320561b45b52cdcae933882a70699a21d;p=gostls13.git cmd/compile: use only bit patterns in isNonNegative CL 212777 added a check to isNonNegative to return true for unsigned values. However, the SSA backend isn't type safe enough for that to be sound. The other checks in isNonNegative look only at the pattern of bits. Remove the type-based check. Updates #37753 Change-Id: I059d0e86353453133f2a160dce53af299f42e533 Reviewed-on: https://go-review.googlesource.com/c/go/+/222620 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- diff --git a/src/cmd/compile/internal/ssa/prove.go b/src/cmd/compile/internal/ssa/prove.go index 4788f2d803..12c2580c95 100644 --- a/src/cmd/compile/internal/ssa/prove.go +++ b/src/cmd/compile/internal/ssa/prove.go @@ -1305,9 +1305,9 @@ func isNonNegative(v *Value) bool { if !v.Type.IsInteger() { panic("isNonNegative bad type") } - if !v.Type.IsSigned() { - return true - } + // TODO: return true if !v.Type.IsSigned() + // SSA isn't type-safe enough to do that now (issue 37753). + // The checks below depend only on the pattern of bits. switch v.Op { case OpConst64: diff --git a/test/fixedbugs/issue37753.go b/test/fixedbugs/issue37753.go new file mode 100644 index 0000000000..ac311e3715 --- /dev/null +++ b/test/fixedbugs/issue37753.go @@ -0,0 +1,18 @@ +// run + +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +//go:noinline +func f(a, b uint) int { + return int(a-b) / 8 +} + +func main() { + if x := f(1, 2); x != 0 { + panic(x) + } +}