dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.93k stars 4.02k forks source link

Implicit struct initialization: handle fields at any depth? #59890

Open RikkiGibson opened 2 years ago

RikkiGibson commented 2 years ago

The current implementation of implicit struct initialization in #59788 only initializes fields directly contained in 'this', but it's possible that it's more optimal to initialize nested fields in some scenarios.

SharpLab

using System;

public struct Pair
{
    public int X, Y;
}

public struct S {
    public Pair Pair;

    public S()
    {
        // Here we insert `Pair = default;` but we could perhaps insert `Pair.Y = default;`
        Pair.X = 42;
    }
}
jcouv commented 2 years ago

From chat with Rikki, triaged this into Compiler.Next milestone. Let's do a quick test/benchmark. Depending on the evaluation, we can address or close.