Bugzilla – Bug 323729
[GMCS] Missing exception from invalid cast.
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by miguel@ximian.com 2007-03-05 18:54:18 MST ---- The following code should thrown an InvalidCastException at runtime, but does not with Mono: using System.Collections.Generic; class CantCastGenericListToArray { public static void Main(string[] args) { IList<object> list = new object[] { "foo", "bar" }; string[] array = (string[])list; if (list.Count != array.Length) { throw new System.ApplicationException(); } } } ---- Additional Comments From vargaz@gmail.com 2007-03-06 06:38:41 MST ---- This is a gmcs/reflection bug. The same program compiled using csc throws an exception on mono, and PEVerify reports errors on the gmcs produced one. ---- Additional Comments From miguel@ximian.com 2007-03-06 12:01:05 MST ---- Just a question: shouldn't our verifier throw here with the invalid generated code? ---- Additional Comments From lupus@ximian.com 2007-03-08 11:48:09 MST ---- Please attach either the buggy generated binary or the IL code so we can add it to the verifier test suite. The test doesn't compile with gmcs for me: gmcs-invalidcast.cs(8,48): error CS0030: Cannot convert type `System.Collections.Generic.IList<object>' to `string[]' ---- Additional Comments From lupus@ximian.com 2007-03-08 12:41:28 MST ---- Ok, with an updated gmcs it compiled: gmcs thinks it doesn't need to insert a cast opcode when storing into array: this is most likely a gmcs bug as doing the cast in IL code generates the exception. As for the verifier: this check is currently missing (mini.c:target_type_is_incompatible), mostly because it is likely to trigger with some methods in the presence of interface vars. I'll see if implementing the check makes at least a full bootstrap working. ---- Additional Comments From martin@ximian.com 2007-04-03 15:10:26 MST ---- Fixed in SVN. Unknown operating system unknown. Setting to default OS "Other".