JIT: (bug) comparing `GetType()` of two references drops the null check on a null reference
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Description
## Repro
```csharp
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
```
NullReferenceException
```
## Actual
```
returned 3
```
`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 (ef97eb54da2).
Contributor guide
Assessment
This issue has not been assessed yet.