From: Stephen Toub Date: Thu, 17 Oct 2019 17:36:29 +0000 (-0400) Subject: Annotate System.Runtime.Numerics for nullable reference types (dotnet/corefx#41861) X-Git-Tag: submit/tizen/20210909.063632~11031^2~278 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b1605301e972d5fa038e697862460007c94f9694;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Annotate System.Runtime.Numerics for nullable reference types (dotnet/corefx#41861) Commit migrated from https://github.com/dotnet/corefx/commit/5f46e170dee7ad6b03c5da8c014d8e95a05b835b --- diff --git a/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs b/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs index 2d4f906..77981ef 100644 --- a/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs +++ b/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs @@ -329,7 +329,7 @@ namespace System.Globalization return null; } - private static unsafe bool ParseNumber(ref char* str, char* strEnd, NumberStyles options, ref NumberBuffer number, StringBuilder sb, NumberFormatInfo numfmt, bool parseDecimal) + private static unsafe bool ParseNumber(ref char* str, char* strEnd, NumberStyles options, ref NumberBuffer number, StringBuilder? sb, NumberFormatInfo numfmt, bool parseDecimal) { Debug.Assert(str != null); Debug.Assert(strEnd != null); @@ -346,7 +346,7 @@ namespace System.Globalization number.sign = false; string decSep; // Decimal separator from NumberFormatInfo. string groupSep; // Group separator from NumberFormatInfo. - string currSymbol = null; // Currency symbol from NumberFormatInfo. + string? currSymbol = null; // Currency symbol from NumberFormatInfo. bool parsingCurrency = false; if ((options & NumberStyles.AllowCurrencySymbol) != 0) @@ -421,7 +421,7 @@ namespace System.Globalization { if (bigNumber) { - sb.Append(ch); + sb!.Append(ch); } else { @@ -463,7 +463,7 @@ namespace System.Globalization bool negExp = false; number.precision = digEnd; if (bigNumber) - sb.Append('\0'); + sb!.Append('\0'); else dig[digEnd] = '\0'; if ((state & StateDigits) != 0) @@ -902,8 +902,10 @@ namespace System.Globalization return result; } - private static unsafe void FormatFixed(ref ValueStringBuilder sb, ref NumberBuffer number, int nMinDigits, int nMaxDigits, NumberFormatInfo info, int[] groupDigits, string sDecimal, string sGroup) + private static unsafe void FormatFixed(ref ValueStringBuilder sb, ref NumberBuffer number, int nMinDigits, int nMaxDigits, NumberFormatInfo info, int[]? groupDigits, string sDecimal, string? sGroup) { + Debug.Assert(sGroup != null || groupDigits == null); + int digPos = number.scale; char* dig = number.digits; int digLength = wcslen(dig); @@ -916,7 +918,7 @@ namespace System.Globalization int groupSizeCount = groupDigits[groupSizeIndex]; // The current total of group size. int groupSizeLen = groupDigits.Length; // The length of groupDigits array. int bufferSize = digPos; // The length of the result buffer string. - int groupSeparatorLen = sGroup.Length; // The length of the group separator string. + int groupSeparatorLen = sGroup!.Length; // The length of the group separator string. int groupSize = 0; // The current group size. // Find out the size of the string buffer for the result. diff --git a/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs b/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs index da7dce2..a9c73d6 100644 --- a/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs +++ b/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs @@ -36,14 +36,14 @@ namespace System.Numerics public static int Compare(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; } public int CompareTo(long other) { throw null; } public int CompareTo(System.Numerics.BigInteger other) { throw null; } - public int CompareTo(object obj) { throw null; } + public int CompareTo(object? obj) { throw null; } [System.CLSCompliantAttribute(false)] public int CompareTo(ulong other) { throw null; } public static System.Numerics.BigInteger Divide(System.Numerics.BigInteger dividend, System.Numerics.BigInteger divisor) { throw null; } public static System.Numerics.BigInteger DivRem(System.Numerics.BigInteger dividend, System.Numerics.BigInteger divisor, out System.Numerics.BigInteger remainder) { throw null; } public bool Equals(long other) { throw null; } public bool Equals(System.Numerics.BigInteger other) { throw null; } - public override bool Equals(object obj) { throw null; } + public override bool Equals(object? obj) { throw null; } [System.CLSCompliantAttribute(false)] public bool Equals(ulong other) { throw null; } public int GetByteCount(bool isUnsigned = false) { throw null; } @@ -144,25 +144,25 @@ namespace System.Numerics public static System.Numerics.BigInteger operator -(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; } public static System.Numerics.BigInteger operator -(System.Numerics.BigInteger value) { throw null; } public static System.Numerics.BigInteger operator +(System.Numerics.BigInteger value) { throw null; } - public static System.Numerics.BigInteger Parse(System.ReadOnlySpan value, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, System.IFormatProvider provider = null) { throw null; } + public static System.Numerics.BigInteger Parse(System.ReadOnlySpan value, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, System.IFormatProvider? provider = null) { throw null; } public static System.Numerics.BigInteger Parse(string value) { throw null; } public static System.Numerics.BigInteger Parse(string value, System.Globalization.NumberStyles style) { throw null; } - public static System.Numerics.BigInteger Parse(string value, System.Globalization.NumberStyles style, System.IFormatProvider provider) { throw null; } - public static System.Numerics.BigInteger Parse(string value, System.IFormatProvider provider) { throw null; } + public static System.Numerics.BigInteger Parse(string value, System.Globalization.NumberStyles style, System.IFormatProvider? provider) { throw null; } + public static System.Numerics.BigInteger Parse(string value, System.IFormatProvider? provider) { throw null; } public static System.Numerics.BigInteger Pow(System.Numerics.BigInteger value, int exponent) { throw null; } public static System.Numerics.BigInteger Remainder(System.Numerics.BigInteger dividend, System.Numerics.BigInteger divisor) { throw null; } public static System.Numerics.BigInteger Subtract(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; } public byte[] ToByteArray() { throw null; } public byte[] ToByteArray(bool isUnsigned = false, bool isBigEndian = false) { throw null; } public override string ToString() { throw null; } - public string ToString(System.IFormatProvider provider) { throw null; } - public string ToString(string format) { throw null; } - public string ToString(string format, System.IFormatProvider provider) { throw null; } - public bool TryFormat(System.Span destination, out int charsWritten, System.ReadOnlySpan format = default(System.ReadOnlySpan), System.IFormatProvider provider = null) { throw null; } - public static bool TryParse(System.ReadOnlySpan value, System.Globalization.NumberStyles style, System.IFormatProvider provider, out System.Numerics.BigInteger result) { throw null; } + public string ToString(System.IFormatProvider? provider) { throw null; } + public string ToString(string? format) { throw null; } + public string ToString(string? format, System.IFormatProvider? provider) { throw null; } + public bool TryFormat(System.Span destination, out int charsWritten, System.ReadOnlySpan format = default(System.ReadOnlySpan), System.IFormatProvider? provider = null) { throw null; } + public static bool TryParse(System.ReadOnlySpan value, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.Numerics.BigInteger result) { throw null; } public static bool TryParse(System.ReadOnlySpan value, out System.Numerics.BigInteger result) { throw null; } - public static bool TryParse(string value, System.Globalization.NumberStyles style, System.IFormatProvider provider, out System.Numerics.BigInteger result) { throw null; } - public static bool TryParse(string value, out System.Numerics.BigInteger result) { throw null; } + public static bool TryParse(string? value, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.Numerics.BigInteger result) { throw null; } + public static bool TryParse(string? value, out System.Numerics.BigInteger result) { throw null; } public bool TryWriteBytes(System.Span destination, out int bytesWritten, bool isUnsigned = false, bool isBigEndian = false) { throw null; } } public partial struct Complex : System.IEquatable, System.IFormattable @@ -192,7 +192,7 @@ namespace System.Numerics public static System.Numerics.Complex Divide(System.Numerics.Complex dividend, double divisor) { throw null; } public static System.Numerics.Complex Divide(System.Numerics.Complex dividend, System.Numerics.Complex divisor) { throw null; } public bool Equals(System.Numerics.Complex value) { throw null; } - public override bool Equals(object obj) { throw null; } + public override bool Equals(object? obj) { throw null; } public static System.Numerics.Complex Exp(System.Numerics.Complex value) { throw null; } public static System.Numerics.Complex FromPolarCoordinates(double magnitude, double phase) { throw null; } public override int GetHashCode() { throw null; } @@ -249,8 +249,8 @@ namespace System.Numerics public static System.Numerics.Complex Tan(System.Numerics.Complex value) { throw null; } public static System.Numerics.Complex Tanh(System.Numerics.Complex value) { throw null; } public override string ToString() { throw null; } - public string ToString(System.IFormatProvider provider) { throw null; } - public string ToString(string format) { throw null; } - public string ToString(string format, System.IFormatProvider provider) { throw null; } + public string ToString(System.IFormatProvider? provider) { throw null; } + public string ToString(string? format) { throw null; } + public string ToString(string? format, System.IFormatProvider? provider) { throw null; } } } diff --git a/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.csproj b/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.csproj index 0ba4a6d..623e8db 100644 --- a/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.csproj +++ b/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.csproj @@ -1,5 +1,6 @@ + enable netcoreapp-Debug;netcoreapp-Release diff --git a/src/libraries/System.Runtime.Numerics/src/System.Runtime.Numerics.csproj b/src/libraries/System.Runtime.Numerics/src/System.Runtime.Numerics.csproj index 3c41b57..807ef17 100644 --- a/src/libraries/System.Runtime.Numerics/src/System.Runtime.Numerics.csproj +++ b/src/libraries/System.Runtime.Numerics/src/System.Runtime.Numerics.csproj @@ -3,6 +3,7 @@ System.Numerics System.Runtime.Numerics true + enable netcoreapp-Debug;netcoreapp-Release diff --git a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs index 7da0f01..80c6693 100644 --- a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs +++ b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs @@ -20,7 +20,7 @@ namespace System.Numerics // For values int.MinValue < n <= int.MaxValue, the value is stored in sign // and _bits is null. For all other values, sign is +1 or -1 and the bits are in _bits internal readonly int _sign; // Do not rename (binary serialization) - internal readonly uint[] _bits; // Do not rename (binary serialization) + internal readonly uint[]? _bits; // Do not rename (binary serialization) // We have to make a choice of how to represent int.MinValue. This is the one // value that fits in an int, but whose negation does not fit in an int. @@ -461,7 +461,7 @@ namespace System.Numerics AssertValid(); } - internal BigInteger(int n, uint[] rgu) + internal BigInteger(int n, uint[]? rgu) { _sign = n; _bits = rgu; @@ -662,27 +662,27 @@ namespace System.Numerics return Parse(value, style, NumberFormatInfo.CurrentInfo); } - public static BigInteger Parse(string value, IFormatProvider provider) + public static BigInteger Parse(string value, IFormatProvider? provider) { return Parse(value, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } - public static BigInteger Parse(string value, NumberStyles style, IFormatProvider provider) + public static BigInteger Parse(string value, NumberStyles style, IFormatProvider? provider) { return BigNumber.ParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider)); } - public static bool TryParse(string value, out BigInteger result) + public static bool TryParse(string? value, out BigInteger result) { return TryParse(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } - public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, out BigInteger result) + public static bool TryParse(string? value, NumberStyles style, IFormatProvider? provider, out BigInteger result) { return BigNumber.TryParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider), out result); } - public static BigInteger Parse(ReadOnlySpan value, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) + public static BigInteger Parse(ReadOnlySpan value, NumberStyles style = NumberStyles.Integer, IFormatProvider? provider = null) { return BigNumber.ParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider)); } @@ -692,7 +692,7 @@ namespace System.Numerics return BigNumber.TryParseBigInteger(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } - public static bool TryParse(ReadOnlySpan value, NumberStyles style, IFormatProvider provider, out BigInteger result) + public static bool TryParse(ReadOnlySpan value, NumberStyles style, IFormatProvider? provider, out BigInteger result) { return BigNumber.TryParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider), out result); } @@ -754,6 +754,8 @@ namespace System.Numerics return s_bnZeroInt; } + Debug.Assert(dividend._bits != null); + if (trivialDivisor) { uint rest; @@ -763,6 +765,8 @@ namespace System.Numerics return new BigInteger(bits, (dividend._sign < 0) ^ (divisor._sign < 0)); } + Debug.Assert(divisor._bits != null); + if (dividend._bits.Length < divisor._bits.Length) { remainder = dividend; @@ -835,6 +839,7 @@ namespace System.Numerics if (trivialLeft) { + Debug.Assert(right._bits != null); return left._sign != 0 ? BigIntegerCalculator.Gcd(right._bits, NumericsHelpers.Abs(left._sign)) : new BigInteger(right._bits, false); @@ -842,11 +847,14 @@ namespace System.Numerics if (trivialRight) { + Debug.Assert(left._bits != null); return right._sign != 0 ? BigIntegerCalculator.Gcd(left._bits, NumericsHelpers.Abs(right._sign)) : new BigInteger(left._bits, false); } + Debug.Assert(left._bits != null && right._bits != null); + if (BigIntegerCalculator.Compare(left._bits, right._bits) < 0) { return GreatestCommonDivisor(right._bits, left._bits); @@ -912,18 +920,18 @@ namespace System.Numerics if (trivialModulus) { uint bits = trivialValue && trivialExponent ? BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), NumericsHelpers.Abs(exponent._sign), NumericsHelpers.Abs(modulus._sign)) : - trivialValue ? BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), exponent._bits, NumericsHelpers.Abs(modulus._sign)) : - trivialExponent ? BigIntegerCalculator.Pow(value._bits, NumericsHelpers.Abs(exponent._sign), NumericsHelpers.Abs(modulus._sign)) : - BigIntegerCalculator.Pow(value._bits, exponent._bits, NumericsHelpers.Abs(modulus._sign)); + trivialValue ? BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), exponent._bits!, NumericsHelpers.Abs(modulus._sign)) : + trivialExponent ? BigIntegerCalculator.Pow(value._bits!, NumericsHelpers.Abs(exponent._sign), NumericsHelpers.Abs(modulus._sign)) : + BigIntegerCalculator.Pow(value._bits!, exponent._bits!, NumericsHelpers.Abs(modulus._sign)); return value._sign < 0 && !exponent.IsEven ? -1 * bits : bits; } else { - uint[] bits = trivialValue && trivialExponent ? BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), NumericsHelpers.Abs(exponent._sign), modulus._bits) : - trivialValue ? BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), exponent._bits, modulus._bits) : - trivialExponent ? BigIntegerCalculator.Pow(value._bits, NumericsHelpers.Abs(exponent._sign), modulus._bits) : - BigIntegerCalculator.Pow(value._bits, exponent._bits, modulus._bits); + uint[] bits = trivialValue && trivialExponent ? BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), NumericsHelpers.Abs(exponent._sign), modulus._bits!) : + trivialValue ? BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), exponent._bits!, modulus._bits!) : + trivialExponent ? BigIntegerCalculator.Pow(value._bits!, NumericsHelpers.Abs(exponent._sign), modulus._bits!) : + BigIntegerCalculator.Pow(value._bits!, exponent._bits!, modulus._bits!); return new BigInteger(bits, value._sign < 0 && !exponent.IsEven); } @@ -955,7 +963,7 @@ namespace System.Numerics uint[] bits = trivialValue ? BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), NumericsHelpers.Abs(exponent)) - : BigIntegerCalculator.Pow(value._bits, NumericsHelpers.Abs(exponent)); + : BigIntegerCalculator.Pow(value._bits!, NumericsHelpers.Abs(exponent)); return new BigInteger(bits, value._sign < 0 && (exponent & 1) != 0); } @@ -972,7 +980,7 @@ namespace System.Numerics return hash; } - public override bool Equals(object obj) + public override bool Equals(object? obj) { AssertValid(); @@ -1097,7 +1105,7 @@ namespace System.Numerics return _bits[cuDiff - 1] < other._bits[cuDiff - 1] ? -_sign : _sign; } - public int CompareTo(object obj) + public int CompareTo(object? obj) { if (obj == null) return 1; @@ -1153,7 +1161,7 @@ namespace System.Numerics public byte[] ToByteArray(bool isUnsigned = false, bool isBigEndian = false) { int ignored = 0; - return TryGetBytes(GetBytesMode.AllocateArray, default, isUnsigned, isBigEndian, ref ignored); + return TryGetBytes(GetBytesMode.AllocateArray, default, isUnsigned, isBigEndian, ref ignored)!; } /// @@ -1217,7 +1225,7 @@ namespace System.Numerics /// If ==, non-null if the span was long enough, null if there wasn't enough room. /// /// If is true and is negative. - private byte[] TryGetBytes(GetBytesMode mode, Span destination, bool isUnsigned, bool isBigEndian, ref int bytesWritten) + private byte[]? TryGetBytes(GetBytesMode mode, Span destination, bool isUnsigned, bool isBigEndian, ref int bytesWritten) { Debug.Assert(mode == GetBytesMode.AllocateArray || mode == GetBytesMode.Count || mode == GetBytesMode.Span, $"Unexpected mode {mode}."); Debug.Assert(mode == GetBytesMode.Span || destination.IsEmpty, $"If we're not in span mode, we shouldn't have been passed a destination."); @@ -1251,7 +1259,7 @@ namespace System.Numerics byte highByte; int nonZeroDwordIndex = 0; uint highDword; - uint[] bits = _bits; + uint[]? bits = _bits; if (bits == null) { highByte = (byte)((sign < 0) ? 0xff : 0x00); @@ -1452,27 +1460,27 @@ namespace System.Numerics return BigNumber.FormatBigInteger(this, null, NumberFormatInfo.CurrentInfo); } - public string ToString(IFormatProvider provider) + public string ToString(IFormatProvider? provider) { return BigNumber.FormatBigInteger(this, null, NumberFormatInfo.GetInstance(provider)); } - public string ToString(string format) + public string ToString(string? format) { return BigNumber.FormatBigInteger(this, format, NumberFormatInfo.CurrentInfo); } - public string ToString(string format, IFormatProvider provider) + public string ToString(string? format, IFormatProvider? provider) { return BigNumber.FormatBigInteger(this, format, NumberFormatInfo.GetInstance(provider)); } - public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider provider = null) // TODO: change format to ReadOnlySpan + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) // TODO: change format to ReadOnlySpan { return BigNumber.TryFormatBigInteger(this, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten); } - private static BigInteger Add(uint[] leftBits, int leftSign, uint[] rightBits, int rightSign) + private static BigInteger Add(uint[]? leftBits, int leftSign, uint[]? rightBits, int rightSign) { bool trivialLeft = leftBits == null; bool trivialRight = rightBits == null; @@ -1484,16 +1492,20 @@ namespace System.Numerics if (trivialLeft) { + Debug.Assert(rightBits != null); uint[] bits = BigIntegerCalculator.Add(rightBits, NumericsHelpers.Abs(leftSign)); return new BigInteger(bits, leftSign < 0); } if (trivialRight) { + Debug.Assert(leftBits != null); uint[] bits = BigIntegerCalculator.Add(leftBits, NumericsHelpers.Abs(rightSign)); return new BigInteger(bits, leftSign < 0); } + Debug.Assert(leftBits != null && rightBits != null); + if (leftBits.Length < rightBits.Length) { uint[] bits = BigIntegerCalculator.Add(rightBits, leftBits); @@ -1516,7 +1528,7 @@ namespace System.Numerics return Subtract(left._bits, left._sign, right._bits, right._sign); } - private static BigInteger Subtract(uint[] leftBits, int leftSign, uint[] rightBits, int rightSign) + private static BigInteger Subtract(uint[]? leftBits, int leftSign, uint[]? rightBits, int rightSign) { bool trivialLeft = leftBits == null; bool trivialRight = rightBits == null; @@ -1528,16 +1540,20 @@ namespace System.Numerics if (trivialLeft) { + Debug.Assert(rightBits != null); uint[] bits = BigIntegerCalculator.Subtract(rightBits, NumericsHelpers.Abs(leftSign)); return new BigInteger(bits, leftSign >= 0); } if (trivialRight) { + Debug.Assert(leftBits != null); uint[] bits = BigIntegerCalculator.Subtract(leftBits, NumericsHelpers.Abs(rightSign)); return new BigInteger(bits, leftSign < 0); } + Debug.Assert(leftBits != null && rightBits != null); + if (BigIntegerCalculator.Compare(leftBits, rightBits) < 0) { uint[] bits = BigIntegerCalculator.Subtract(rightBits, leftBits); @@ -1738,7 +1754,7 @@ namespace System.Numerics value.AssertValid(); int sign = value._sign; - uint[] bits = value._bits; + uint[]? bits = value._bits; if (bits == null) return sign; @@ -2015,16 +2031,20 @@ namespace System.Numerics if (trivialLeft) { + Debug.Assert(right._bits != null); uint[] bits = BigIntegerCalculator.Multiply(right._bits, NumericsHelpers.Abs(left._sign)); return new BigInteger(bits, (left._sign < 0) ^ (right._sign < 0)); } if (trivialRight) { + Debug.Assert(left._bits != null); uint[] bits = BigIntegerCalculator.Multiply(left._bits, NumericsHelpers.Abs(right._sign)); return new BigInteger(bits, (left._sign < 0) ^ (right._sign < 0)); } + Debug.Assert(left._bits != null && right._bits != null); + if (left._bits == right._bits) { uint[] bits = BigIntegerCalculator.Square(left._bits); @@ -2065,10 +2085,13 @@ namespace System.Numerics if (trivialDivisor) { + Debug.Assert(dividend._bits != null); uint[] bits = BigIntegerCalculator.Divide(dividend._bits, NumericsHelpers.Abs(divisor._sign)); return new BigInteger(bits, (dividend._sign < 0) ^ (divisor._sign < 0)); } + Debug.Assert(dividend._bits != null && divisor._bits != null); + if (dividend._bits.Length < divisor._bits.Length) { return s_bnZeroInt; @@ -2102,10 +2125,13 @@ namespace System.Numerics if (trivialDivisor) { + Debug.Assert(dividend._bits != null); uint remainder = BigIntegerCalculator.Remainder(dividend._bits, NumericsHelpers.Abs(divisor._sign)); return dividend._sign < 0 ? -1 * remainder : remainder; } + Debug.Assert(dividend._bits != null && divisor._bits != null); + if (dividend._bits.Length < divisor._bits.Length) { return dividend; diff --git a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigNumber.cs b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigNumber.cs index 07768fd..4bf034d 100644 --- a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigNumber.cs +++ b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigNumber.cs @@ -273,6 +273,7 @@ using System.Buffers; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Text; @@ -302,7 +303,7 @@ namespace System.Numerics } - internal static bool TryValidateParseStyleInteger(NumberStyles style, out ArgumentException e) + internal static bool TryValidateParseStyleInteger(NumberStyles style, [NotNullWhen(false)] out ArgumentException? e) { // Check for undefined flags if ((style & InvalidNumberStyles) != 0) @@ -322,7 +323,7 @@ namespace System.Numerics return true; } - internal static bool TryParseBigInteger(string value, NumberStyles style, NumberFormatInfo info, out BigInteger result) + internal static bool TryParseBigInteger(string? value, NumberStyles style, NumberFormatInfo info, out BigInteger result) { if (value == null) { @@ -338,7 +339,7 @@ namespace System.Numerics unsafe { result = BigInteger.Zero; - ArgumentException e; + ArgumentException? e; if (!TryValidateParseStyleInteger(style, out e)) throw e; // TryParse still throws ArgumentException on invalid NumberStyles @@ -376,7 +377,7 @@ namespace System.Numerics internal static BigInteger ParseBigInteger(ReadOnlySpan value, NumberStyles style, NumberFormatInfo info) { - ArgumentException e; + ArgumentException? e; if (!TryValidateParseStyleInteger(style, out e)) throw e; @@ -503,12 +504,12 @@ namespace System.Numerics return (char)0; // Custom format } - private static string FormatBigIntegerToHex(bool targetSpan, BigInteger value, char format, int digits, NumberFormatInfo info, Span destination, out int charsWritten, out bool spanSuccess) + private static string? FormatBigIntegerToHex(bool targetSpan, BigInteger value, char format, int digits, NumberFormatInfo info, Span destination, out int charsWritten, out bool spanSuccess) { Debug.Assert(format == 'x' || format == 'X'); // Get the bytes that make up the BigInteger. - byte[] arrayToReturnToPool = null; + byte[]? arrayToReturnToPool = null; Span bits = stackalloc byte[64]; // arbitrary threshold if (!value.TryWriteOrCountBytes(bits, out int bytesWrittenOrNeeded)) { @@ -587,9 +588,9 @@ namespace System.Numerics } } - internal static string FormatBigInteger(BigInteger value, string format, NumberFormatInfo info) + internal static string FormatBigInteger(BigInteger value, string? format, NumberFormatInfo info) { - return FormatBigInteger(targetSpan: false, value, format, format, info, default, out _, out _); + return FormatBigInteger(targetSpan: false, value, format, format, info, default, out _, out _)!; } internal static bool TryFormatBigInteger(BigInteger value, ReadOnlySpan format, NumberFormatInfo info, Span destination, out int charsWritten) @@ -598,9 +599,9 @@ namespace System.Numerics return spanSuccess; } - private static string FormatBigInteger( + private static string? FormatBigInteger( bool targetSpan, BigInteger value, - string formatString, ReadOnlySpan formatSpan, + string? formatString, ReadOnlySpan formatSpan, NumberFormatInfo info, Span destination, out int charsWritten, out bool spanSuccess) { Debug.Assert(formatString == null || formatString.Length == formatSpan.Length); diff --git a/src/libraries/System.Runtime.Numerics/src/System/Numerics/Complex.cs b/src/libraries/System.Runtime.Numerics/src/System/Numerics/Complex.cs index d94653ed..e5e0d63 100644 --- a/src/libraries/System.Runtime.Numerics/src/System/Numerics/Complex.cs +++ b/src/libraries/System.Runtime.Numerics/src/System/Numerics/Complex.cs @@ -369,7 +369,7 @@ namespace System.Numerics return left.m_real != right.m_real || left.m_imaginary != right.m_imaginary; } - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (!(obj is Complex)) return false; return Equals((Complex)obj); @@ -394,17 +394,17 @@ namespace System.Numerics return string.Format(CultureInfo.CurrentCulture, "({0}, {1})", m_real, m_imaginary); } - public string ToString(string format) + public string ToString(string? format) { return string.Format(CultureInfo.CurrentCulture, "({0}, {1})", m_real.ToString(format, CultureInfo.CurrentCulture), m_imaginary.ToString(format, CultureInfo.CurrentCulture)); } - public string ToString(IFormatProvider provider) + public string ToString(IFormatProvider? provider) { return string.Format(provider, "({0}, {1})", m_real, m_imaginary); } - public string ToString(string format, IFormatProvider provider) + public string ToString(string? format, IFormatProvider? provider) { return string.Format(provider, "({0}, {1})", m_real.ToString(format, provider), m_imaginary.ToString(format, provider)); }