Skip to content

Commit

Permalink
Fix icsharpcode#2407: Operator '-' cannot be applied to operand of ty…
Browse files Browse the repository at this point in the history
…pe 'nuint'
  • Loading branch information
ElektroKill committed Aug 11, 2021
1 parent bd9cc24 commit 9b162c7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/NativeInts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,10 @@ public void LocalTypeFromUse()
intptr = intPtr;
i = num + 1;
}

public nint NegateUnsigned(nuint x)
{
return (nint)(0 - x);
}
}
}
5 changes: 3 additions & 2 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,8 +1375,9 @@ TranslatedExpression HandleBinaryNumeric(BinaryNumericInstruction inst, BinaryOp
if (op == BinaryOperatorType.Subtract && inst.Left.MatchLdcI(0)) {
IType rightUType = NullableType.GetUnderlyingType(right.Type);
if (rightUType.IsKnownType(KnownTypeCode.Int32) || rightUType.IsKnownType(KnownTypeCode.Int64)
|| rightUType.IsCSharpSmallIntegerType() || rightUType.IsCSharpNativeIntegerType()) {
// unary minus is supported on signed int and long, and on the small integer types (since they promote to int)
|| rightUType.IsCSharpSmallIntegerType() || rightUType.Kind == TypeKind.NInt)
{
// unary minus is supported on signed int, nint and long, and on the small integer types (since they promote to int)
var uoe = new UnaryOperatorExpression(UnaryOperatorType.Minus, right.Expression);
uoe.AddAnnotation(inst.CheckForOverflow ? AddCheckedBlocks.CheckedAnnotation : AddCheckedBlocks.UncheckedAnnotation);
var resultType = FindArithmeticType(inst.RightInputType, Sign.Signed);
Expand Down

0 comments on commit 9b162c7

Please sign in to comment.