diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/NativeInts.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/NativeInts.cs index b9d8429f6..e2d6b7ed5 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/NativeInts.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/NativeInts.cs @@ -109,6 +109,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty Console.WriteLine(-i); } + public unsafe int* PtrArithmetic(int* ptr) + { + return ptr + i; + } + + public unsafe nint* PtrArithmetic(nint* ptr) + { + return ptr + u; + } + public object[] Boxing() { return new object[10] { diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index ce88a7ea3..8f6dc1076 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -1143,10 +1143,7 @@ namespace ICSharpCode.Decompiler.CSharp { if (!expr.Type.IsCSharpPrimitiveIntegerType() && !expr.Type.IsCSharpNativeIntegerType()) { // pointer arithmetic accepts all primitive integer types, but no enums etc. - StackType targetType = expr.Type.GetStackType() == StackType.I4 ? StackType.I4 : StackType.I8; - expr = expr.ConvertTo( - compilation.FindType(targetType.ToKnownTypeCode(expr.Type.GetSign())), - this); + expr = expr.ConvertTo(FindArithmeticType(expr.Type.GetStackType(), expr.Type.GetSign()), this); } return expr; } diff --git a/ICSharpCode.Decompiler/IL/PointerArithmeticOffset.cs b/ICSharpCode.Decompiler/IL/PointerArithmeticOffset.cs index f1e7032dc..3064a4520 100644 --- a/ICSharpCode.Decompiler/IL/PointerArithmeticOffset.cs +++ b/ICSharpCode.Decompiler/IL/PointerArithmeticOffset.cs @@ -36,7 +36,7 @@ namespace ICSharpCode.Decompiler.IL if (mul.CheckForOverflow != checkForOverflow) return null; if (elementSize > 0 && mul.Right.MatchLdcI(elementSize.Value) - || mul.Right.UnwrapConv(ConversionKind.SignExtend) is SizeOf sizeOf && sizeOf.Type.Equals(pointerElementType)) { + || mul.Right.UnwrapConv(ConversionKind.SignExtend) is SizeOf sizeOf && NormalizeTypeVisitor.TypeErasure.EquivalentTypes(sizeOf.Type, pointerElementType)) { var countOffsetInst = mul.Left; if (unwrapZeroExtension) { countOffsetInst = countOffsetInst.UnwrapConv(ConversionKind.ZeroExtend);