Skip to content

Commit 7c21927

Browse files
Updating generic math to support user-defined checked operators (#67714)
* Uncomment the user-defined checked operators for generic math * Change SA1000 to be a suggestion until DotNetAnalyzers/StyleCopAnalyzers#3478 can be resolved * Regenerating the System.Runtime ref assembly * Adding tests covering user-defined checked operators in generic math * Remove the checked() context from integer division, since there is no behavioral difference on .NET * Ensure the doc comments around user-defined checked operators are uncommented * Fix some doc comments that shouldn't have been uncommented yet
1 parent c093bb8 commit 7c21927

40 files changed

+2392
-1185
lines changed

eng/CodeAnalysis.src.globalconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,8 @@ dotnet_diagnostic.IL3002.severity = warning
866866
dotnet_diagnostic.SA0001.severity = none
867867

868868
# SA1000: Spacing around keywords
869-
dotnet_diagnostic.SA1000.severity = warning
869+
# suggestion until https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3478 is resolved
870+
dotnet_diagnostic.SA1000.severity = suggestion
870871

871872
# SA1001: Commas should not be preceded by whitespace
872873
dotnet_diagnostic.SA1001.severity = warning

src/libraries/System.Private.CoreLib/src/System/Byte.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
300300
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
301301
static byte IAdditionOperators<byte, byte, byte>.operator +(byte left, byte right) => (byte)(left + right);
302302

303-
// /// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_CheckedAddition(TSelf, TOther)" />
304-
// static byte IAdditionOperators<byte, byte, byte>.operator checked +(byte left, byte right) => checked((byte)(left + right));
303+
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_CheckedAddition(TSelf, TOther)" />
304+
static byte IAdditionOperators<byte, byte, byte>.operator checked +(byte left, byte right) => checked((byte)(left + right));
305305

306306
//
307307
// IAdditiveIdentity
@@ -381,8 +381,8 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
381381
/// <inheritdoc cref="IDecrementOperators{TSelf}.op_Decrement(TSelf)" />
382382
static byte IDecrementOperators<byte>.operator --(byte value) => --value;
383383

384-
// /// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
385-
// static byte IDecrementOperators<byte>.operator checked --(byte value) => checked(--value);
384+
/// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
385+
static byte IDecrementOperators<byte>.operator checked --(byte value) => checked(--value);
386386

387387
//
388388
// IDivisionOperators
@@ -391,8 +391,8 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
391391
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
392392
static byte IDivisionOperators<byte, byte, byte>.operator /(byte left, byte right) => (byte)(left / right);
393393

394-
// /// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
395-
// static byte IDivisionOperators<byte, byte, byte>.operator checked /(byte left, byte right) => checked((byte)(left / right));
394+
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
395+
static byte IDivisionOperators<byte, byte, byte>.operator checked /(byte left, byte right) => (byte)(left / right);
396396

397397
//
398398
// IEqualityOperators
@@ -411,8 +411,8 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
411411
/// <inheritdoc cref="IIncrementOperators{TSelf}.op_Increment(TSelf)" />
412412
static byte IIncrementOperators<byte>.operator ++(byte value) => ++value;
413413

414-
// /// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
415-
// static byte IIncrementOperators<byte>.operator checked ++(byte value) => checked(++value);
414+
/// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
415+
static byte IIncrementOperators<byte>.operator checked ++(byte value) => checked(++value);
416416

417417
//
418418
// IMinMaxValue
@@ -445,8 +445,8 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
445445
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_Multiply(TSelf, TOther)" />
446446
static byte IMultiplyOperators<byte, byte, byte>.operator *(byte left, byte right) => (byte)(left * right);
447447

448-
// /// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
449-
// static byte IMultiplyOperators<byte, byte, byte>.operator checked *(byte left, byte right) => checked((byte)(left * right));
448+
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
449+
static byte IMultiplyOperators<byte, byte, byte>.operator checked *(byte left, byte right) => checked((byte)(left * right));
450450

451451
//
452452
// INumber
@@ -937,8 +937,8 @@ public static bool TryCreate<TOther>(TOther value, out byte result)
937937
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_Subtraction(TSelf, TOther)" />
938938
static byte ISubtractionOperators<byte, byte, byte>.operator -(byte left, byte right) => (byte)(left - right);
939939

940-
// /// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
941-
// static byte ISubtractionOperators<byte, byte, byte>.operator checked -(byte left, byte right) => checked((byte)(left - right));
940+
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
941+
static byte ISubtractionOperators<byte, byte, byte>.operator checked -(byte left, byte right) => checked((byte)(left - right));
942942

943943
//
944944
// IUnaryNegationOperators
@@ -947,8 +947,8 @@ public static bool TryCreate<TOther>(TOther value, out byte result)
947947
/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_UnaryNegation(TSelf)" />
948948
static byte IUnaryNegationOperators<byte, byte>.operator -(byte value) => (byte)(-value);
949949

950-
// /// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
951-
// static byte IUnaryNegationOperators<byte, byte>.operator checked -(byte value) => checked((byte)(-value));
950+
/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
951+
static byte IUnaryNegationOperators<byte, byte>.operator checked -(byte value) => checked((byte)(-value));
952952

953953
//
954954
// IUnaryPlusOperators

src/libraries/System.Private.CoreLib/src/System/Char.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,8 @@ public static int ConvertToUtf32(string s, int index)
10681068
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
10691069
static char IAdditionOperators<char, char, char>.operator +(char left, char right) => (char) (left + right);
10701070

1071-
// /// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
1072-
// static char IAdditionOperators<char, char, char>.operator checked +(char left, char right) => checked((char)(left + right));
1071+
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
1072+
static char IAdditionOperators<char, char, char>.operator checked +(char left, char right) => checked((char)(left + right));
10731073

10741074
//
10751075
// IAdditiveIdentity
@@ -1149,8 +1149,8 @@ public static int ConvertToUtf32(string s, int index)
11491149
/// <inheritdoc cref="IDecrementOperators{TSelf}.op_Decrement(TSelf)" />
11501150
static char IDecrementOperators<char>.operator --(char value) => --value;
11511151

1152-
// /// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
1153-
// static char IDecrementOperators<char>.operator checked --(char value) => checked(--value);
1152+
/// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
1153+
static char IDecrementOperators<char>.operator checked --(char value) => checked(--value);
11541154

11551155
//
11561156
// IDivisionOperators
@@ -1159,8 +1159,8 @@ public static int ConvertToUtf32(string s, int index)
11591159
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
11601160
static char IDivisionOperators<char, char, char>.operator /(char left, char right) => (char)(left / right);
11611161

1162-
// /// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
1163-
// static char IDivisionOperators<char, char, char>.operator /(char left, char right) => checked((char)(left / right));
1162+
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
1163+
static char IDivisionOperators<char, char, char>.operator checked /(char left, char right) => (char)(left / right);
11641164

11651165
//
11661166
// IEqualityOperators
@@ -1179,8 +1179,8 @@ public static int ConvertToUtf32(string s, int index)
11791179
/// <inheritdoc cref="IIncrementOperators{TSelf}.op_Increment(TSelf)" />
11801180
static char IIncrementOperators<char>.operator ++(char value) => ++value;
11811181

1182-
// /// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
1183-
// static char IIncrementOperators<char>.operator checked ++(char value) => checked(++value);
1182+
/// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
1183+
static char IIncrementOperators<char>.operator checked ++(char value) => checked(++value);
11841184

11851185
//
11861186
// IMinMaxValue
@@ -1213,8 +1213,8 @@ public static int ConvertToUtf32(string s, int index)
12131213
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_Multiply(TSelf, TOther)" />
12141214
static char IMultiplyOperators<char, char, char>.operator *(char left, char right) => (char)(left * right);
12151215

1216-
// /// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
1217-
// static char IMultiplyOperators<char, char, char>.operator checked *(char left, char right) => checked((char)(left * right));
1216+
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
1217+
static char IMultiplyOperators<char, char, char>.operator checked *(char left, char right) => checked((char)(left * right));
12181218

12191219
//
12201220
// INumber
@@ -1733,8 +1733,8 @@ static bool ISpanParsable<char>.TryParse(ReadOnlySpan<char> s, IFormatProvider?
17331733
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_Subtraction(TSelf, TOther)" />
17341734
static char ISubtractionOperators<char, char, char>.operator -(char left, char right) => (char)(left - right);
17351735

1736-
// /// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
1737-
// static char ISubtractionOperators<char, char, char>.operator checked -(char left, char right) => checked((char)(left - right));
1736+
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
1737+
static char ISubtractionOperators<char, char, char>.operator checked -(char left, char right) => checked((char)(left - right));
17381738

17391739
//
17401740
// IUnaryNegationOperators
@@ -1743,8 +1743,8 @@ static bool ISpanParsable<char>.TryParse(ReadOnlySpan<char> s, IFormatProvider?
17431743
/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_UnaryNegation(TSelf)" />
17441744
static char IUnaryNegationOperators<char, char>.operator -(char value) => (char)(-value);
17451745

1746-
// /// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
1747-
// static char IUnaryNegationOperators<char, char>.operator checked -(char value) => checked((char)(-value));
1746+
/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
1747+
static char IUnaryNegationOperators<char, char>.operator checked -(char value) => checked((char)(-value));
17481748

17491749
//
17501750
// IUnaryPlusOperators

src/libraries/System.Private.CoreLib/src/System/DateTime.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,8 +1511,8 @@ internal static bool TryCreate(int year, int month, int day, int hour, int minut
15111511
// IAdditionOperators
15121512
//
15131513

1514-
// /// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
1515-
// static DateTime IAdditionOperators<DateTime, TimeSpan, DateTime>.operator checked +(DateTime left, TimeSpan right) => checked(left + right);
1514+
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
1515+
static DateTime IAdditionOperators<DateTime, TimeSpan, DateTime>.operator checked +(DateTime left, TimeSpan right) => left + right;
15161516

15171517
//
15181518
// IAdditiveIdentity
@@ -1550,10 +1550,10 @@ internal static bool TryCreate(int year, int month, int day, int hour, int minut
15501550
// ISubtractionOperators
15511551
//
15521552

1553-
// /// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
1554-
// static DateTime ISubtractionOperators<DateTime, TimeSpan, DateTime>.operator checked -(DateTime left, TimeSpan right) => checked(left - right);
1553+
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
1554+
static DateTime ISubtractionOperators<DateTime, TimeSpan, DateTime>.operator checked -(DateTime left, TimeSpan right) => left - right;
15551555

1556-
// /// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
1557-
// static TimeSpan ISubtractionOperators<DateTime, DateTime, TimeSpan>.operator checked -(DateTime left, DateTime right) => checked(left - right);
1556+
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
1557+
static TimeSpan ISubtractionOperators<DateTime, DateTime, TimeSpan>.operator checked -(DateTime left, DateTime right) => left - right;
15581558
}
15591559
}

src/libraries/System.Private.CoreLib/src/System/DateTimeOffset.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,8 @@ public static implicit operator DateTimeOffset(DateTime dateTime) =>
867867
// IAdditionOperators
868868
//
869869

870-
// /// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
871-
// static DateTimeOffset IAdditionOperators<DateTimeOffset, TimeSpan, DateTimeOffset>.operator checked +(DateTimeOffset left, TimeSpan right) => checked(left + right);
870+
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
871+
static DateTimeOffset IAdditionOperators<DateTimeOffset, TimeSpan, DateTimeOffset>.operator checked +(DateTimeOffset left, TimeSpan right) => left + right;
872872

873873
//
874874
// IAdditiveIdentity
@@ -906,10 +906,10 @@ public static implicit operator DateTimeOffset(DateTime dateTime) =>
906906
// ISubtractionOperators
907907
//
908908

909-
// /// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
910-
// static DateTimeOffset ISubtractionOperators<DateTimeOffset, TimeSpan, DateTimeOffset>.operator checked -(DateTimeOffset left, TimeSpan right) => checked(left - right);
909+
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
910+
static DateTimeOffset ISubtractionOperators<DateTimeOffset, TimeSpan, DateTimeOffset>.operator checked -(DateTimeOffset left, TimeSpan right) => left - right;
911911

912-
// /// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
913-
// static TimeSpan ISubtractionOperators<DateTimeOffset, DateTimeOffset, TimeSpan>.operator checked -(DateTimeOffset left, DateTimeOffset right) => checked(left - right);
912+
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
913+
static TimeSpan ISubtractionOperators<DateTimeOffset, DateTimeOffset, TimeSpan>.operator checked -(DateTimeOffset left, DateTimeOffset right) => left - right;
914914
}
915915
}

src/libraries/System.Private.CoreLib/src/System/Decimal.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,8 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
10821082
// IAdditionOperators
10831083
//
10841084

1085-
// /// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
1086-
// static decimal IAdditionOperators<decimal, decimal, decimal>.operator checked +(decimal left, decimal right) => checked(left + right);
1085+
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
1086+
static decimal IAdditionOperators<decimal, decimal, decimal>.operator checked +(decimal left, decimal right) => left + right;
10871087

10881088
//
10891089
// IAdditiveIdentity
@@ -1096,22 +1096,22 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
10961096
// IDecrementOperators
10971097
//
10981098

1099-
// /// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
1100-
// static decimal IDecrementOperators<decimal>.operator checked --(decimal value) => checked(--value);
1099+
/// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
1100+
static decimal IDecrementOperators<decimal>.operator checked --(decimal value) => --value;
11011101

11021102
//
11031103
// IDivisionOperators
11041104
//
11051105

1106-
// /// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
1107-
// static decimal IDivisionOperators<decimal, decimal, decimal>.operator checked /(decimal left, decimal right) => checked(left / right);
1106+
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
1107+
static decimal IDivisionOperators<decimal, decimal, decimal>.operator checked /(decimal left, decimal right) => left / right;
11081108

11091109
//
11101110
// IIncrementOperators
11111111
//
11121112

1113-
// /// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
1114-
// static decimal IIncrementOperators<decimal>.operator checked ++(decimal value) => checked(++value);
1113+
/// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
1114+
static decimal IIncrementOperators<decimal>.operator checked ++(decimal value) => ++value;
11151115

11161116
//
11171117
// IMinMaxValue
@@ -1134,8 +1134,8 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
11341134
// IMultiplyOperators
11351135
//
11361136

1137-
// /// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
1138-
// public static decimal operator checked *(decimal left, decimal right) => checked(left * right);
1137+
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
1138+
public static decimal operator checked *(decimal left, decimal right) => left * right;
11391139

11401140
//
11411141
// INumber
@@ -1504,14 +1504,14 @@ public static bool TryCreate<TOther>(TOther value, out decimal result)
15041504
// ISubtractionOperators
15051505
//
15061506

1507-
// /// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
1508-
// static decimal ISubtractionOperators<decimal, decimal, decimal>.operator checked -(decimal left, decimal right) => checked(left - right);
1507+
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
1508+
static decimal ISubtractionOperators<decimal, decimal, decimal>.operator checked -(decimal left, decimal right) => left - right;
15091509

15101510
//
15111511
// IUnaryNegationOperators
15121512
//
15131513

1514-
// /// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
1515-
// static decimal IUnaryNegationOperators<decimal, decimal>.operator checked -(decimal value) => checked(-value);
1514+
/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
1515+
static decimal IUnaryNegationOperators<decimal, decimal>.operator checked -(decimal value) => -value;
15161516
}
15171517
}

0 commit comments

Comments
 (0)