diff --git a/CHANGELOG.md b/CHANGELOG.md
index 62d640b16..b11401cbb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,8 +15,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### C# -> VB
+* Evaluate built-in nint/nuint operators on Long/ULong, so the distance between two pointers doesn't throw or fail to compile
* Don't emit Me on the first parameter of an extension method - [#1118](https://github.com/icsharpcode/CodeConverter/issues/1118)
* Truncate towards zero when converting a fractional number to an integer, instead of inheriting VB's rounding - [#1021](https://github.com/icsharpcode/CodeConverter/issues/1021)
+* Convert nint/nuint to IntPtr/UIntPtr instead of the undefined nint/System.nint
## [10.0.2] - 2026-11-08
diff --git a/CodeConverter/Util/VBUtil.cs b/CodeConverter/Util/VBUtil.cs
index 0c510af96..28683c3b7 100644
--- a/CodeConverter/Util/VBUtil.cs
+++ b/CodeConverter/Util/VBUtil.cs
@@ -23,6 +23,7 @@ public static SyntaxKind GetExpressionOperatorTokenKind(this SyntaxKind op)
SyntaxKind.SubtractExpression => SyntaxKind.MinusToken,
SyntaxKind.MultiplyExpression => SyntaxKind.AsteriskToken,
SyntaxKind.DivideExpression => SyntaxKind.SlashToken,
+ SyntaxKind.IntegerDivideExpression => SyntaxKind.BackslashToken,
SyntaxKind.ModuloExpression => SyntaxKind.ModKeyword,
SyntaxKind.LeftShiftExpression => SyntaxKind.LessThanLessThanToken,
SyntaxKind.RightShiftExpression => SyntaxKind.GreaterThanGreaterThanToken,
diff --git a/CodeConverter/VB/CsExpander.cs b/CodeConverter/VB/CsExpander.cs
index 95b1ed82a..cf2dce4e2 100644
--- a/CodeConverter/VB/CsExpander.cs
+++ b/CodeConverter/VB/CsExpander.cs
@@ -12,9 +12,34 @@ public SyntaxNode ExpandNode(SyntaxNode node, SemanticModel semanticModel,
Workspace workspace)
{
SyntaxNode expandedNode = Simplifier.Expand(node, semanticModel, workspace);
- return WithoutGlobalOverqualification(expandedNode);
+ return WithNativeIntegerKeywords(WithoutGlobalOverqualification(expandedNode));
}
+ ///
+ /// Roslyn displays System.IntPtr/UIntPtr with the C# keywords nint/nuint (the same types since C# 11), so the expansion
+ /// qualifies them as System.nint/System.nuint, which don't exist. Put the keyword back so the name still binds.
+ ///
+ private static SyntaxNode WithNativeIntegerKeywords(SyntaxNode expandedNode)
+ {
+ var qualifiedNativeIntegers = expandedNode.DescendantNodesAndSelf()
+ .Where(n => n is QualifiedNameSyntax { Left: var left, Right: var right } && IsSystemNamespace(left) && IsNativeIntegerKeyword(right)
+ || n is MemberAccessExpressionSyntax { Expression: var expression, Name: var name } && IsSystemNamespace(expression) && IsNativeIntegerKeyword(name))
+ .ToArray();
+ if (!qualifiedNativeIntegers.Any()) return expandedNode;
+ return expandedNode.ReplaceNodes(qualifiedNativeIntegers, (original, _) => {
+ var keyword = original is QualifiedNameSyntax q ? q.Right : ((MemberAccessExpressionSyntax)original).Name;
+ return keyword.WithTriviaFrom(original);
+ });
+ }
+
+ private static bool IsNativeIntegerKeyword(SimpleNameSyntax name) => name is IdentifierNameSyntax { IsNint: true } or IdentifierNameSyntax { IsNuint: true };
+
+ private static bool IsSystemNamespace(ExpressionSyntax expression) => expression switch {
+ IdentifierNameSyntax id => id.Identifier.ValueText == nameof(System),
+ AliasQualifiedNameSyntax alias => alias.Alias.IsGlobalId() && alias.Name.Identifier.ValueText == nameof(System),
+ _ => false
+ };
+
///
/// The VB reduction step doesn't seem to reduce things qualified with global, so don't add it anywhere it isn't already
///
diff --git a/CodeConverter/VB/NativeIntegerOperators.cs b/CodeConverter/VB/NativeIntegerOperators.cs
new file mode 100644
index 000000000..b0ddd4da9
--- /dev/null
+++ b/CodeConverter/VB/NativeIntegerOperators.cs
@@ -0,0 +1,142 @@
+using Microsoft.CodeAnalysis.Operations;
+using Microsoft.CodeAnalysis.VisualBasic;
+using ExpressionSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax;
+using SyntaxFactory = Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory;
+using SyntaxKind = Microsoft.CodeAnalysis.VisualBasic.SyntaxKind;
+
+namespace ICSharpCode.CodeConverter.VB;
+
+///
+/// C#'s built-in operators on nint/nuint (also spelled IntPtr/UIntPtr since C# 11) have no VB counterpart: VB only sees
+/// IntPtr/UIntPtr's own operators (+ and - with an Integer offset, =, <>) and explicit conversions, because .NET 7's
+/// numeric operators are explicit interface implementations. With Option Strict Off VB would otherwise narrow an operand
+/// to Integer (throwing beyond Int32, e.g. for the distance between two pointers) or not compile at all.
+/// So such an operation is evaluated on Long/ULong, with the result converted back when C#'s result is a native integer.
+/// Where VB's own IntPtr/UIntPtr operator does what C#'s does, the output is left as it is.
+/// Like other integer arithmetic, the Long/ULong operation is overflow-checked in VB where C#'s is unchecked by default.
+///
+internal class NativeIntegerOperators
+{
+ private readonly SemanticModel _semanticModel;
+ private readonly CommonConversions _commonConversions;
+
+ public NativeIntegerOperators(SemanticModel semanticModel, CommonConversions commonConversions)
+ {
+ _semanticModel = semanticModel;
+ _commonConversions = commonConversions;
+ }
+
+ /// Null when the operation needs nothing special
+ public ExpressionSyntax ConvertBinaryOrNull(CSSyntax.BinaryExpressionSyntax node, ExpressionSyntax vbLeft, ExpressionSyntax vbRight)
+ {
+ if (_semanticModel.GetOperation(node) is not IBinaryOperation { OperatorMethod: null } operation) return null;
+ var operatorType = IsNativeInteger(operation.LeftOperand.Type) ? operation.LeftOperand.Type
+ : IsNativeInteger(operation.RightOperand.Type) ? operation.RightOperand.Type
+ : null;
+ return ConvertOrNull(operation.OperatorKind, operatorType, OriginalType(operation.LeftOperand), vbLeft,
+ OriginalType(operation.RightOperand), vbRight, operation.Type);
+ }
+
+ /// The value to assign to the target, or null when the compound assignment needs nothing special
+ public ExpressionSyntax ConvertCompoundAssignmentValueOrNull(CSSyntax.AssignmentExpressionSyntax node, ExpressionSyntax vbTarget, ExpressionSyntax vbValue)
+ {
+ if (_semanticModel.GetOperation(node) is not ICompoundAssignmentOperation { OperatorMethod: null } operation) return null;
+ var targetType = operation.Target.Type;
+ var operatorType = IsNativeInteger(targetType) ? targetType : null;
+ return ConvertOrNull(operation.OperatorKind, operatorType, targetType, vbTarget, OriginalType(operation.Value), vbValue, targetType);
+ }
+
+ /// Null when the operation needs nothing special
+ public ExpressionSyntax ConvertUnaryOrNull(CSSyntax.PrefixUnaryExpressionSyntax node, ExpressionSyntax vbOperand)
+ {
+ if (_semanticModel.GetOperation(node) is not IUnaryOperation { OperatorMethod: null } operation || !IsNativeInteger(operation.Type)) return null;
+ var kind = operation.OperatorKind switch {
+ UnaryOperatorKind.Minus => SyntaxKind.UnaryMinusExpression,
+ UnaryOperatorKind.Plus => SyntaxKind.UnaryPlusExpression,
+ UnaryOperatorKind.BitwiseNegation => SyntaxKind.NotExpression,
+ _ => SyntaxKind.None
+ };
+ if (kind == SyntaxKind.None) return null;
+ var unary = SyntaxFactory.UnaryExpression(kind, SyntaxFactory.Token(kind.GetExpressionOperatorTokenKind()), To64Bit(vbOperand, operation.Type));
+ return ToNativeInteger(unary, operation.Type);
+ }
+
+ private ExpressionSyntax ConvertOrNull(BinaryOperatorKind operatorKind, ITypeSymbol operatorType,
+ ITypeSymbol leftType, ExpressionSyntax vbLeft, ITypeSymbol rightType, ExpressionSyntax vbRight, ITypeSymbol resultType)
+ {
+ if (operatorType == null && !IsNativeInteger(leftType) && !IsNativeInteger(rightType)) return null;
+ if (operatorType != null && HasMatchingVbOperator(operatorKind, leftType, rightType)) return null;
+ var kind = ToVbBinaryKind(operatorKind);
+ if (kind == SyntaxKind.None) return null;
+
+ var isShift = operatorKind is BinaryOperatorKind.LeftShift or BinaryOperatorKind.RightShift;
+ var binary = SyntaxFactory.BinaryExpression(kind,
+ ConvertOperand(vbLeft, leftType, operatorType),
+ SyntaxFactory.Token(kind.GetExpressionOperatorTokenKind()),
+ isShift ? vbRight : ConvertOperand(vbRight, rightType, operatorType));
+ return IsNativeInteger(resultType) ? ToNativeInteger(binary, resultType) : binary;
+ }
+
+ ///
+ /// IntPtr/UIntPtr declare = and <>, and + and - with an Integer offset, which are unchecked like C#'s.
+ /// An operand narrower than Integer widens to it.
+ ///
+ private static bool HasMatchingVbOperator(BinaryOperatorKind operatorKind, ITypeSymbol leftType, ITypeSymbol rightType) =>
+ operatorKind switch {
+ BinaryOperatorKind.Equals or BinaryOperatorKind.NotEquals => true,
+ BinaryOperatorKind.Add or BinaryOperatorKind.Subtract => IsNativeInteger(leftType) && WidensToInteger(rightType),
+ _ => false
+ };
+
+ private static bool WidensToInteger(ITypeSymbol type) =>
+ type?.SpecialType is SpecialType.System_SByte or SpecialType.System_Byte or SpecialType.System_Int16
+ or SpecialType.System_UInt16 or SpecialType.System_Int32;
+
+ ///
+ /// A native integer operand becomes Long/ULong. With a ULong operator, VB would otherwise pick Decimal for ULong with
+ /// a signed operand, so the other operand becomes ULong too; with a Long operator VB widens it.
+ ///
+ private static ExpressionSyntax ConvertOperand(ExpressionSyntax vbOperand, ITypeSymbol operandType, ITypeSymbol operatorType)
+ {
+ if (IsNativeInteger(operandType)) return To64Bit(vbOperand, operandType);
+ if (operatorType?.SpecialType == SpecialType.System_UIntPtr && operandType?.SpecialType != SpecialType.System_UInt64) {
+ return To64Bit(vbOperand, operatorType);
+ }
+ return vbOperand;
+ }
+
+ private static ExpressionSyntax To64Bit(ExpressionSyntax vbOperand, ITypeSymbol nativeIntegerType)
+ {
+ var keyword = nativeIntegerType.SpecialType == SpecialType.System_UIntPtr ? SyntaxKind.CULngKeyword : SyntaxKind.CLngKeyword;
+ return SyntaxFactory.PredefinedCastExpression(SyntaxFactory.Token(keyword), vbOperand);
+ }
+
+ private ExpressionSyntax ToNativeInteger(ExpressionSyntax vbExpression, ITypeSymbol nativeIntegerType) =>
+ SyntaxFactory.CTypeExpression(vbExpression, _commonConversions.GetFullyQualifiedNameSyntax(nativeIntegerType, allowGlobalPrefix: false));
+
+ private static SyntaxKind ToVbBinaryKind(BinaryOperatorKind operatorKind) =>
+ operatorKind switch {
+ BinaryOperatorKind.Add => SyntaxKind.AddExpression,
+ BinaryOperatorKind.Subtract => SyntaxKind.SubtractExpression,
+ BinaryOperatorKind.Multiply => SyntaxKind.MultiplyExpression,
+ BinaryOperatorKind.Divide => SyntaxKind.IntegerDivideExpression,
+ BinaryOperatorKind.Remainder => SyntaxKind.ModuloExpression,
+ BinaryOperatorKind.LeftShift => SyntaxKind.LeftShiftExpression,
+ BinaryOperatorKind.RightShift => SyntaxKind.RightShiftExpression,
+ BinaryOperatorKind.And => SyntaxKind.AndExpression,
+ BinaryOperatorKind.Or => SyntaxKind.OrExpression,
+ BinaryOperatorKind.ExclusiveOr => SyntaxKind.ExclusiveOrExpression,
+ BinaryOperatorKind.Equals => SyntaxKind.EqualsExpression,
+ BinaryOperatorKind.NotEquals => SyntaxKind.NotEqualsExpression,
+ BinaryOperatorKind.LessThan => SyntaxKind.LessThanExpression,
+ BinaryOperatorKind.LessThanOrEqual => SyntaxKind.LessThanOrEqualExpression,
+ BinaryOperatorKind.GreaterThan => SyntaxKind.GreaterThanExpression,
+ BinaryOperatorKind.GreaterThanOrEqual => SyntaxKind.GreaterThanOrEqualExpression,
+ _ => SyntaxKind.None
+ };
+
+ private static ITypeSymbol OriginalType(IOperation operand) =>
+ operand is IConversionOperation { IsImplicit: true } conversion ? conversion.Operand.Type : operand.Type;
+
+ private static bool IsNativeInteger(ITypeSymbol type) => type?.SpecialType is SpecialType.System_IntPtr or SpecialType.System_UIntPtr;
+}
diff --git a/CodeConverter/VB/NativeIntegerTypeExtensions.cs b/CodeConverter/VB/NativeIntegerTypeExtensions.cs
new file mode 100644
index 000000000..abc289132
--- /dev/null
+++ b/CodeConverter/VB/NativeIntegerTypeExtensions.cs
@@ -0,0 +1,15 @@
+namespace ICSharpCode.CodeConverter.VB;
+
+///
+/// C# nint/nuint are contextual keywords for System.IntPtr/System.UIntPtr (the same types since C# 11),
+/// and Roslyn displays those types with them, e.g. in the casts that Simplifier.Expand adds.
+/// VB has no keyword for them, and there is no System.nint type to qualify them with.
+///
+internal static class NativeIntegerTypeExtensions
+{
+ public static ITypeSymbol GetNativeIntegerTypeOrNull(this SemanticModel semanticModel, CSSyntax.IdentifierNameSyntax name)
+ {
+ if (!name.IsNint && !name.IsNuint || name.SyntaxTree != semanticModel.SyntaxTree) return null;
+ return semanticModel.GetSymbolInfo(name).Symbol is ITypeSymbol { IsNativeIntegerType: true } type ? type : null;
+ }
+}
diff --git a/CodeConverter/VB/NodesVisitor.cs b/CodeConverter/VB/NodesVisitor.cs
index f988e5ea1..ae4fca4af 100644
--- a/CodeConverter/VB/NodesVisitor.cs
+++ b/CodeConverter/VB/NodesVisitor.cs
@@ -52,6 +52,7 @@ internal class NodesVisitor : CS.CSharpSyntaxVisitor
private readonly HashSet _extraImports = new();
private readonly CSharpHelperMethodDefinition _cSharpHelperMethodDefinition;
private readonly CommonConversions _commonConversions;
+ private readonly NativeIntegerOperators _nativeIntegerOperators;
private readonly HashSet _addedNames = new();
private int _placeholder = 1;
@@ -74,6 +75,7 @@ public NodesVisitor(CS.CSharpCompilation compilation, SemanticModel semanticMode
_outputWillBeOverflowChecked = outputWillBeOverflowChecked;
TriviaConvertingVisitor = new CommentConvertingVisitorWrapper(this);
_commonConversions = new CommonConversions(semanticModel, vbSyntaxGenerator, TriviaConvertingVisitor, _outputWillBeOverflowChecked);
+ _nativeIntegerOperators = new NativeIntegerOperators(semanticModel, _commonConversions);
_cSharpHelperMethodDefinition = new CSharpHelperMethodDefinition();
}
@@ -1082,7 +1084,9 @@ public override VisualBasicSyntaxNode VisitPrefixUnaryExpression(CSSyntax.Prefix
ExpressionSyntaxExtensions.CreateArgList((ExpressionSyntax)node.Operand.Accept(TriviaConvertingVisitor))
);
}
- return SyntaxFactory.UnaryExpression(kind, SyntaxFactory.Token(kind.GetExpressionOperatorTokenKind()), (ExpressionSyntax)node.Operand.Accept(TriviaConvertingVisitor));
+ var operand = (ExpressionSyntax)node.Operand.Accept(TriviaConvertingVisitor);
+ return _nativeIntegerOperators.ConvertUnaryOrNull(node, operand)
+ ?? SyntaxFactory.UnaryExpression(kind, SyntaxFactory.Token(kind.GetExpressionOperatorTokenKind()), operand);
}
private static string GetQualifiedName(params string[] names) {
return names.Aggregate((seed, current) => seed + '.' + current);
@@ -1195,8 +1199,11 @@ node.Parent is CSSyntax.ForStatementSyntax ||
node.Parent.IsParentKind(CS.SyntaxKind.SetAccessorDeclaration);
}
- private static AssignmentStatementSyntax MakeAssignmentStatement(CSSyntax.AssignmentExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right)
+ private AssignmentStatementSyntax MakeAssignmentStatement(CSSyntax.AssignmentExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right)
{
+ if (_nativeIntegerOperators.ConvertCompoundAssignmentValueOrNull(node, left, right) is { } nativeIntegerValue) {
+ return SyntaxFactory.SimpleAssignmentStatement(left, nativeIntegerValue);
+ }
var kind = CS.CSharpExtensions.Kind(node).ConvertToken(TokenContext.Local);
if (node.IsKind(CS.SyntaxKind.AndAssignmentExpression, CS.SyntaxKind.OrAssignmentExpression, CS.SyntaxKind.ExclusiveOrAssignmentExpression, CS.SyntaxKind.ModuloAssignmentExpression)) {
return SyntaxFactory.SimpleAssignmentStatement(
@@ -1404,6 +1411,10 @@ public override VisualBasicSyntaxNode VisitBinaryExpression(CSSyntax.BinaryExpre
return SyntaxFactory.IsNotExpression(vbLeft, vbRight);
}
+ if (_nativeIntegerOperators.ConvertBinaryOrNull(node, vbLeft, vbRight) is { } nativeIntegerOperation) {
+ return nativeIntegerOperation;
+ }
+
var kind = CS.CSharpExtensions.Kind(node).ConvertToken(TokenContext.Local);
if (node.IsKind(CS.SyntaxKind.AddExpression) && (leftConvertedType.SpecialType == SpecialType.System_String
|| rightConvertedType.SpecialType == SpecialType.System_String)) {
@@ -1935,6 +1946,9 @@ public override VisualBasicSyntaxNode VisitOmittedTypeArgument(CSSyntax.OmittedT
public override VisualBasicSyntaxNode VisitIdentifierName(CSSyntax.IdentifierNameSyntax node)
{
+ if (_semanticModel.GetNativeIntegerTypeOrNull(node) is { } nativeIntegerType) {
+ return _commonConversions.GetFullyQualifiedNameSyntax(nativeIntegerType, allowGlobalPrefix: false);
+ }
return WrapTypedNameIfNecessary(SyntaxFactory.IdentifierName(_commonConversions.ConvertIdentifier(node.Identifier)), node);
}
diff --git a/Tests/TestData/SelfVerifyingTests/CSToVB/NativeIntegerArithmeticTests.cs b/Tests/TestData/SelfVerifyingTests/CSToVB/NativeIntegerArithmeticTests.cs
new file mode 100644
index 000000000..96ed385e1
--- /dev/null
+++ b/Tests/TestData/SelfVerifyingTests/CSToVB/NativeIntegerArithmeticTests.cs
@@ -0,0 +1,105 @@
+using Xunit;
+
+///
+/// Values beyond Int32 (the tests run in a 64-bit process), so a conversion that narrows a native integer to Integer throws.
+///
+public class NativeIntegerArithmeticTests
+{
+ private static readonly nint Low = (nint)0x1_0000_0000L;
+ private static readonly nint High = (nint)0x1_0000_0010L;
+ private static readonly nuint UnsignedLow = (nuint)0x1_0000_0000UL;
+ private static readonly nuint UnsignedHigh = (nuint)0x1_0000_0010UL;
+
+ [Fact]
+ public void DistanceBetweenPointers()
+ {
+ nint distance = High - Low;
+ Assert.Equal(16L, (long)distance);
+ }
+
+ [Fact]
+ public void SumOfNativeIntegers() => Assert.Equal(0x2_0000_0010L, (long)(Low + High));
+
+ [Fact]
+ public void OffsetByInt()
+ {
+ int offset = 4;
+ nint pointer = Low + offset;
+ Assert.Equal(0x1_0000_0004L, (long)pointer);
+ Assert.Equal(0x1_0000_0000L, (long)(pointer - offset));
+ }
+
+ [Fact]
+ public void Comparisons()
+ {
+ Assert.True(High > Low);
+ Assert.True(Low < High);
+ Assert.True(High >= Low);
+ Assert.False(High <= Low);
+ Assert.True(High != Low);
+ Assert.False(High == Low);
+ }
+
+ [Fact]
+ public void MultiplicativeOperators()
+ {
+ Assert.Equal(0x2_0000_0020L, (long)(High * 2));
+ Assert.Equal(0x8000_0008L, (long)(High / 2));
+ Assert.Equal(16L, (long)(High % Low));
+ }
+
+ [Fact]
+ public void BitwiseAndShiftOperators()
+ {
+ Assert.Equal(0x1_0000_0000L, (long)(High & Low));
+ Assert.Equal(0x1_0000_0010L, (long)(High | Low));
+ Assert.Equal(16L, (long)(High ^ Low));
+ Assert.Equal(0x2_0000_0000L, (long)(Low << 1));
+ Assert.Equal(0x1000_0000L, (long)(Low >> 4));
+ }
+
+ [Fact]
+ public void UnaryOperators()
+ {
+ Assert.Equal(-0x1_0000_0000L, (long)(-Low));
+ Assert.Equal(~0x1_0000_0000L, (long)(~Low));
+ }
+
+ [Fact]
+ public void CompoundAssignments()
+ {
+ nint value = Low;
+ value += High;
+ Assert.Equal(0x2_0000_0010L, (long)value);
+ value -= High;
+ Assert.Equal(0x1_0000_0000L, (long)value);
+ value *= 2;
+ value /= 4;
+ Assert.Equal(0x8000_0000L, (long)value);
+ value <<= 1;
+ value |= 1;
+ Assert.Equal(0x1_0000_0001L, (long)value);
+ value += 1;
+ value++;
+ Assert.Equal(0x1_0000_0003L, (long)value);
+ }
+
+ [Fact]
+ public void UnsignedNativeIntegers()
+ {
+ nuint distance = UnsignedHigh - UnsignedLow;
+ Assert.Equal(16UL, (ulong)distance);
+ Assert.True(UnsignedHigh > UnsignedLow);
+ Assert.Equal(0x2_0000_0010UL, (ulong)(UnsignedLow + UnsignedHigh));
+ }
+
+ [Fact]
+ public void MixedWithLong()
+ {
+ long sum = High + 1L;
+ Assert.Equal(0x1_0000_0011L, sum);
+ long total = 1;
+ total += High;
+ Assert.Equal(0x1_0000_0011L, total);
+ }
+}
diff --git a/Tests/VB/TypeCastTests.cs b/Tests/VB/TypeCastTests.cs
index d4bd5013d..1ab5df6e5 100644
--- a/Tests/VB/TypeCastTests.cs
+++ b/Tests/VB/TypeCastTests.cs
@@ -265,4 +265,157 @@ Private Sub Test(d As Double, m As Decimal)
End Sub
End Class");
}
-}
+
+ [Fact]
+ public async Task NativeIntegerTypesAsync() {
+ await TestConversionCSharpToVisualBasicAsync(
+ @"public class Test {
+ private nint _signed;
+ private nuint _unsigned;
+ public nuint TestMethod(nint value, int i, uint u) {
+ _signed = value;
+ _signed = (nint)i;
+ _unsigned = (nuint)u;
+ return _unsigned;
+ }
+}",
+ @"Public Class Test
+ Private _signed As IntPtr
+ Private _unsigned As UIntPtr
+ Public Function TestMethod(value As IntPtr, i As Integer, u As UInteger) As UIntPtr
+ _signed = value
+ _signed = CType(i, IntPtr)
+ _unsigned = CType(u, UIntPtr)
+ Return _unsigned
+ End Function
+End Class");
+ }
+
+ [Fact]
+ public async Task IntPtrArgumentAsync() {
+ await TestConversionCSharpToVisualBasicAsync(
+ @"using System;
+using System.Runtime.InteropServices;
+
+class NativeMethods {
+ [DllImport(""user32.dll"", EntryPoint = ""GetWindowLongPtr"")]
+ static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
+ public static long GetWindowLong(IntPtr hWnd, int nIndex) {
+ return GetWindowLongPtr64(hWnd, nIndex).ToInt64();
+ }
+}",
+ @"Imports System
+Imports System.Runtime.InteropServices
+
+Friend Class NativeMethods
+
+ Private Shared Function GetWindowLongPtr64(hWnd As IntPtr, nIndex As Integer) As IntPtr
+ End Function
+ Public Shared Function GetWindowLong(hWnd As IntPtr, nIndex As Integer) As Long
+ Return GetWindowLongPtr64(hWnd, nIndex).ToInt64()
+ End Function
+End Class");
+ }
+
+ [Fact]
+ public async Task NativeIntegerTypeContextsAsync() {
+ await TestConversionCSharpToVisualBasicAsync(
+ @"using System;
+using System.Collections.Generic;
+
+public class Test {
+ public nint[] Array = new nint[1];
+ public List List = new List();
+ public nint? Nullable;
+ public Func Convert;
+
+ public Type TestMethod() {
+ nint local = 5;
+ var inferred = (nint)local;
+ nint max = nint.MaxValue;
+ nint zero = default(nint);
+ nint none = default;
+ Nullable = local;
+ return typeof(nuint);
+ }
+}",
+ @"Imports System
+Imports System.Collections.Generic
+
+Public Class Test
+ Public Array As IntPtr() = New IntPtr(0) {}
+ Public List As List(Of UIntPtr) = New List(Of UIntPtr)()
+ Public Nullable As IntPtr?
+ Public Convert As Func(Of IntPtr, UIntPtr)
+
+ Public Function TestMethod() As Type
+ Dim local As IntPtr = 5
+ Dim inferred = local
+ Dim max = IntPtr.MaxValue
+ Dim zero As IntPtr = Nothing
+ Dim none As IntPtr = Nothing
+ Nullable = local
+ Return GetType(UIntPtr)
+ End Function
+End Class");
+ }
+
+ [Fact]
+ public async Task UserTypeNamedNintAsync() {
+ await TestConversionCSharpToVisualBasicAsync(
+ @"using System;
+
+class nint { }
+
+class Test {
+ private nint _custom = new nint();
+ private IntPtr _pointer = IntPtr.Zero;
+ private System.UIntPtr _unsigned = System.UIntPtr.Zero;
+}",
+ @"Imports System
+
+Friend Class nint
+End Class
+
+Friend Class Test
+ Private _custom As nint = New nint()
+ Private _pointer As IntPtr = IntPtr.Zero
+ Private _unsigned As UIntPtr = UIntPtr.Zero
+End Class");
+ }
+
+ [Fact]
+ public async Task NativeIntegerOperatorsAsync() {
+ await TestConversionCSharpToVisualBasicAsync(
+ @"public class Test {
+ public long TestMethod(nint start, nint end, nuint size, int offset, long total) {
+ nint distance = end - start;
+ nint next = start + offset;
+ bool before = start < end;
+ bool same = start == end;
+ nint half = distance / 2;
+ nuint doubled = size * 2;
+ nint negated = -distance;
+ distance += end;
+ distance -= 1;
+ total += start;
+ return total + distance;
+ }
+}",
+ @"Public Class Test
+ Public Function TestMethod(start As IntPtr, [end] As IntPtr, size As UIntPtr, offset As Integer, total As Long) As Long
+ Dim distance = CType(CLng([end]) - CLng(start), IntPtr)
+ Dim [next] = start + offset
+ Dim before = CLng(start) < CLng([end])
+ Dim same = start = [end]
+ Dim half = CType(CLng(distance) \ 2, IntPtr)
+ Dim doubled = CType(CULng(size) * CULng(2), UIntPtr)
+ Dim negated = CType(-CLng(distance), IntPtr)
+ distance = CType(CLng(distance) + CLng([end]), IntPtr)
+ distance -= 1
+ total = total + CLng(start)
+ Return total + CLng(distance)
+ End Function
+End Class");
+ }
+}
\ No newline at end of file