Open LDM issues with native integers · Issue #3259 · dotnet/csharplang (original) (raw)
Open LDM issues with native integers
Open issues for native integer support in C# 9.
Do native integer types leak into programs compiled with -langversion:8?
// -langversion:9 class Lib { static nint NativeInt; }
// -langversion:8 var i = Lib.NativeInt; // nint i?
Conclusion: members using native integer types are available in -langversion:8, and native integer types are visible through var
, but use of new built-in operators and conversions are errors, other than identity conversions between native integer types and underlying types.
Should IntPtr
arithmetic use nint
operators?
Identity conversions between native integers and underlying types allow built-in operators for native integers to apply to underlying types.
// -langversion:9 static IntPtr Multiply(IntPtr x, int y) { return x * y; // nint.op_Multiply(nint, nint) }
Conclusion: built-in operators for native integer types, and conversions from native integer types, are ignored if none of the operands are native integers.
Are built-in conversions and operators available from -langversion:8?
// -langversion:8 _ = Lib.NativeInt + 1; // nint.op_Addition(nint, nint) or IntPtr.op_Addition(IntPtr, int)? _ = Lib.NativeInt * 2; // nint.op_Multiply(nint, nint) or error?
Conclusion: use of built-in conversions and operators are errors in -langversion:8
Allow constant folding at runtime rather than compile time
Should unchecked
constant expressions be calculated at runtime if platform-dependent? See u2
below.
const nint m = int.MaxValue; const nint u1 = unchecked(m + 1); // error CS0133: expression must be constant nint u2 = unchecked(m + 1); // calculated at runtime?
Should checked
constant expressions be calculated at runtime if platform-dependent, perhaps with a warning that the value may overflow? See c2
below.
const nint m = int.MaxValue; const nint c1 = checked(m + 1); // error CS0220: overflows at compile time in checked mode nint c2 = checked(m + 1); // calculated at runtime, warn at compile time?
Conclusion: constant folding is executed at runtime rather than compile-time if the result depends on the size of native integers, and in a checked
context, a warning is reported that the result may overflow.
Which interfaces are implemented by nint
and nuint
?
Should nint
and nuint
implement all of the interfaces implemented by System.IntPtr
and System.UIntPtr
or an explicit set?
public struct IntPtr : ISerializable, IEquatable, ... { }
Conclusion: native integer types should implement all interfaces implemented by the underlying types, with corresponding substitution of underlying type arguments.