Bug 43636

Summary: [Cycle 8] "Index was out of range. Must be non-negative and less than the size of the collection" in `System.Collections.Generic.List`1[T].set_Item()` when attempting to compile certain C# code involving tasks, async/await, and try/catch/finally
Product: [Mono] Compilers Reporter: Brendan Zagaeski (Xamarin Support) <brendan.zagaeski>
Component: C#Assignee: Marek Safar <masafa>
Status: VERIFIED FIXED    
Severity: critical CC: adam, manuel, mono-bugs+mono, nitescua, ruben.macias, shrutis, towang
Priority: ---    
Version: 4.6.0 (C8)   
Target Milestone: 4.6.0 (C8)   
Hardware: PC   
OS: Mac OS   
Tags: BZRC8B1_C7SR1S1 Is this bug a regression?: Yes
Last known good build: Mono 4.4.2 (Cycle 7 – Service Release 1)

Description Brendan Zagaeski (Xamarin Support) 2016-08-22 21:47:34 UTC
[Cycle 8] "Index was out of range. Must be non-negative and less than the size of the collection" in `System.Collections.Generic.List`1[T].set_Item()` when attempting to compile certain C# code involving tasks, async/await, and try/catch/finally




## Regression status: regression in Mono 4.6 (Cycle 8)

BAD:  Mono 4.6.0 (mono-4.6.0-branch/d0fc1a6)
GOOD: Mono 4.4.2 (mono-4.4.0-branch-c7sr1/f72fe45)




## Steps to replicate

Attempt to build the following code, for example directly on the command line via `mcs`:

mono /Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.5/mcs.exe /target:library /out:MyLibrary.dll MyLibrary.cs

* * *

using System;
using System.Threading.Tasks;

namespace MyLibrary
{
    public class Class1
    {
    }

    public class Class2
    {
        public Task Foo()
        {
            throw new NotImplementedException();
        }

        private async Task<Class1> Bar()
        {
            Task<Class1> task = null;

            try
            {
                try
                {
                    await Foo();
                }
                catch { }

                return await task;
            }
            catch (Exception e)
            {
                try
                {
                    await Foo();
                }
                catch { }

                throw;
            }
            finally
            {
            }
        }
    }
}




## BAD Results (on Mono 4.6.0)

