dotnet/roslyn

Target-typed expressions with nested elements/branches don't get converted when one of nested expressions is directly an error

Open

#81,365 创建于 2025年11月20日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)C# (20,414 star) (4,257 fork)batch import
Area-CompilersConcept-Design Debthelp wanted

描述

Discovered in https://github.com/dotnet/roslyn/pull/80907

Steps to Reproduce:

class C
{
    public void M(bool b, int i)
    {
        C[] cs = [a, default];

        C c1 = b ? a : default;

        C c2 = i switch
        {
            1 => a,
            _ => default,
        };

        (C, int) t = (a, default);
    }
}

Hover over default literal in each expression

Expected Behavior: I get type information about the literal

Actual Behavior: Nothing is shown

Breakdown: The underlying issue lies in the compiler layer. Althouth in each case target type can be determined, expressions don't get converted to it due to one of nested elements/expressions being directly an error. If an error isn't directly in the element/branch, e.g. if I change collection expression to [new(a), default] even though the first element still contains an error, types start to flow through, so I get correct type information both on new and default. The same applies to other examples as well.

There is theoretical IDE impact, but I believe it is extreamly rare to see it in practical applications

贡献者指南

Target-typed expressions with nested elements/branches don't get converted when one of nested expressions is directly an error · dotnet/roslyn#81365 | Good First Issue