dotnet/roslyn

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

Open

#81365 opened on Nov 20, 2025

View on GitHub
 (0 comments) (0 reactions) (0 assignees)C# (20,414 stars) (4,257 forks)batch import
Area-CompilersConcept-Design Debthelp wanted

Description

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

Contributor guide