From 44cb31abaed9bc46d673d21107fe8faa37c1595d Mon Sep 17 00:00:00 2001 From: Sebastien Lebreton Date: Fri, 26 Jun 2026 09:16:39 +0200 Subject: [PATCH] Fix #3820: decompile dynamic ~ as ~x instead of an unsupported-opcode error VisitDynamicUnaryOperatorInstruction handled every dynamic unary operator except ExpressionType.OnesComplement, so ~x on a dynamic operand fell through to the unsupported-opcode error expression and produced uncompilable output (an incomplete cast that fails to parse). Map it to the bitwise-complement operator, like the sibling unary cases. Assisted-by: Copilot:claude-opus-4.8:GitHub Copilot CLI --- ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.cs | 1 + ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.cs index 619540157..7811ffe01 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.cs @@ -394,6 +394,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty //++a; DynamicTests.Casts(-a); DynamicTests.Casts(+a); + DynamicTests.Casts(~a); } private static void Loops(dynamic list) diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index 0b6b87504..f07420fb4 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -4540,6 +4540,8 @@ namespace ICSharpCode.Decompiler.CSharp return CreateUnaryOperator(UnaryOperatorType.Minus, isChecked: true); case ExpressionType.UnaryPlus: return CreateUnaryOperator(UnaryOperatorType.Plus, isChecked: inst.BinderFlags.HasFlag(CSharpBinderFlags.CheckedContext)); + case ExpressionType.OnesComplement: + return CreateUnaryOperator(UnaryOperatorType.BitNot); case ExpressionType.IsTrue: var operand = TranslateDynamicArgument(inst.Operand, inst.OperandArgumentInfo); Expression expr;