Bug 21893

Summary: Calling IList<enum>.Contains() throws an InvalidCastException
Product: iOS Reporter: Greg Shackles <gshackles>
Component: XI runtimeAssignee: Zoltan Varga <vargaz>
Status: VERIFIED FIXED    
Severity: normal CC: gouri.kumari, mono-bugs+monotouch, Rajneeshk, sebastien, steve
Priority: Normal    
Version: 7.2.6   
Target Milestone: 7.4.0   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description Greg Shackles 2014-08-06 18:48:08 UTC
With the following code:

---

public override void ViewDidLoad()
{
	base.ViewDidLoad();

	// this works: List<MyEnum> _myValues = new List<MyEnum> { MyEnum.AValue };
	IList<MyEnum> _myValues = new List<MyEnum> { MyEnum.AValue }; // this doesn't

	bool pleaseDontCrash = _myValues.Contains(MyEnum.AnotherValue);
}

enum MyEnum
{
	AValue,
	AnotherValue
}

---

When running in the simulator all is fine, but on a device this will throw the following exception:

2014-08-06 18:44:21.812 EnumContainsBugRepro[24026:60b] Unhandled managed exception: Unable to cast object of type 'EnumContainsBugRepro.EnumContainsBugReproViewController+MyEnum' to type 'System.Int32'. (System.InvalidCastException)
  at (wrapper managed-to-native) object:__icall_wrapper_mono_object_castclass_unbox (object,intptr)
  at System.Array.UnsafeMov[MyEnum,Int32] (MyEnum instance) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Array.cs:3170 
  at (wrapper unknown) object:gsharedvt_in ()
  at (wrapper unknown) object:gsharedvt_out ()
  at System.Collections.Generic.EnumIntEqualityComparer`1[EnumContainsBugRepro.EnumContainsBugReproViewController+MyEnum].IndexOf (MyEnum[] array, MyEnum value, Int32 startIndex, Int32 endIndex) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs:200 
  at (wrapper unknown) object:gsharedvt_in ()
  at (wrapper unknown) object:gsharedvt_out ()
  at System.Array.IndexOf[MyEnum] (MyEnum[] array, MyEnum value, Int32 startIndex, Int32 count) [0x0003c] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Array.cs:3031 
  at (wrapper unknown) object:gsharedvt_in ()
  at (wrapper unknown) object:gsharedvt_out ()
  at System.Collections.Generic.List`1[EnumContainsBugRepro.EnumContainsBugReproViewController+MyEnum].Contains (MyEnum item) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/List.cs:193 
  at (wrapper unknown) object:gsharedvt_in ()
  at EnumContainsBugRepro.EnumContainsBugReproViewController.ViewDidLoad () [0x00018] in /Users/greg/Projects/EnumContainsBugRepro/EnumContainsBugRepro/EnumContainsBugReproViewController.cs:23 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:39 
  at EnumContainsBugRepro.Application.Main (System.String[] args) [0x00008] in /Users/greg/Projects/EnumContainsBugRepro/EnumContainsBugRepro/Main.cs:17 

---

If I change the IList<> to a List<>, things work as expected. I just started running into this bug so it must have been introduced in a recent update, as this code has been working fine previously.

Here's my version info:

