dotnet/roslyn

Unnecessary defensive copies on enums passed by `in`

Open

#72.016 aperta il 9 feb 2024

Vedi su GitHub
 (4 commenti) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-CompilersBughelp wanted

Metriche repository

Star
 (20.414 star)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

The compiler emits unnecessary defensive copies on enums passed by in.

Version Used:

Sharplab.io Also reproduces with the compiler included in the .NET 8.0.100 SDK

Steps to Reproduce:

using System;
public class C
{
    public void M<T1, T2>(in Enum1 value1, ref Enum1 value2, Enum1 value3, in T1 value4, in T2 value5) where T1 : Enum where T2 : struct, Enum
    {
        value1.ToString(); //defensive copy (unnecessary)
        value2.ToString(); //no defensive copy
        value3.ToString(); //no defensive copy
        value4.ToString(); //defensive copy (unnecessary)
        value5.ToString(); //defensive copy (unnecessary)
    }
}
public enum Enum1
{
}

Link

Expected Behavior:

None of the calls to .ToString() emit a defensive copy, since none is required for any of them.

Actual Behavior:

value1.ToString() emits a defensive copy, and similar for value4 and value5.

Guida contributor