Query let clause whose value is a switch expression decompiles to <>h__TransparentIdentifier0 (and if (1 == 0) { } in Debug builds)

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

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

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

評価

難易度
4/5
見積もり時間
1〜2日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp

調査の方向性

Start with the query expression transform that recognizes transparent identifiers; the issue does not name its source file or a regression test. Trace how it handles the Select lambda when the body contains a generated local before the anonymous object creation, including Debug sequence-point statements. Done when the let is restored for the supplied Debug and Release repros and the decompiled output preserves the query semantics.

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

説明

AI
Version

ICSharpCode.Decompiler 11.1.0.9782 (NuGet), default settings (C# language version: latest). Input compiled with the .NET 10 SDK (C# 14), net10.0.

Repro
using System;
using System.Linq;

static class Program {
	static void Main() {
		int[] values = { 1, 2, 5 };
		var query = from v in values
					let size = v switch {
						1 => "One",
						< 4 => "Few",
						_ => "Many"
					}
					where size != "Few"
					select new { v, size };
		foreach (var x in query)
			Console.WriteLine(x);
	}
}

Build it (dotnet build -c Release and -c Debug) and decompile Program.

Expected

The let clause is kept, as it is when the let value is a plain expression:

var query = from v in values
	let size = v switch { 1 => "One", < 4 => "Few", _ => "Many" }
	where size != "Few"
	select new { v, size };

(or let size = (v >= 4) ? "Many" : ((v != 1) ? "Few" : "One") if the switch expression isn't restored.)

Actual

Release build:

foreach (var item in from <>h__TransparentIdentifier0 in new int[3] { 1, 2, 5 }.Select((int v) =>
	{
		string size = ((v >= 4) ? "Many" : ((v != 1) ? "Few" : "One"));
		return new { v, size };
	})
	where <>h__TransparentIdentifier0.size != "Few"
	select new { <>h__TransparentIdentifier0.v, <>h__TransparentIdentifier0.size })
{
	Console.WriteLine(item);
}

Debug build (also has two empty if (1 == 0) { } statements):

var query = from <>h__TransparentIdentifier0 in values.Select((int v) =>
	{
		if (1 == 0)
		{
		}
		string size = ((v >= 4) ? "Many" : ((v != 1) ? "Few" : "One"));
		if (1 == 0)
		{
		}
		return new { v, size };
	})
	where <>h__TransparentIdentifier0.size != "Few"
	select new { <>h__TransparentIdentifier0.v, <>h__TransparentIdentifier0.size };

The output compiles, but <>h__TransparentIdentifier0 is the compiler's name, the let is gone, and the Select lambda is a statement block. With several lets, joins and group ... into (a common reporting query) the chain of transparent identifiers gets long and hard to read.

Analysis

The compiler stores the switch expression's value in a temporary before it creates the transparent identifier's anonymous object, so the Select lambda isn't a single new { v, size } expression but { string size = <switch>; return new { v, size }; }. In Debug builds the compiler also emits the if (1 == 0) sequence-point helpers around the switch. The query expression transform only recognizes a transparent identifier when the lambda body is the anonymous object creation itself, so it doesn't turn this lambda into a let and leaves the transparent identifier in the query.

Plain let values (no switch expression) are fine, so the switch expression's temporary seems to be what stops the pattern from matching.

Possible fix

Before the query expression transform (or in it), accept a lambda body of the form

{ T a = <expr>; ...; return new { x, a, ... }; }

when the locals have generated names, are each used once, in order, inside the anonymous object creation, by inlining them into the creation, and remove the empty if (1 == 0) { } statements first. dnSpy does this as an AST transform that runs before IntroduceQueryExpressions, and its regression test (a 300-line LINQ/anonymous type benchmark, Debug and Release builds) then gets every let back, and the code compiles and prints the same as the original.

The same happens for let clauses with constant-pattern switch expressions (c.Tier switch { 1 => "Bronze", 2 => "Silver", _ => "Gold" }), in joins and before group ... into.

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