Version Used: Visual Studio 15.9.04012.0. File -> New -> Project -> Console (DotNet Core)
Steps to Reproduce:
Paste the following code:
using System;
using System.Collections.Generic;
namespace CompilerBugZero
{
class Program
{
static void Main(string[] args)
{
var hdl = FindHandle();
if (IntPtr.Zero == hdl)
{
throw new InvalidOperationException();
}
}
static IntPtr FindHandle()
{
var candidates = new List<(IntPtr hwnd, string title, string className)>();
candidates.Add((new IntPtr(123), "title", "className"));
if (candidates.Count == 1)
{
// Breakpoint here
return candidates[0].hwnd;
}
throw new InvalidOperationException();
}
}
}
Set a breakpoint the line below // Breakpoint here
See how everything is Zero when it shouldn't:

Note how the list still has the correct value but accessing [0] returns a zeroed tuple apparently.
Expected Behavior:
Debugger shows the correct values. Code works at runtime.
Actual Behavior:
Debugger doesn't show correct value. In fact in our real application I feel like we had the wrong behavior (ie IntPtr.Zero) at runtime. In the real application this happened in a PInvoke scenario. However, I couldn't reproduce this here. This might be a wrong positive, but worth considering when investigating this.
Workaround:
Change the code to
if (candidates.Count == 1)
{
// Breakpoint here
- return candidates[0].hwnd;
+ var t = candidates[0];
+ return t.hwnd;
}
I confirm this reproduces and it's likely bug in the EE. Very odd.
Looks like:
https://github.com/dotnet/roslyn/issues/29951
https://github.com/dotnet/roslyn/issues/34155
It eventually ended up here:
@0xd4d Thanks for tracking it down. Closing as a dup of dotnet/coreclr#25519.
Most helpful comment
Looks like:
https://github.com/dotnet/roslyn/issues/29951
https://github.com/dotnet/roslyn/issues/34155
It eventually ended up here:
https://github.com/dotnet/coreclr/issues/25519