Xamarin Studio
Version 5.2 (build 386)
Installation UUID: 170d1faa-5368-4a06-a157-d043c894d176
Runtime:
	Mono 3.6.0 ((no/f540f8a)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 306000039

Apple Developer Tools
Xcode 5.1.1 (5085)
Build 5B1008

Xamarin.iOS
Version: 7.2.6.28 (Business Edition)
Hash: 606f31a
Branch: 
Build date: 2014-08-01 15:27:48-0400
Comment 1 Greg Shackles 2014-08-06 20:19:03 UTC
I can confirm that the crash is not reproducible on:

Xamarin Studio
Version 5.1.4 (build 0)
Installation UUID: 873a8c88-365b-4f18-860e-6b6f44da7bb4
Runtime:
	Mono 3.4.0 ((no/954ed3c)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000214

Xamarin.iOS
Version: 7.2.5.5 (Enterprise Edition)
Hash: 87026b6
Branch: 
Build date: 2014-07-02 19:39:08-0400
Comment 2 Sebastien Pouliot 2014-08-06 20:47:51 UTC
Thanks for distilling a small test case!

I can reproduce the issue with our (internal) 7.4.0 branch.

2014-08-06 20:44:20.835 dontlink[1824:60b] 	[FAIL] Bug21893 : System.InvalidCastException : Unable to cast object of type 'MonoTouchFixtures.DontLinkRegressionTests+MyEnum' to type 'System.Int32'.
2014-08-06 20:44:20.838 dontlink[1824:60b] 		  at (wrapper managed-to-native) object:__icall_wrapper_mono_object_castclass_unbox (object,intptr)
2014-08-06 20:44:20.840 dontlink[1824:60b] 		  at System.Array.UnsafeMov[MyEnum,Int32] (MyEnum instance) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Array.cs:3170 
2014-08-06 20:44:20.842 dontlink[1824:60b] 		  at (wrapper unknown) object:gsharedvt_in ()
2014-08-06 20:44:20.844 dontlink[1824:60b] 		  at (wrapper unknown) object:gsharedvt_out ()
2014-08-06 20:44:20.846 dontlink[1824:60b] 		  at System.Collections.Generic.EnumIntEqualityComparer`1[MonoTouchFixtures.DontLinkRegressionTests+MyEnum].IndexOf (MyEnum[] array, MyEnum value, Int32 startIndex, Int32 endIndex) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs:200 
2014-08-06 20:44:20.848 dontlink[1824:60b] 		  at (wrapper unknown) object:gsharedvt_in ()
2014-08-06 20:44:20.850 dontlink[1824:60b] 		  at (wrapper unknown) object:gsharedvt_out ()
2014-08-06 20:44:20.851 dontlink[1824:60b] 		  at System.Array.IndexOf[MyEnum] (MyEnum[] array, MyEnum value, Int32 startIndex, Int32 count) [0x0003c] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Array.cs:3031 
2014-08-06 20:44:20.852 dontlink[1824:60b] 		  at (wrapper unknown) object:gsharedvt_in ()
2014-08-06 20:44:20.854 dontlink[1824:60b] 		  at (wrapper unknown) object:gsharedvt_out ()
2014-08-06 20:44:20.855 dontlink[1824:60b] 		  at System.Collections.Generic.List`1[MonoTouchFixtures.DontLinkRegressionTests+MyEnum].Contains (MyEnum item) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/List.cs:193 
2014-08-06 20:44:20.856 dontlink[1824:60b] 		  at (wrapper unknown) object:gsharedvt_in ()
2014-08-06 20:44:20.857 dontlink[1824:60b] 		  at MonoTouchFixtures.DontLinkRegressionTests.Bug21893 () [0x00012] in /Developer/MonoTouch/Source/monotouch/tests/dontlink/DontLinkRegressionTests.cs:160 
2014-08-06 20:44:20.858 dontlink[1824:60b] 		  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
2014-08-06 20:44:20.860 dontlink[1824:60b] 		  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00044] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:230 

Something must have regressed with the gsharedvt support -> Zoltan
Comment 3 Zoltan Varga 2014-08-07 07:58:10 UTC
Fixed in mono master 0f1a427b6704f3b66c8fb53dca8dae6d178e6ba2 and bumped mt to it in 23eb35fe35c6c85daff3d8b08907c1d019292d5a.
Comment 4 Greg Shackles 2014-08-07 08:01:45 UTC
Thanks for the quick fix! Any sense of when a fix will make it out?

Alternatively, do you know which version it was introduced in?
Comment 5 Sebastien Pouliot 2014-08-07 09:44:10 UTC
@Zoltan The branch for (mono) 3.8 and XI (7.4) are not yet frozen (and that's a regression) can you backport it to mono-3.8-branch so I can bump XI ?
Comment 6 Zoltan Varga 2014-08-07 09:52:23 UTC
Backported this and the fix for #21882 as 1a2c7216c1ed5c0e8e6852fe23cba48db1106a58.
Comment 7 Sebastien Pouliot 2014-08-07 10:14:03 UTC
monotouch-7.4.0-branch af3beabe5648bc172707e96e44e6e1bf6e214719

@Greg, it will be part of 7.4 which is planned for the end of the month (with beta sooner than that).
Comment 8 Sebastien Pouliot 2014-08-07 11:26:17 UTC
QA: unit test added in

* master 1a713c2ecc30c65ac7ff9c48e135a3fabe451a02
* monotouch-7.4.0-branch ed4257c221b46e7c125e2a50e4ce88d5b7258d15
Comment 9 Steve Gordon 2014-08-13 17:17:44 UTC
This bug also surfaces when using the ObservableCollection<T>.Contains() when T is an enum.
Comment 10 Sebastien Pouliot 2014-08-13 17:19:56 UTC
XI 7.4 was just released into beta. You might want to validate that this also solve your issue (just in case).
Comment 11 GouriKumari 2014-08-19 12:27:29 UTC
Verified this bug with build version: 7.4.0.102 (Enterprise Edition), Hash:
141adb9.

Unit test  "Aot_Gsharedvt_21893", added to linksdk regression test passed on device.


Test Log:
https://wrench.internalx.com/Wrench/WebServices/Download.aspx?workfile_id=3281406