Result of switch expression not being lifted into initializer, causing init-only properties to be set outside of initializer

オープン
#4,003 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
52/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
csharp
領域
devtools

調査の方向性

ILSpy 11.0 で提供された C# 入力を再現し、switch 式とオブジェクト初期化子の周辺に生成される C# を調べます。デコンパイラーのパターン マッチングと制御フローの処理を追跡します。結果が初期化子の内部にとどまり、後から init-only プロパティに代入されず、パターンの制御フローが正しければ完了です。

索引モデルが issue の本文から書いたものです。

説明

Bug Decompiler
Input code
namespace ConsoleApp2;

public class foo
{
    public float Health { get; set; }
    public float MaxHealth { get; set; }
    public object? Player { get; set; }
}

public class bar
{
    public float? Health { get; init; }
    public float? MaxHealth { get; init; }
}

public static class TEST
{
    public static void doThing2()
    {
        var foo_var = new foo
        {
            Health = 10f,
            MaxHealth = 10f,
            Player = new object()
        };
        var bar_var = new bar
        {
            Health = foo_var switch
            {
                { Health: 0f } or { Health: 10f, Player: not null } => null,
                _ => foo_var.Health
            },
            MaxHealth = foo_var.MaxHealth == 10.0 ? null : foo_var.MaxHealth,
        };
    }
}
Erroneous output
namespace ConsoleApp2;

public class foo
{
	public float Health { get; set; }
	public float MaxHealth { get; set; }
	public object? Player { get; set; }
}

public class bar
{
	public float? Health { get; init; }
	public float? MaxHealth { get; init; }
}

public static class TEST
{
	public static void doThing2()
	{
		foo foo_var = new foo
		{
			Health = 10f,
			MaxHealth = 10f,
			Player = new object()
		};
		bar obj = new bar
		{
			MaxHealth = (((double)foo_var.MaxHealth == 10.0) ? ((float?)null) : new float?(foo_var.MaxHealth))
		};
		float? health2;
		if (foo_var != null)
		{
			float health = foo_var.Health;
			if (health == 0f || (health == 10f && foo_var.Player != null))
			{
				health2 = null;
				goto IL_0095;
			}
		}
		health2 = foo_var.Health;
		goto IL_0095;
	IL_0095:
		obj.Health = health2; // <-- `Health` is init-only, compile error
	}
}
Details
  • Product in use: ILSpy
  • Version in use: 11.0

There are also control flow issues because it's not properly recognizing a pattern matcher here

主要言語
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 を短くまとめたダイジェスト。