Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open LDM issues with native integers #3259

Closed
cston opened this issue Mar 9, 2020 · 0 comments
Closed

Open LDM issues with native integers #3259

cston opened this issue Mar 9, 2020 · 0 comments
Labels

Comments

@cston
Copy link
Member

cston commented Mar 9, 2020

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<IntPtr>, ... { }

Conclusion: native integer types should implement all interfaces implemented by the underlying types, with corresponding substitution of underlying type arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants