Russ Cox | d2cc988 | 2012-02-16 23:50:37 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 2 | |
| 3 | // Copyright 2011 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
Rob Pike | 83976e3 | 2012-02-19 14:28:53 +1100 | [diff] [blame] | 7 | // Test evaluation order. |
| 8 | |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 9 | package main |
| 10 | |
| 11 | var calledf int |
| 12 | |
| 13 | func f() int { |
| 14 | calledf++ |
| 15 | return 0 |
| 16 | } |
| 17 | |
| 18 | func g() int { |
| 19 | return calledf |
| 20 | } |
| 21 | |
| 22 | var xy string |
| 23 | |
| 24 | func x() bool { |
| 25 | for false { |
| 26 | } // no inlining |
| 27 | xy += "x" |
| 28 | return false |
| 29 | } |
| 30 | |
| 31 | func y() string { |
| 32 | for false { |
| 33 | } // no inlining |
| 34 | xy += "y" |
| 35 | return "abc" |
| 36 | } |
| 37 | |
| 38 | func main() { |
| 39 | if f() == g() { |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 40 | panic("wrong f,g order") |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | if x() == (y() == "abc") { |
| 44 | panic("wrong compare") |
| 45 | } |
| 46 | if xy != "xy" { |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 47 | panic("wrong x,y order") |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 48 | } |
| 49 | } |