dotnet/roslyn

Unnecessary defensive copies on enums passed by `in`

Open

#72,016 创建于 2024年2月9日

在 GitHub 查看
 (4 评论) (0 反应) (0 负责人)C# (4,257 fork)batch import
Area-CompilersBughelp wanted

仓库指标

Star
 (20,414 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 256 个 PR)

描述

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.

贡献者指南