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 | |
Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 3 | // Copyright 2011 The Go Authors. All rights reserved. |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 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 | |
Todd Neal | e3e0122 | 2015-10-29 21:45:19 -0500 | [diff] [blame] | 24 | //go:noinline |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 25 | func x() bool { |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 26 | xy += "x" |
| 27 | return false |
| 28 | } |
| 29 | |
Todd Neal | e3e0122 | 2015-10-29 21:45:19 -0500 | [diff] [blame] | 30 | //go:noinline |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 31 | func y() string { |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 32 | xy += "y" |
| 33 | return "abc" |
| 34 | } |
| 35 | |
| 36 | func main() { |
| 37 | if f() == g() { |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 38 | panic("wrong f,g order") |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | if x() == (y() == "abc") { |
| 42 | panic("wrong compare") |
| 43 | } |
| 44 | if xy != "xy" { |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 45 | panic("wrong x,y order") |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 46 | } |
| 47 | } |