[Mono-bugs] [Bug 52588][Wis] New - User-defined conditional logical operators may evaluate parameters twice

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Sun, 4 Jan 2004 15:47:52 -0500 (EST)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by bmaurer@users.sf.net.

http://bugzilla.ximian.com/show_bug.cgi?id=52588

--- shadow/52588	2004-01-04 15:47:51.000000000 -0500
+++ shadow/52588.tmp.30051	2004-01-04 15:47:52.000000000 -0500
@@ -0,0 +1,83 @@
+Bug#: 52588
+Product: Mono/Compilers
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Wishlist
+Component: C#
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: bmaurer@users.sf.net               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: User-defined conditional logical operators may evaluate parameters twice
+
+using System;
+
+class Foo {
+	static void Main () {	
+		Console.WriteLine (t && f); Console.WriteLine ();
+		Console.WriteLine (t && t); Console.WriteLine ();
+	}
+	
+	static MyBool t { get { Console.WriteLine ("t"); return new MyBool (true); }}
+	static MyBool f { get { Console.WriteLine ("f"); return new MyBool (false); }}
+}
+
+public struct MyBool {
+	bool v;
+	
+	public MyBool (bool v) { this.v = v; }
+	
+	public static MyBool operator & (MyBool x, MyBool y) {
+		return new MyBool (x.v & y.v);  
+	}
+	
+	public static MyBool operator | (MyBool x, MyBool y) {
+		return new MyBool (x.v | y.v);  
+	}
+	
+	public static bool operator true (MyBool x) {
+		return x.v;  
+	}
+	
+	public static bool operator false (MyBool x) {
+		return ! x.v;  
+	}
+	
+	public override string ToString () { return v.ToString (); }
+}
+
+With MCS we print:
+
+[benm@Ben tmp]$ mono t.exe
+t
+t
+f
+False
+ 
+t
+t
+t
+
+With CSC it prints
+t
+f
+False
+ 
+t
+t
+True
+
+According to 14.11.2: User-defined conditional logical operators
+
+The operation x && y is evaluated as T.false(x) ? x : T.&(x, y)...
+The operation x || y is evaluated as T.true(x) ? x : T.|(x, y)...
+
+In either of these operations, the expression given by x is only evaluated
+once, and the expression given by y is either not evaluated or evaluated
+exactly once.