> Unhandled Exception:
> Mono.CSharp.InternalErrorException: MyLibrary.cs(18,9): MyLibrary.Class2.<Bar>c__async0.MoveNext() ---> Mono.CSharp.InternalErrorException: (1,1): ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
> Parameter name: index
>   at System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00011] in <8cd55ece525b4760b63de40980e005aa>:0 
>   at System.ThrowHelper.ThrowArgumentOutOfRangeException () [0x00000] in <8cd55ece525b4760b63de40980e005aa>:0 
>   at System.Collections.Generic.List`1[T].set_Item (System.Int32 index, T value) [0x0000c] in <8cd55ece525b4760b63de40980e005aa>:0 
>   at IKVM.Reflection.Emit.ILGenerator.MarkLabel (IKVM.Reflection.Emit.Label loc) [0x00018] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.EmitContext.MarkLabel (IKVM.Reflection.Emit.Label label) [0x00000] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.AwaitStatement.EmitPrologue (Mono.CSharp.EmitContext ec) [0x0018c] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Await.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, System.Boolean leave_copy, System.Boolean prepare_for_load) [0x0007c] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Return.DoEmit (Mono.CSharp.EmitContext ec) [0x0005a] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x0000d] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00013] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00050] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.TryCatch.DoEmit (Mono.CSharp.EmitContext ec) [0x00012] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x0000d] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.TryFinally.EmitTryBody (Mono.CSharp.EmitContext ec) [0x00092] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.TryFinallyBlock.DoEmit (Mono.CSharp.EmitContext ec) [0x00007] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x0000d] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00013] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00050] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.ParametersBlock.EmitEmbedded (Mono.CSharp.EmitContext ec) [0x00036] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.StateMachineInitializer.EmitMoveNext (Mono.CSharp.EmitContext ec) [0x001b4] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.StateMachineInitializer+MoveNextBodyStatement.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.StateMachineInitializer+MoveNextBodyStatement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00013] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00050] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.ParametersBlock.Emit (Mono.CSharp.EmitContext ec) [0x00036] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00045] in <65ad872b0e564119b94b07e7497063eb>:0 
>    --- End of inner exception stack trace ---
>   at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x000df] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.MethodData.Emit (Mono.CSharp.TypeDefinition parent) [0x00092] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.MethodOrOperator.Emit () [0x001d0] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Method.Emit () [0x0012c] in <65ad872b0e564119b94b07e7497063eb>:0 
>    --- End of inner exception stack trace ---
>   at Mono.CSharp.Method.Emit () [0x00141] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.TypeDefinition.Emit () [0x0026e] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.ClassOrStruct.Emit () [0x00025] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.TypeDefinition.Emit () [0x0026e] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.ClassOrStruct.Emit () [0x00025] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Class.Emit () [0x00000] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.TypeDefinition.EmitContainer () [0x0000f] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.TypeContainer.EmitContainer () [0x0001e] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.NamespaceContainer.EmitContainer () [0x00007] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.TypeContainer.EmitContainer () [0x0001e] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.NamespaceContainer.EmitContainer () [0x00007] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.TypeContainer.EmitContainer () [0x0001e] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.ModuleContainer.EmitContainer () [0x0009c] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.AssemblyDefinition.Emit () [0x00098] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.AssemblyDefinitionStatic.Emit () [0x00126] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Driver.Compile () [0x002f3] in <65ad872b0e564119b94b07e7497063eb>:0 
>   at Mono.CSharp.Driver.Main (System.String[] args) [0x00052] in <65ad872b0e564119b94b07e7497063eb>:0 



## GOOD Results (with Mono 4.4.2)

The compilation completes successfully.




## Additional environment info (brief)

OS X 10.11.6
Comment 1 Brendan Zagaeski (Xamarin Support) 2016-08-22 21:49:49 UTC
*** Bug 43629 has been marked as a duplicate of this bug. ***
Comment 2 Marek Safar 2016-08-25 16:47:15 UTC
Fixed in master and Mono 4.6

Brendan, thanks for the test!
Comment 4 Marek Safar 2016-08-26 08:39:50 UTC
*** Bug 43746 has been marked as a duplicate of this bug. ***
Comment 5 Marek Safar 2016-08-26 08:41:19 UTC
*** Bug 43724 has been marked as a duplicate of this bug. ***
Comment 6 Andrei.N 2017-05-19 19:55:57 UTC
I just got the same issue and I'm using Android 7.1


[0:] EXCEPTION: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  at System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00000] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/throwhelper.cs:93 
  at System.ThrowHelper.ThrowArgumentOutOfRangeException () [0x00000] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/throwhelper.cs:56 
  at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x0000c] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/collections/generic/list.cs:181 
  at Xamarin.Forms.Application+NavigationImpl+<OnPopModal>d__2.MoveNext () [0x0000d] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Application.cs:317 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 
  at Prism.Navigation.PageNavigationService+<GoBackAsync>d__11.MoveNext () [0x00188] in <7a32c28f35024e719a93e31b4d1e3d2a>:0 . Priority: High. Timestamp:2017-05-19 22:49:11Z
05-19 22:49:11.174 I/Choreographer( 2336): Skipped 3161 frames!  The application may be doing too much work on its main thread.
05-19 22:49:11.175 D/Mono    ( 2336): [0x972e5600] worker starting
05-19 22:49:11.175 D/Mono    ( 2336): [0x94bb4a80] worker starting
Comment 7 Andrei.N 2017-05-19 19:56:38 UTC
I mean I'm using Xamarin Android 7.1, Visual Studio 2015.
Comment 8 Andrei.N 2017-05-19 19:57:09 UTC
Any workarounds?