Skip to content

C# -> VB: Convert nint/nuint to IntPtr/UIntPtr - #1320

Open
Alexilia wants to merge 2 commits into
icsharpcode:masterfrom
Alexilia:fix/native-integer-types
Open

Alexilia wants to merge 2 commits into
icsharpcode:masterfrom
Alexilia:fix/native-integer-types

Conversation

@Alexilia

@Alexilia Alexilia commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Roslyn displays IntPtr/UIntPtr as nint/nuint, so name expansion wrote the nonexistent System.nint/System.nuint (declarations, generic arguments, nint.MaxValue) and casts like CType(hWnd, nint) for plain IntPtr arguments, which also stopped the rest of the expansion from being reduced. All give BC30002 in VB.

Solution

  • The expander puts the keyword back, and a name bound to a native integer type converts to IntPtr/UIntPtr.
  • Not included: conversions between nint and nuint, native integer arithmetic, and implicit conversions to native ints, which are narrowing in VB (nint local = 5; -> Dim local As IntPtr = 5 compiles only with Option Strict Off).
  • At least one test covering the code changed (each type position, an IntPtr argument to an extern method, and a user type named nint and explicit IntPtr staying unchanged)

Process

  • Attachment level to the code in this PR: high. It was written with Claude (Anthropic), then reviewed by me; I understand it and take responsibility for it as my own code.
  • LLM primarily used: Claude Opus 5.5

@GrahamTheCoder

GrahamTheCoder commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Hmm I'm a bit worried about that overflow situation. It feels like one of the main use cases for nint would be pointer arithmetic, and if you try to find the distance between two nint pointers it will throw now. The converter currently does use Option Strict Off for its output.
I think it'd be worth trying to add the casting work onto this for just the situations it's needed (or making them emit an error if that turns out significantly easier) in VisitBinaryExpression/VisitAssignmentExpression/MakeAssignmentStatement.
Let me know if you want to do that or have a better suggestion

Roslyn displays System.IntPtr/UIntPtr with the nint/nuint keywords (the same
types since C# 11), so name expansion wrote them as the nonexistent
System.nint/System.nuint (in declarations, generic arguments and member
access such as nint.MaxValue) and added casts like `(nint)hWnd` for plain
IntPtr arguments. VB has no such names (BC30002), and the failed binding also
stopped the reducer from undoing the rest of the expansion.

The expander now puts the keyword back, and a name bound to a native integer
type is converted to the VB name of its type. A user type named nint and
explicit IntPtr/UIntPtr are unchanged.
VB sees only IntPtr/UIntPtr's own operators (+ and - with an Integer offset,
=, <>) and explicit conversions; .NET 7's numeric operators are explicit
interface implementations. So with Option Strict Off `end - start` on two
nint values narrowed `start` to Integer and threw beyond Int32, and
comparisons, *, /, Mod, bitwise operators, shifts and unary - / Not didn't
compile.

A built-in C# operator on nint/nuint is now evaluated on Long/ULong, with the
result converted back when C#'s result is a native integer; division becomes
\. Compound assignments are expanded the same way. Where VB's own operator
does what C#'s does (=, <>, and + or - with an Integer offset), the output is
unchanged.
@Alexilia
Alexilia force-pushed the fix/native-integer-types branch from 924e0eb to 430ce0f Compare November 24, 2026 20:16
@Alexilia

Copy link
Copy Markdown
Contributor Author

Good catch, thanks. I've added the casts in a separate commit (430ce0f). VB only sees IntPtr/UIntPtr's own operators (+/- with an Integer offset, =, <>), because .NET 7's numeric operators are explicit interface implementations. So end - start narrowed start to Integer and threw beyond Int32, and comparisons, *, /, Mod, bitwise operators, shifts and unary -/Not didn't compile at all.

A built-in operator on nint/nuint is now evaluated on Long/ULong and converted back when C#'s result is a native integer, e.g. CType(CLng([end]) - CLng(start), IntPtr). Division becomes \. Compound assignments are expanded the same way (distance = CType(CLng(distance) + CLng([end]), IntPtr)). Where VB's own operator already matches C#'s, the output is unchanged: =/<>, and +/- with an Integer-or-narrower offset on the right (start + offset, distance -= 1, p++). The logic is in a small VB/NativeIntegerOperators class called from VisitBinaryExpression, VisitPrefixUnaryExpression and MakeAssignmentStatement.

Tests: a CS -> VB self-verifying test (NativeIntegerArithmeticTests, values beyond Int32 in a 64-bit process) covers pointer distance, sums, the Integer offset, comparisons, * / %, bitwise and shift operators, unary operators, compound assignments, nuint, and mixing with long. All 10 failed before, since the converted file didn't compile; run one at a time, six don't compile, the distance, sum and mixed-with-long ones throw, and the Integer offset one already worked. They pass now. A conversion test (TypeCastTests.NativeIntegerOperatorsAsync) pins the output shapes. No existing expectation changed.

Still not handled, and noted in the class doc: like other integer arithmetic, the Long/ULong operation is overflow-checked in VB, where C# is unchecked by default. Also not handled: >>>, ++/-- used as a value (they go through Interlocked, which has no IntPtr overload), and conversions between nint and nuint. Happy to look at any of those separately if you think they're worth it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants