Author: raja
Date: 2005-04-18 09:25:06 -0400 (Mon, 18 Apr 2005)
New Revision: 43214
Added:
trunk/mcs/tests/test-369.cs
Modified:
trunk/mcs/mcs/ChangeLog
trunk/mcs/mcs/expression.cs
trunk/mcs/tests/ChangeLog
trunk/mcs/tests/Makefile
Log:
Fix #74481.
* mcs/expression.cs (Binary.EqualsNullIsReferenceEquals): New.
(Binary.DoResolveOperator): Use it to avoid blindly optimizing out
all null comparisons against reference types.
* tests/test-369.cs: New test based on #74481.
Modified: trunk/mcs/mcs/ChangeLog
===================================================================
--- trunk/mcs/mcs/ChangeLog 2005-04-18 12:58:51 UTC (rev 43213)
+++ trunk/mcs/mcs/ChangeLog 2005-04-18 13:25:06 UTC (rev 43214)
@@ -1,3 +1,10 @@
+2005-04-18 Raja R Harinath <[EMAIL PROTECTED]>
+
+ Fix #74481.
+ * expression.cs (Binary.EqualsNullIsReferenceEquals): New.
+ (Binary.DoResolveOperator): Use it to avoid blindly optimizing out
+ all null comparisons against reference types.
+
2005-04-18 Marek Safar <[EMAIL PROTECTED]>
Fix# 74565
Modified: trunk/mcs/mcs/expression.cs
===================================================================
--- trunk/mcs/mcs/expression.cs 2005-04-18 12:58:51 UTC (rev 43213)
+++ trunk/mcs/mcs/expression.cs 2005-04-18 13:25:06 UTC (rev 43214)
@@ -2248,17 +2248,28 @@
return null;
}
+ //
+ // This is used to check if a test 'x == null' can be optimized
to a reference equals,
+ // i.e., not invoke op_Equality.
+ //
+ static bool EqualsNullIsReferenceEquals (Type t)
+ {
+ return t == TypeManager.object_type || t ==
TypeManager.string_type ||
+ t == TypeManager.delegate_type ||
t.IsSubclassOf (TypeManager.delegate_type);
+ }
+
Expression ResolveOperator (EmitContext ec)
{
Type l = left.Type;
Type r = right.Type;
- //
- // Special cases: string comapred to null
- //
if (oper == Operator.Equality || oper ==
Operator.Inequality){
- if ((!TypeManager.IsValueType (l) && r ==
TypeManager.null_type) ||
- (!TypeManager.IsValueType (r) && l ==
TypeManager.null_type)) {
+ //
+ // Optimize out call to op_Equality in a few
cases.
+ //
+ if ((l == TypeManager.null_type &&
EqualsNullIsReferenceEquals (r)) ||
+ (r == TypeManager.null_type &&
EqualsNullIsReferenceEquals (l))) {
+
Type = TypeManager.bool_type;
return this;
Modified: trunk/mcs/tests/ChangeLog
===================================================================
--- trunk/mcs/tests/ChangeLog 2005-04-18 12:58:51 UTC (rev 43213)
+++ trunk/mcs/tests/ChangeLog 2005-04-18 13:25:06 UTC (rev 43214)
@@ -1,3 +1,7 @@
+2005-04-18 Raja R Harinath <[EMAIL PROTECTED]>
+
+ * test-369.cs: New test based on #74481.
+
2005-04-18 Marek Safar <[EMAIL PROTECTED]>
* test-362.cs: Enabled.
Modified: trunk/mcs/tests/Makefile
===================================================================
--- trunk/mcs/tests/Makefile 2005-04-18 12:58:51 UTC (rev 43213)
+++ trunk/mcs/tests/Makefile 2005-04-18 13:25:06 UTC (rev 43214)
@@ -36,7 +36,7 @@
test-333 test-311 test-335 test-336 test-337 test-287
test-338 test-339 test-341 test-334 test-342 \
test-344 test-345 test-346 test-347 test-348 test-349
test-350 test-351 test-352 test-353 test-354 test-355 \
test-356 test-357 test-358 test-359 test-360 test-361
test-362 test-363 test-364 test-365 test-366 test-367 \
- test-368 \
+ test-368 test-369 \
mtest-7-dll mtest-7-exe \
mtest-8-dll mtest-8-exe
Added: trunk/mcs/tests/test-369.cs
===================================================================
--- trunk/mcs/tests/test-369.cs 2005-04-18 12:58:51 UTC (rev 43213)
+++ trunk/mcs/tests/test-369.cs 2005-04-18 13:25:06 UTC (rev 43214)
@@ -0,0 +1,24 @@
+class Test {
+ static int count;
+
+ static public bool operator == (Test x, Test y)
+ {
+ ++count;
+ return false;
+ }
+
+ static public bool operator != (Test x, Test y) { return true; }
+
+ public override bool Equals (object o) { return false; }
+
+ public override int GetHashCode () { return 0; }
+
+ static void Main ()
+ {
+ Test y = new Test ();
+ if (y == null)
+ throw new System.Exception ();
+ if (count != 1)
+ throw new System.Exception ("Operator == was not
called");
+ }
+}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches