diff options
author | Russ Cox <rsc@golang.org> | 2012-01-25 17:53:50 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2012-01-25 17:53:50 -0500 |
commit | 26cf03465112923dbeb12d7d64c25b113b7ee85e (patch) | |
tree | 36e57368c89076d51b98ce8739f3c43589c6ff7f /test | |
parent | 179f82987842d5859c904f2e62437c547b6810be (diff) | |
download | go-26cf03465112923dbeb12d7d64c25b113b7ee85e.tar.gz |
gc: fix order of evaluation
Pulling function calls out to happen before the
expression being evaluated was causing illegal
reorderings even without inlining; with inlining
it got worse. This CL adds a separate ordering pass
to move things with a fixed order out of expressions
and into the statement sequence, where they will
not be reordered by walk.
Replaces lvd's CL 5534079.
Fixes issue 2740.
R=lvd
CC=golang-dev
http://codereview.appspot.com/5569062
Diffstat (limited to 'test')
-rw-r--r-- | test/fixedbugs/bug401.go | 41 | ||||
-rw-r--r-- | test/func8.go | 47 | ||||
-rw-r--r-- | test/reorder2.go | 174 |
3 files changed, 250 insertions, 12 deletions
diff --git a/test/fixedbugs/bug401.go b/test/fixedbugs/bug401.go index baad1bc7d..553e217b7 100644 --- a/test/fixedbugs/bug401.go +++ b/test/fixedbugs/bug401.go @@ -1,29 +1,46 @@ -// $G $D/$F.go || echo "Bug398" +// $G $D/$F.go && $L $F.$A && ./$A.out || echo "Bug401" // Copyright 2011 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. // Issue 2582 -package foo - -type T struct {} +package main + +type T struct{} + func (T) cplx() complex128 { - for false {} // avoid inlining - return complex(1,0) + for false { + } // avoid inlining + return complex(1, 0) +} + +func (T) cplx2() complex128 { + return complex(0, 1) } type I interface { cplx() complex128 } -func f(e float32, t T) { +func main() { - _ = real(t.cplx()) - _ = imag(t.cplx()) + var t T + + if v := real(t.cplx()); v != 1 { + panic("not-inlined complex call failed") + } + _ = imag(t.cplx()) + + _ = real(t.cplx2()) + if v := imag(t.cplx2()); v != 1 { + panic("potentially inlined complex call failed") + } var i I i = t - _ = real(i.cplx()) - _ = imag(i.cplx()) -}
\ No newline at end of file + if v := real(i.cplx()); v != 1 { + panic("potentially inlined complex call failed") + } + _ = imag(i.cplx()) +} diff --git a/test/func8.go b/test/func8.go new file mode 100644 index 000000000..bb6106453 --- /dev/null +++ b/test/func8.go @@ -0,0 +1,47 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2011 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 + +var calledf int + +func f() int { + calledf++ + return 0 +} + +func g() int { + return calledf +} + +var xy string + +func x() bool { + for false { + } // no inlining + xy += "x" + return false +} + +func y() string { + for false { + } // no inlining + xy += "y" + return "abc" +} + +func main() { + if f() == g() { + println("wrong f,g order") + } + + if x() == (y() == "abc") { + panic("wrong compare") + } + if xy != "xy" { + println("wrong x,y order") + } +} diff --git a/test/reorder2.go b/test/reorder2.go new file mode 100644 index 000000000..3e149853a --- /dev/null +++ b/test/reorder2.go @@ -0,0 +1,174 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2010 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. + +// derived from fixedbugs/bug294.go + +package main + +var log string + +type TT int + +func (t TT) a(s string) TT { + log += "a(" + s + ")" + return t +} + +func (TT) b(s string) string { + log += "b(" + s + ")" + return s +} + +type F func(s string) F + +func a(s string) F { + log += "a(" + s + ")" + return F(a) +} + +func b(s string) string { + log += "b(" + s + ")" + return s +} + +type I interface { + a(s string) I + b(s string) string +} + +type T1 int + +func (t T1) a(s string) I { + log += "a(" + s + ")" + return t +} + +func (T1) b(s string) string { + log += "b(" + s + ")" + return s +} + +// f(g(), h()) where g is not inlinable but h is will have the same problem. +// As will x := g() + h() (same conditions). +// And g() <- h(). +func f(x, y string) { + log += "f(" + x + ", " + y + ")" +} + +func ff(x, y string) { + for false { + } // prevent inl + log += "ff(" + x + ", " + y + ")" +} + +func h(x string) string { + log += "h(" + x + ")" + return x +} + +func g(x string) string { + for false { + } // prevent inl + log += "g(" + x + ")" + return x +} + +func main() { + err := 0 + var t TT + if a("1")("2")("3"); log != "a(1)a(2)a(3)" { + println("expecting a(1)a(2)a(3) , got ", log) + err++ + } + log = "" + + if t.a("1").a(t.b("2")); log != "a(1)b(2)a(2)" { + println("expecting a(1)b(2)a(2), got ", log) + err++ + } + log = "" + if a("3")(b("4"))(b("5")); log != "a(3)b(4)a(4)b(5)a(5)" { + println("expecting a(3)b(4)a(4)b(5)a(5), got ", log) + err++ + } + log = "" + var i I = T1(0) + if i.a("6").a(i.b("7")).a(i.b("8")).a(i.b("9")); log != "a(6)b(7)a(7)b(8)a(8)b(9)a(9)" { + println("expecting a(6)ba(7)ba(8)ba(9), got", log) + err++ + } + log = "" + + if s := t.a("1").b("3"); log != "a(1)b(3)" || s != "3" { + println("expecting a(1)b(3) and 3, got ", log, " and ", s) + err++ + } + log = "" + + if s := t.a("1").a(t.b("2")).b("3") + t.a("4").b("5"); log != "a(1)b(2)a(2)b(3)a(4)b(5)" || s != "35" { + println("expecting a(1)b(2)a(2)b(3)a(4)b(5) and 35, got ", log, " and ", s) + err++ + } + log = "" + + if s := t.a("4").b("5") + t.a("1").a(t.b("2")).b("3"); log != "a(4)b(5)a(1)b(2)a(2)b(3)" || s != "53" { + println("expecting a(4)b(5)a(1)b(2)a(2)b(3) and 35, got ", log, " and ", s) + err++ + } + log = "" + + if ff(g("1"), g("2")); log != "g(1)g(2)ff(1, 2)" { + println("expecting g(1)g(2)ff..., got ", log) + err++ + } + log = "" + + if ff(g("1"), h("2")); log != "g(1)h(2)ff(1, 2)" { + println("expecting g(1)h(2)ff..., got ", log) + err++ + } + log = "" + + if ff(h("1"), g("2")); log != "h(1)g(2)ff(1, 2)" { + println("expecting h(1)g(2)ff..., got ", log) + err++ + } + log = "" + + if ff(h("1"), h("2")); log != "h(1)h(2)ff(1, 2)" { + println("expecting h(1)h(2)ff..., got ", log) + err++ + } + log = "" + + if s := g("1") + g("2"); log != "g(1)g(2)" || s != "12" { + println("expecting g1g2 and 12, got ", log, " and ", s) + err++ + } + log = "" + + if s := g("1") + h("2"); log != "g(1)h(2)" || s != "12" { + println("expecting g1h2 and 12, got ", log, " and ", s) + err++ + } + log = "" + + if s := h("1") + g("2"); log != "h(1)g(2)" || s != "12" { + println("expecting h1g2 and 12, got ", log, " and ", s) + err++ + } + log = "" + + if s := h("1") + h("2"); log != "h(1)h(2)" || s != "12" { + println("expecting h1h2 and 12, got ", log, " and ", s) + err++ + } + log = "" + + if err > 0 { + panic("fail") + } +} |