Repository: dotnet/roslyn

Issue #44720: System.BadImageFormatException when execute the local generic method
Created: 2020-05-30T13:24:02Z
The application was already running, however I got System.BadImageFormatException during execute local method with generic argument. Crashed on:

.NET Core 3.1.300 x64
.NET Framework 4.8.03752 x64

The example to reproduce the issue CompilerBug.zip:
using System;
using System.Collections.Generic;
using System.Linq;

namespace CompilerBug
{
    internal static class Program
    {
        // Inline the method in `main()` fix the issue
        private static List<object> Create(DataConfig dataConfig, string name)
        {
            var tasks = new List<object>();
            foreach (var fieldInfo in typeof(DataConfig).GetFields())
            {
                // Move the method out of `foreach` fix the issue
                IReadOnlyCollection<TResult> Filter<TResult>(IEnumerable<KeyValuePair<string, TResult>> value)
                    where TResult : BaseConfig
                {
                    return value.Where(y => y.Key == name).Select(y => y.Value).ToList();
                }

                object CreateObject()
                {
                    if (fieldInfo.GetValue(dataConfig) is KeyValuePair<string, Config>[] value)
                        return new ConfigObject(Filter(value));
                    throw new NotSupportedException();
                }

                tasks.Add(CreateObject());
            }

            return tasks;
        }

        private static void Main()
        {
            const string key = "key";
            GC.KeepAlive(Create(new DataConfig {Config = new[] {new KeyValuePair<string, Config>(key, new Config())}}, key));
        }

        private abstract class BaseConfig
        {
        }

        private sealed class Config : BaseConfig
        {
        }

        private sealed class DataConfig
        {
            public KeyValuePair<string, Config>[] Config;
        }

        private sealed class ConfigObject
        {
            public ConfigObject(IReadOnlyCollection<Config> filter)
            {
            }
        }
    }
}

Duplicate issue #49599: BUG: Incorect code-gen for local methods along with generics and dynamic
Created: 2020-11-24T16:14:31Z
Version Used: VS Pro 16.8.2 (targeting .NET Core 3.1 or .NET 5)
Steps to Reproduce:
Compile and run:
public class Program
{
    static void Main()
    {
        Local1<object>();

        static void Local1<T>()
        {
            Local3();

            static int Local2(dynamic n) => (int)n;
        }

        static int Local3() => (int)(dynamic)4;
    }
}
Expected Behavior:
Program runs successfully.
Actual Behavior:
Fails with: System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format.'.
Program runs successfully when at least one of following is true:

Generic parameter T is removed.
Definition of Local2 is removed.
Definition of Local2 is placed next to definition of Local3.
Definition of Local3 is placed next to definition of Local2.
Local2 does not take 'dynamic' as param.
Local3 does not use 'dynamic'.

Closing comment (2020-11-24T18:50:57Z): Possibly related to #44720, but since it reproduces in situations where only 'static' local functions are used, it seems unlikely..?