Bugzilla – Bug 323269
[GMCS] Unable to compare a struct to null
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by pawel.sakowski@mindbreeze.com 2007-01-22 12:41:31 MST ---- Description of Problem: gmcs is unable to compare value types to null. Steps to reproduce the problem: 1. Attempt to compile the following code: class X{static void Main(){ if (System.DateTime.Now == null); }} Actual Results: struct-vs-null.cs(2,1): warning CS0642: Possible mistaken empty statement struct-vs-null.cs(2,21): error CS0019: Operator `==' cannot be applied to operands of type `System.DateTime' and `null' Compilation failed: 1 error(s), 1 warnings Expected Results: A successful compilation (the warning is O.K.) How often does this happen? 100% Additional Information: csc compiles the same code successfully. mono 1.2 How pointless such a comparison is is beyond the scope of this report. ---- Additional Comments From gert.driesen@pandora.be 2007-01-22 17:26:32 MST ---- Note: csc 1.x reports the same error, but using csc 2.x it compiles fine. ---- Additional Comments From miguel@ximian.com 2007-01-28 13:33:49 MST ---- I can not find a place in the spec that addresses this issue (the closest I found was related to nullable types). I do not want to support this without a good reason, because the code above is masking a real problem: that comparing "Now" to null is always going to be false (always, not sometimes, but always). ---- Additional Comments From pawel.sakowski@mindbreeze.com 2007-01-29 05:13:35 MST ---- Yes, looks like the spec is pretty silent about this. It seems that neither the results of such a comparison are specified, nor is it explicitly forbidden. Regarding "always going to be false": yes, that's exactly what I meant with "pointless". The pre-existing code I've met that triggered this issue used a comparison of a struct to null as an over-eager check for ArgumentNullException.