From c1ac674b0e9400202c5f0c43bb2c906b3843bc0d Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 18 Aug 2026 21:53:39 +0200 Subject: [PATCH] Avoid unused ref-locals if not allowed by the option. This is needed to pass the AsyncAwaitPatterns correctness test with legacy csc. --- ICSharpCode.Decompiler/CSharp/StatementBuilder.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs index d7cb0c3a5..3eeb55e89 100644 --- a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs @@ -90,7 +90,18 @@ namespace ICSharpCode.Decompiler.CSharp protected override TranslatedStatement Default(ILInstruction inst) { - return new ExpressionStatement(exprBuilder.Translate(inst)) + Expression expr = exprBuilder.Translate(inst); + if (!settings.UseRefLocalsForAccurateOrderOfEvaluation + && expr is DirectionExpression dir) + { + // Top-level "ref" (e.g. result if ldelema is unused) + // would get turned into ref-local by DeclareVariables. + // If ref locals aren't supported; remove the "ref", + // introducing a load. This way we create compilable code + // while preserving the bounds check of the ldelema. + expr = dir.Expression.Detach(); + } + return new ExpressionStatement(expr) .WithILInstruction(inst); }