aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josharian@gmail.com>2018-04-12 08:23:00 -0700
committerAndrew Bonventre <andybons@golang.org>2018-04-27 20:52:26 +0000
commit5b6ff694bbc867f4ae95fdc624fb1f4511e5623e (patch)
tree82aeb5e89624959f0b1a7a37f9250085027f6c3c
parent11d7a5a950a7189855bd98caf3083c9ea3d77beb (diff)
downloadgo-5b6ff694bbc867f4ae95fdc624fb1f4511e5623e.tar.gz
go-5b6ff694bbc867f4ae95fdc624fb1f4511e5623e.zip
[release-branch.go1.10] cmd/compile: fix evaluation of "" < s
Fixes #24935 Change-Id: Ifa79ab3dfe69297eeef85f7193cd5f85e5982bc5 Reviewed-on: https://go-review.googlesource.com/106655 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-on: https://go-review.googlesource.com/108944 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Andrew Bonventre <andybons@golang.org>
-rw-r--r--src/cmd/compile/internal/gc/walk.go7
-rw-r--r--test/fixedbugs/issue24817.go64
2 files changed, 71 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go
index f48513dc73..ca3025bbaa 100644
--- a/src/cmd/compile/internal/gc/walk.go
+++ b/src/cmd/compile/internal/gc/walk.go
@@ -1261,6 +1261,13 @@ opswitch:
}
if cs != nil {
cmp := Op(n.Etype)
+ // Our comparison below assumes that the non-constant string
+ // is on the left hand side, so rewrite "" cmp x to x cmp "".
+ // See issue 24817.
+ if Isconst(n.Left, CTSTR) {
+ cmp = brrev(cmp)
+ }
+
// maxRewriteLen was chosen empirically.
// It is the value that minimizes cmd/go file size
// across most architectures.
diff --git a/test/fixedbugs/issue24817.go b/test/fixedbugs/issue24817.go
new file mode 100644
index 0000000000..ba2a138ed3
--- /dev/null
+++ b/test/fixedbugs/issue24817.go
@@ -0,0 +1,64 @@
+// run
+
+// Copyright 2018 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.
+
+// Check all ways to compare a non-constant string to the empty string.
+
+package main
+
+import (
+ "fmt"
+ "os"
+)
+
+var (
+ s = "abc"
+ e = ""
+ failed bool
+)
+
+func main() {
+ want(true, "" < s, `"" < s`)
+ want(false, s < "", `s < ""`)
+ want(false, "" < e, `"" < e`)
+ want(false, e < "", `e < ""`)
+
+ want(true, "" <= s, `"" <= s`)
+ want(false, s <= "", `s <= ""`)
+ want(true, "" <= e, `"" <= e`)
+ want(true, e <= "", `e <= ""`)
+
+ want(false, "" > s, `"" > s`)
+ want(true, s > "", `s > ""`)
+ want(false, "" > e, `"" > e`)
+ want(false, e > "", `e > ""`)
+
+ want(false, "" >= s, `"" >= s`)
+ want(true, s >= "", `s >= ""`)
+ want(true, "" >= e, `"" >= e`)
+ want(true, e >= "", `e >= ""`)
+
+ want(false, "" == s, `"" == s`)
+ want(false, s == "", `s == ""`)
+ want(true, "" == e, `"" == e`)
+ want(true, e == "", `e == ""`)
+
+ want(true, "" != s, `"" != s`)
+ want(true, s != "", `s != ""`)
+ want(false, "" != e, `"" != e`)
+ want(false, e != "", `e != ""`)
+
+ if failed {
+ os.Exit(1)
+ }
+}
+
+//go:noinline
+func want(b bool, have bool, msg string) {
+ if b != have {
+ fmt.Println(msg)
+ failed = true
+ }
+}