aboutsummaryrefslogtreecommitdiff
path: root/test/const.go
diff options
context:
space:
mode:
authorTrey Lawrence <lawrence.trey@gmail.com>2016-08-23 16:43:43 -0400
committerMatthew Dempsky <mdempsky@google.com>2016-09-17 01:12:24 +0000
commitfc5df089da6c02397897f11a875a593353dc0590 (patch)
tree2e6aa8d444ca3faadf2a491e5caa44d6d7d14f46 /test/const.go
parent6fe1febc867237fdf9ae40483044ed377144627f (diff)
downloadgo-fc5df089da6c02397897f11a875a593353dc0590.tar.gz
go-fc5df089da6c02397897f11a875a593353dc0590.zip
cmd/compile: fix compiler bug for constant equality comparison
The compiler incorrectly will error when comparing a nil pointer interface to a nil pointer of any other type. Example: (*int)(nil) == interface{}(nil) Will error with "gc: illegal constant expression: *int == interface {}" Fixes #16702 Change-Id: I1a15d651df2cfca6762b1783a28b377b2e6ff8c6 Reviewed-on: https://go-review.googlesource.com/27591 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'test/const.go')
-rw-r--r--test/const.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/test/const.go b/test/const.go
index 6c29336396..f8e0a753cb 100644
--- a/test/const.go
+++ b/test/const.go
@@ -123,9 +123,44 @@ func floats() {
assert(f == f1e3, "f == f1e3")
}
+func interfaces() {
+ var (
+ nilN interface{}
+ nilI *int
+ five = 5
+
+ _ = nil == interface{}(nil)
+ _ = interface{}(nil) == nil
+ )
+ ii := func(i1 interface{}, i2 interface{}) bool { return i1 == i2 }
+ ni := func(n interface{}, i int) bool { return n == i }
+ in := func(i int, n interface{}) bool { return i == n }
+ pi := func(p *int, i interface{}) bool { return p == i }
+ ip := func(i interface{}, p *int) bool { return i == p }
+
+ assert((interface{}(nil) == interface{}(nil)) == ii(nilN, nilN),
+ "for interface{}==interface{} compiler == runtime")
+
+ assert(((*int)(nil) == interface{}(nil)) == pi(nilI, nilN),
+ "for *int==interface{} compiler == runtime")
+ assert((interface{}(nil) == (*int)(nil)) == ip(nilN, nilI),
+ "for interface{}==*int compiler == runtime")
+
+ assert((&five == interface{}(nil)) == pi(&five, nilN),
+ "for interface{}==*int compiler == runtime")
+ assert((interface{}(nil) == &five) == ip(nilN, &five),
+ "for interface{}==*int compiler == runtime")
+
+ assert((5 == interface{}(5)) == ni(five, five),
+ "for int==interface{} compiler == runtime")
+ assert((interface{}(5) == 5) == in(five, five),
+ "for interface{}==int comipiler == runtime")
+}
+
func main() {
ints()
floats()
+ interfaces()
assert(ctrue == true, "ctrue == true")
assert(cfalse == false, "cfalse == false")