Bug 3627 - [aot] System.ExecutionException - Attempting to JIT compile method ;System.Linq.Enumerable:<ToLookup `2>
Summary: [aot] System.ExecutionException - Attempting to JIT compile method ;System.Li...
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 1.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-02-23 21:08 UTC by siraleigh2012
Modified: 2012-03-07 16:57 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Sample solution to reproduce the reported error (5.40 MB, application/octet-stream)
2012-02-28 17:12 UTC, siraleigh2012

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description siraleigh2012 2012-02-23 21:08:26 UTC
Trying to execute the following line on an iPhone in Debug mode (works fine in the emulator):
rows = this.QueryAllRows().ToList();

Attempting to JIT compile method 'System.Linq.Enumerable:<ToLookup`2>m__5F<masterdetail.PersonDetail, int> (masterdetail.PersonDetail)' while running with --aot-only.

public void RefreshData()
	rows = this.QueryAllRows().ToList();
public IEnumerable<CounselingWithPerson> QueryAllRows ()
	return (from wqual in _db.Table<Counseling>()
	        join personTable in _db.Table<PersonDetail>() on wqual.FK_PERSON equals personTable.Id
                     where personTable.RecordType > 0 
                     orderby wqual.InitialCounselingDate descending
                     select new CounselingWithPerson(wqual, personTable));	
Comment 1 siraleigh2012 2012-02-24 11:27:43 UTC
Version info:
	Mono 2.10.8 (tarball Mon Dec 19 17:43:18 EST 2011)
	GTK 2.24.5
	GTK# (
Apple Developer Tools:
	 Xcode 4.3 (1175)
	 Build 4E109
Monotouch: 5.2.5
Mono for Android not installed
Build information:
	Release ID: 20806005
	Git revision: 96a2067d048f7e91f9515c869f214243f1926953
	Build date: 2012-02-17 03:31:11+0000
Operating System:
	Mac OS X 10.7.3
Comment 2 Zoltan Varga 2012-02-27 12:16:35 UTC
-> mt.
Comment 3 Sebastien Pouliot 2012-02-28 15:05:02 UTC
You're hitting one of the generic limitations that occurs when using the AOT compiler. See http://docs.xamarin.com/ios/about/limitations#Limited_Generics_Support

The most common causes are the use of value types (which can sometime be changed to classes), the use of generic virtual methods (which LINQ often use with using "order by").

We can often suggest workarounds for them but we need to be able to reproduce it first. That requires a test case that contains the type definitions (e.g. CounselingWithPerson, Counseling, PersonDetail...) so we can reproduce the error(s) ourselves.
Comment 4 siraleigh2012 2012-02-28 17:12:48 UTC
Created attachment 1430 [details]
Sample solution to reproduce the reported error

The attached project will run fine in the simulator, but throws the JIT exception when run on a device.
Comment 5 Sebastien Pouliot 2012-02-28 18:09:59 UTC
Thanks. I can duplicate the issue and will look into it asap.

Note that it works on the simulator because the JIT is being used in that case (it's possible on OSX and provides much faster "edit-build-debug" cycles than using AOT would allow).
Comment 6 Sebastien Pouliot 2012-02-29 09:06:57 UTC
some details (mostly for me/xamarin to avoid duplicating this)

Attempting to JIT compile method 'System.Linq.Enumerable:<ToLookup`2>m__5A<JitError.PersonDetail, int> (JitError.PersonDetail)' while running with --aot-only.

Enumerable.cs: line #2966

    list.Add (elementSelector (element));

list is: List<PersonDetail>
elementSelector is: Func<PersonDetail,PersonDetail>
element is: PersonDetail
PersonDetail is a class (not a value type)

[ERROR] FATAL UNHANDLED EXCEPTION: System.ExecutionEngineException: Attempting to JIT compile method 'System.Linq.Enumerable:<ToLookup`2>m__5A<JitError.PersonDetail, int> (JitError.PersonDetail)' while running with --aot-only.

  at System.Linq.Enumerable.ToLookup[PersonDetail,Int32,PersonDetail] (IEnumerable`1 source, System.Func`2 keySelector, System.Func`2 elementSelector, IEqualityComparer`1 comparer) [0x00079] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:2966 
  at System.Linq.Enumerable.ToLookup[PersonDetail,Int32] (IEnumerable`1 source, System.Func`2 keySelector, IEqualityComparer`1 comparer) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:2934 
  at System.Linq.Enumerable+<CreateJoinIterator>c__Iterator18`4[JitError.Counseling,JitError.PersonDetail,System.Int32,<>__AnonType0`2[JitError.Counseling,JitError.PersonDetail]].MoveNext () [0x00023] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:1157 
  at System.Collections.Generic.List`1[<>__AnonType0`2[JitError.Counseling,JitError.PersonDetail]].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[<>__AnonType0`2[JitError.Counseling,JitError.PersonDetail]]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.ToArray[<>__AnonType0`2] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1[<>__AnonType0`2[JitError.Counseling,JitError.PersonDetail]]..ctor (IEnumerable`1 source, System.Linq.SortContext`1 context) [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1+<Sort>c__Iterator2F[<>__AnonType0`2[JitError.Counseling,JitError.PersonDetail]].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator1D`2[<>__AnonType0`2[JitError.Counseling,JitError.PersonDetail],JitError.CounselingWithPerson].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.Count[CounselingWithPerson] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
  at JitError.JitErrorViewController.btnClick (MonoTouch.Foundation.NSObject sender) [0x0000f] in /Users/poupou/Downloads/JitError/JitError/JitErrorViewController.cs:43 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
  at JitError.Application.Main (System.String[] args) [0x00000] in /Users/poupou/Downloads/JitError/JitError/Main.cs:17
Comment 7 Sebastien Pouliot 2012-02-29 09:53:12 UTC
The issue is with the (deep internal) use of value types from the LINQ query (the previous stack traces give an idea).

You can workaround this by changing your query to avoid this, e.g. change

            join personTable in _db.Table<PersonDetail>() on wqual.FK_PERSON equals personTable.Id


            join personTable in _db.Table<PersonDetail>() on wqual.FK_PERSON.ToString() equals personTable.Id.ToString()

that will change the generated-generic code to use a class (string) instead of a value-type (int). That in turn allows the AOT compiler to generate the required code (since all generic-signatures using classes can be shared) and your code to run correctly (if not optimal) on devices.
Comment 8 Sebastien Pouliot 2012-03-01 15:36:41 UTC
Both the original and the workarounds where added to our test suite. We'll be able to update it when any change occurs.
Comment 9 Rodrigo Kumpera 2012-03-07 16:57:33 UTC
Bug has been fixed and will be part of the 5.3 alpha.