Query let clause whose value is a switch expression decompiles to <>h__TransparentIdentifier0 (and if (1 == 0) { } in Debug builds)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 55/100
調査の方向性
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 の本文から書いたものです。
説明
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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
icsharpcode/ILSpy のほかの issue
-
External UI
難易度 1/5 1〜3時間 初心者へのやさしさ 75/100
icsharpcode/ILSpy#4284 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
icsharpcode/ILSpy#4357 ·
メンテナーはふだん 1 日以内に返信
-
Misdecompilation of a lazy initialization pattern対応中かも @siegfriedpammer が 5 日前に担当しました。 オープンBug Decompiler
難易度 4/5 3〜5日 初心者へのやさしさ 20/100
icsharpcode/ILSpy#4349 ·
メンテナーはふだん 1 日以内に返信
-
Bug
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
icsharpcode/ILSpy#4343 ·
メンテナーはふだん 1 日以内に返信
-
Enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
icsharpcode/ILSpy#4298 · コメント 1 件 · リアクション 2 件 ·
メンテナーはふだん 1 日以内に返信
icsharpcode/ILSpy の issue をすべて見る
似ている issue
-
type:task ux
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
elsa-workflows/elsa-foundation#5253 ·
メンテナーはふだん 1 日以内に返信