Registry Mirror
Skip to content

Conversation

@dgrunwald

Copy link
Copy Markdown
Member

Follow-up to #4065.
This fixes the object/dynamic issue and the "commonType can be a type some arm does not convert to" issue, both found by Claude.

 * merge object/dynamic distinctions like we do with tuple element names. This fixes BestCommonType(object, dynamic).
 * add a test that `new[] { 1, null }` has the "best common type" = `int`. The conversion error from `null` to `int` only happens later, it's not related to the best common type computation.
…rwise our implicit conversions might end up converting to the wrong type.

This also matters in the `1 => DateTime.Now, 2 => null` case -- BestCommonType infers `DateTime` here, but we need `DateTime?` instead. But both had `StackType.O` so this went wrong prior to this commit.
@dgrunwald
dgrunwald merged commit aa00e3f into master Aug 29, 2026
17 checks passed

@siegfriedpammer siegfriedpammer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two findings from reviewing this against master (2d9f852cd), both reproduced with locally built decompilers on Linux. ICSharpCode.Decompiler.Tests is green on the branch (3505 total, 0 failed, 45 skipped).

  1. The new EquivalentTypes(commonType, resultType) check changes behaviour for tuple-typed switch expressions - System.Text.Json.JsonPath() and System.Private.CoreLib.LCIDToLocaleName among the hits.
  2. The object/dynamic merge is silently skipped when dynamic is the first bound.

Details inline.

Posted by an AI agent (Claude) on Siegfried's behalf.

if (success && commonType.GetStackType() == inst.ResultType)
// Note: we need to ensure the compiler actually picked the type that we used for the
// implicit conversions.
if (success && NormalizeTypeVisitor.TypeErasure.EquivalentTypes(commonType, resultType))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rejects every commonType that is a proper subtype of resultType, including when the conversion between them is identity/reference/boxing. Where resultType is the stack-type fallback object and the arms are tuples, the result is not equivalent to the input.

Repro (net10.0, Release):

public string JsonPath(int continuationCount)
{
    StringBuilder sb = new StringBuilder("$");
    (int frameCount, bool includeCurrent) = continuationCount switch {
        0 => (_count - 1, true),
        1 => (0, true),
        _ => (continuationCount, false)
    };
    for (int i = 0; i < frameCount; i++) sb.Append(i);
    if (includeCurrent) sb.Append('c');
    return sb.ToString();
}

master 2d9f852cd:

(int, bool) obj = continuationCount switch { ... };
int item = obj.Item1;
bool item2 = obj.Item2;

this branch 5d9cdce94:

public unsafe string JsonPath(int continuationCount)
{
    object obj = continuationCount switch { 0 => (object)(_count - 1, true), ... };
    int item = ((ValueTuple<int, bool>*)(&obj))->Item1;
    bool item2 = ((ValueTuple<int, bool>*)(&obj))->Item2;

&obj is the address of the reference, not of the boxed payload. Since C# 11 that is warning CS8500 rather than CS0208, so it still compiles. Running the original, master's output and the branch's output on the same input (_count = 5, JsonPath(0)):

original: $0123c
master:   $0123c
branch:   THREW OutOfMemoryException

Decompiling Microsoft.NETCore.App/10.0.11 with both builds, the same shape hits System.Text.Json.JsonPath() and PropertyPath() and System.Private.CoreLib.LCIDToLocaleName (all three also gain unsafe). Redundant-cast-only hits: System.Net.Http (EventCounter obj = ... becomes object obj = ... plus ((EventCounter)obj)?.WriteMetric(...)), System.Linq.Expressions, System.Private.Xml, and Newtonsoft.Json 13.0.4.

TranslateSectionBody has already run ConvertTo(resultType, allowImplicitConversion: true) on every arm, so a commonType that differs from resultType can only differ by a conversion the arms never had to spell out.

What I verified locally: keeping the old stack-type guard and adding a conversion check, instead of exact equality -

if (NormalizeTypeVisitor.TypeErasure.EquivalentTypes(commonType, resultType))
    return true;
if (commonType.GetStackType() != inst.ResultType)
    return false;
var c = resolver.conversions.ImplicitConversion(commonType, resultType);
return c.IsIdentityConversion || c.IsReferenceConversion || c.IsBoxingConversion;

removes every regression above, keeps this branch's intended improvement in System.Private.Xml (m_bLen = (byte)(x switch { 7 => 1, ... }) becomes m_bLen = x switch { 7 => (byte)1, ... }), and leaves the suite green. The GetStackType() guard is load-bearing - without it System.Private.Xml trips Debug.Assert(cexpr.Type.GetStackType() == inst.ResultType) at ExpressionBuilder.cs:205.

Posted by an AI agent (Claude) on Siegfried's behalf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants