csharplang
csharplang copied to clipboard
Bug in control flow analysis for out arguments (VS 16.8, .NET 5)
Looks like the C# compiler have a bug in control flow analysis
// Project1.csproj
using Library;
namespace ControlFlowError
{
public sealed class ValueProvider
{
public bool TryGetValue(out Result result)
{
// Expected: The out parameter 'result' must be assigned to before control leaves the current method
return false;
}
}
}
// Project2.csproj
namespace Library
{
public readonly struct Result
{
public object Value { get; }
//public readonly object Value; // 2. Works as expected
//public readonly int Value; // 3. Works as expected
}
}
Steps to Reproduce: Clone the project https://github.com/rameel/ControlFlowError
Expected Behavior: Compilation error with the message: The out parameter 'result' must be assigned to before control leaves the current method
Actual Behavior: Compiled successfully
This is by design (the struct has no accessible fields), but I believe we want to change the design.
Ah, got it
Also reported at https://github.com/dotnet/roslyn/issues/39062 and many other places.