Repro
using System;
using System.Runtime.CompilerServices;
internal static class Program
{
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static int Test(string a, string b)
{
if (a.GetType() != b.GetType())
return 2;
return 3;
}
private static void Main()
{
try
{
Console.WriteLine($"returned {Test(null, "text")}");
}
catch (NullReferenceException)
{
Console.WriteLine("NullReferenceException");
}
}
}
Expected
Actual
a is null, so a.GetType() must throw, but the whole method body is optimized away to
return 3.
Platform
Windows x64, local Checked build of dotnet/runtime main (ef97eb5).
Repro
Expected
Actual
aisnull, soa.GetType()must throw, but the whole method body is optimized away toreturn 3.Platform
Windows x64, local Checked build of dotnet/runtime main (ef97eb5).