runtime-async: pretty-test gaps in exception/try-finally rewriting
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- csharp
- 领域
- compilers, testing-qa
调研方向
先从 RuntimeAsyncExceptionRewriteTransform 及其 TryRewriteFlagBasedEarlyReturn 和 TryRewriteMultiHandlerTryCatch 匹配器开始,然后在 runtime-async 分支上运行 Async 和 RuntimeAsync 测试集。为列出的 try-finally、异构 handler、嵌套、循环跳转和仅 throw repro 添加覆盖;完成的标准是它们的反编译输出不再保留原始的降级模式。
由索引模型根据 Issue 内容生成。
描述
Exploratory testing of the runtime-async branch surfaced several gaps in the RuntimeAsync* pretty-test pipeline. The full Async / RuntimeAsync test set is green today, but the matchers introduced in RuntimeAsyncExceptionRewriteTransform are narrowly fitted to the shapes the existing fixtures exercise. Patterns just outside that envelope produce raw lowered IL in the decompiled output.
All findings reproduce on the runtime-async branch (post-review HEAD). They are runtime-async specific (corresponding state-machine Async tests pass), except where noted.
Cluster 1 — Flag-based early-return matcher (TryRewriteFlagBasedEarlyReturn)
The matcher is too narrowly fitted. Four distinct failing shapes:
-
Direct
return X;inside try-finally with await in finally.
The matcher requiresparentBlock.Instructions[tryFinallyIdx + 1] is Branch br(i.e. abr checkBlockto a dedicated check-block). In the simplest case the compiler emits the flag check inline: directly after the TryFinally,parentBlockhasif (flag == 1) leave function (capture); throw null(theIfInstructionis the next sibling, not preceded by abr).public async Task<int> ReturnFromTryFinally() { try { return 42; } finally { await Task.CompletedTask; } }Decompiled output retains
int num; int result; ...; if (num == 1) return result; throw null;. -
Void
return;— same gate, value-less form. Same diagnosis as 1. -
Nested try-finally with
returnin inner try.
The flag-setter block in the outer try is no longer 2 instructions (stloc flag(K); leave try) — it has a preceding capture-forwarding store (stloc result(innerCapture); stloc outerFlag(K); leave outer-try). The strictb.Instructions.Count == 2gate in the flag-setter scan rejects it.public async Task<int> ReturnFromInsideNestedTryFinally() { try { try { return 42; } finally { await Task.CompletedTask; } } finally { await Task.CompletedTask; } } -
break/continueinside loop inside try-finally with await.
Runtime-async applies the capture-and-flag-then-leave pattern not just toreturnbut to any non-local jump that needs to run the finally first.directLeave.IsLeavingFunctionrejects loop-level leaves, so the flag machinery survives.for (int i = 0; i < n; i++) { try { if (i == 1) continue; if (i == 5) break; } finally { await Task.CompletedTask; } }
Proposed fix: generalise the matcher.
- Accept either a
Branchto a dedicated check-block or an inlineIfInstructionimmediately after the TryFinally (closes 1, 2). - Relax the flag-setter shape to "ends with
stloc flagVar(K); leave try", allowing arbitrary capture-forwarding stores before it (closes 3). - Replace
directLeave.IsLeavingFunctionwith "leaves any ancestor container" and synthesise the appropriateLeaveto that container — the AST builder rendersbreak/continue/returnbased on the target (closes 4).
Cluster 2 — Multi-handler matcher (TryRewriteMultiHandlerTryCatch)
The matcher succeeds for LoadsToCatch's specific shape (three Exception-typed handlers, all with throw; in their bodies, uniform variable access) but fails for any heterogeneous variant:
// Fails: two different typed catches
try { await t; }
catch (InvalidOperationException ex)
{
await Task.Yield();
Console.WriteLine(ex.Message);
}
catch (ArgumentException ex)
{
await Task.Yield();
Console.WriteLine(ex.Message);
}
// Also fails: typed catch + untyped catch
try { await t; }
catch (InvalidOperationException ex) { await Task.Yield(); Console.WriteLine(ex.Message); }
catch { await Task.Yield(); Console.WriteLine("other"); }
// Also fails: typed catch + Exception with throw;
try { await t; }
catch (InvalidOperationException ex) { await Task.Yield(); Console.WriteLine(ex.Message); }
catch (Exception) { await Task.Yield(); throw; }
All three produce raw lowered output with the flag-dispatched switch (num) post-catch handling intact, plus the ExceptionDispatchInfo.Capture(...).Throw() idiom where a throw; was the source.
Proposed investigation: add ILAst-level logging to identify which handler trips up the matcher, then relax the per-handler shape constraint to accept heterogeneous prefix-store sequences. Most likely the issue is the prefix-store pattern differing per handler (e.g. cast-class store layout differs when one handler accesses the variable and the other doesn't).
Cluster 3 — "Must be last instruction" Run-loop gate
// At the top of the Run-loop, before any per-tryCatch matcher:
if (tryCatch.ChildIndex != parentBlock.Instructions.Count - 1)
continue;
This rejects any try-catch that isn't the last instruction in its parent block. For a top-level try-catch followed by an implicit function-leave it works, but for nested cases the parent block typically continues with a follow-up instruction (br continuation or similar), so the gate rejects.
Concrete reproducer:
public async Task TryCatchFinallyAllAwait()
{
try { await Task.CompletedTask; Console.WriteLine("try"); }
catch (Exception) { await Task.CompletedTask; Console.WriteLine("catch"); }
finally { await Task.CompletedTask; Console.WriteLine("finally"); }
}
The outer try-finally recovers, but the inner try-catch (now inside the outer try block) is no longer the last instruction in its parent block. The gate rejects, leaving the flag-based catch dispatch intact.
Proposed fix: drop the gate. Each matcher already verifies the post-catch successor shape via its own parentBlock.Instructions[tryCatch.ChildIndex + 1]-style checks; this Run-loop-level gate is over-restrictive. Alternatively run the rewriter iteratively to a fixed point so each level is matched in turn.
Cluster 4 — Pre-existing bug exposed (also fails on baseline)
throw as the only exit from try body:
public async Task ThrowInsideTryFinally()
{
try { throw new InvalidOperationException(); }
finally { await Task.Yield(); }
}
The try body has no outward Branch / Leave (just a throw). The existence check seenExit (and the pre-review existence check) rejects, leaving the raw lowered output. Confirmed to fail on the branch tip prior to this review as well — not a regression, but the same pattern of "too-strict matcher" applies.
Proposed fix: drop the seenExit requirement entirely. The other gates (dispatch idiom recognition, catch-handler shape, stloc obj(ldnull) pre-init) are sufficient to identify the lowered pattern; a try body whose only exit is a throw is a legitimate input.
Out of scope (general decompiler limitations, not runtime-async specific)
These failed in the exploration but fail equivalently on the state-machine Async pipeline, so they're pre-existing decompiler limitations rather than runtime-async bugs:
- ternary
?:with awaits in both arms — decompiled asif/return. is T namepattern insidewhenfilter — produces a BlockContainer the expression-builder can't render.checked { ... }block decompiled as inlinechecked(...).- 3+ awaits in single arithmetic expression on non-Optimize — split into intermediate locals.
Tests that pass and could be added as coverage (no bugs)
These pass today but weren't in the fixtures — worth adding as regression guards:
- consecutive top-level try-finally
async ValueTask<T>(smoke test)async Task<(int, int)>returning tuple from awaited task- direct
return await foo(); - true
throw;rethrow after await - typed catch with await + variable use
catch (T) when (...)with no variable name- switch with awaits in cases inside try-finally
- nested try-catch inside catch
- try-finally inside catch
- multiple consecutive awaits in finally
- return in try-catch (no finally)
- 主要语言
- C#
- 星标
- 27.1k
- 派生
- 3.8k
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 44
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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 天内回复
-
AI
难度 4/5 1-2 天 新手友好度 55/100
icsharpcode/ILSpy#4300 ·
维护者通常 1 天内回复
查看 icsharpcode/ILSpy 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
umbraco/Umbraco.Automate#612 ·
维护者通常 1 天内回复
-
崩溃日志只按 CRLF 分行未关闭
难度 2/5 1-3 小时 新手友好度 72/100
PCL-Community/PCL-CE#3872 ·
维护者通常 1 天内回复
-
type:task ux
难度 2/5 1-3 小时 新手友好度 72/100
elsa-workflows/elsa-foundation#5253 ·
维护者通常 1 天内回复