Cast lost when ternary with mismatched branch types is hoisted to `object` local

オープン
#3,736 コメント 3 件 リアクション 1 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@siegfriedpammer がすでに取り組んでいます。

2026年8月27日 から。

評価

この issue はまだ評価されていません。

説明

Bug Feedback Needed

When a ?:'s two branches have different stack types and ILSpy can't find a common type tighter than object, it hoists the conditional into an if/else with an object obj local — but drops the explicit cast that was present in the source. The dropped cast can re-bind to a different overload on recompile.

Repro
using System.Collections.Generic;
using System.Linq;

public class Repro
{
    public IEnumerator<byte> Bug(byte[] data, bool reversed)
        => (reversed ? ((IEnumerable<byte>)data).Reverse() : (IEnumerable<byte>)data).GetEnumerator();
}
ilspycmd 10.0.1.8346 output
public IEnumerator<byte> Bug(byte[] data, bool reversed)
{
    object obj;
    if (!reversed)
    {
        obj = data;
    }
    else
    {
        obj = data.Reverse();  // (IEnumerable<byte>) cast lost
    }
    return ((IEnumerable<byte>)obj).GetEnumerator();
}

When a project-local void Reverse(this byte[]) (or similar shadowing extension) is in scope, data.Reverse() re-binds to it on recompile → CS0029 ("Cannot implicitly convert type 'void' to 'object'"). Even without a shadow, the decompile is semantically lossy.

dotPeek emits the inline ternary form with both casts preserved.

Related

#1501, #1614, #3322, #3464, #793 are all in the same family (ILSpy dropping a needed cast); none is exactly this ?:→if/else hoist variant.

主要言語
C#
スター
27.1k
フォーク
3.8k
平均マージ
1日 23時間
マージ済み PR(30日)
44

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

icsharpcode/ILSpy のほかの issue

icsharpcode/ILSpy の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。