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

Updating generic math to support user-defined checked operators #67714

Merged
3 changes: 2 additions & 1 deletion eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,8 @@ dotnet_diagnostic.IL3002.severity = warning
dotnet_diagnostic.SA0001.severity = none

# SA1000: Spacing around keywords
dotnet_diagnostic.SA1000.severity = warning
# suggestion until https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3478 is resolved
dotnet_diagnostic.SA1000.severity = suggestion

# SA1001: Commas should not be preceded by whitespace
dotnet_diagnostic.SA1001.severity = warning
Expand Down
14 changes: 7 additions & 7 deletions src/libraries/System.Private.CoreLib/src/System/Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
static byte IAdditionOperators<byte, byte, byte>.operator +(byte left, byte right) => (byte)(left + right);

// /// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_CheckedAddition(TSelf, TOther)" />
// static byte IAdditionOperators<byte, byte, byte>.operator checked +(byte left, byte right) => checked((byte)(left + right));
static byte IAdditionOperators<byte, byte, byte>.operator checked +(byte left, byte right) => checked((byte)(left + right));
tannergooding marked this conversation as resolved.
Show resolved Hide resolved

//
// IAdditiveIdentity
Expand Down Expand Up @@ -382,7 +382,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
static byte IDecrementOperators<byte>.operator --(byte value) => --value;

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

//
// IDivisionOperators
Expand All @@ -392,7 +392,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
static byte IDivisionOperators<byte, byte, byte>.operator /(byte left, byte right) => (byte)(left / right);

// /// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
// static byte IDivisionOperators<byte, byte, byte>.operator checked /(byte left, byte right) => checked((byte)(left / right));
static byte IDivisionOperators<byte, byte, byte>.operator checked /(byte left, byte right) => checked((byte)(left / right));
tannergooding marked this conversation as resolved.
Show resolved Hide resolved

//
// IEqualityOperators
Expand All @@ -412,7 +412,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
static byte IIncrementOperators<byte>.operator ++(byte value) => ++value;

// /// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
tannergooding marked this conversation as resolved.
Show resolved Hide resolved
// static byte IIncrementOperators<byte>.operator checked ++(byte value) => checked(++value);
static byte IIncrementOperators<byte>.operator checked ++(byte value) => checked(++value);

//
// IMinMaxValue
Expand Down Expand Up @@ -446,7 +446,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
static byte IMultiplyOperators<byte, byte, byte>.operator *(byte left, byte right) => (byte)(left * right);

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

//
// INumber
Expand Down Expand Up @@ -938,7 +938,7 @@ public static bool TryCreate<TOther>(TOther value, out byte result)
static byte ISubtractionOperators<byte, byte, byte>.operator -(byte left, byte right) => (byte)(left - right);

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

//
// IUnaryNegationOperators
Expand All @@ -948,7 +948,7 @@ public static bool TryCreate<TOther>(TOther value, out byte result)
static byte IUnaryNegationOperators<byte, byte>.operator -(byte value) => (byte)(-value);

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

//
// IUnaryPlusOperators
Expand Down
14 changes: 7 additions & 7 deletions src/libraries/System.Private.CoreLib/src/System/Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ public static int ConvertToUtf32(string s, int index)
static char IAdditionOperators<char, char, char>.operator +(char left, char right) => (char) (left + right);

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

//
// IAdditiveIdentity
Expand Down Expand Up @@ -1150,7 +1150,7 @@ public static int ConvertToUtf32(string s, int index)
static char IDecrementOperators<char>.operator --(char value) => --value;

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

//
// IDivisionOperators
Expand All @@ -1160,7 +1160,7 @@ public static int ConvertToUtf32(string s, int index)
static char IDivisionOperators<char, char, char>.operator /(char left, char right) => (char)(left / right);

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

//
// IEqualityOperators
Expand All @@ -1180,7 +1180,7 @@ public static int ConvertToUtf32(string s, int index)
static char IIncrementOperators<char>.operator ++(char value) => ++value;

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

//
// IMinMaxValue
Expand Down Expand Up @@ -1214,7 +1214,7 @@ public static int ConvertToUtf32(string s, int index)
static char IMultiplyOperators<char, char, char>.operator *(char left, char right) => (char)(left * right);

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

//
// INumber
Expand Down Expand Up @@ -1734,7 +1734,7 @@ static bool ISpanParsable<char>.TryParse(ReadOnlySpan<char> s, IFormatProvider?
static char ISubtractionOperators<char, char, char>.operator -(char left, char right) => (char)(left - right);

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

//
// IUnaryNegationOperators
Expand All @@ -1744,7 +1744,7 @@ static bool ISpanParsable<char>.TryParse(ReadOnlySpan<char> s, IFormatProvider?
static char IUnaryNegationOperators<char, char>.operator -(char value) => (char)(-value);

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

//
// IUnaryPlusOperators
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ internal static bool TryCreate(int year, int month, int day, int hour, int minut
//

// /// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
// static DateTime IAdditionOperators<DateTime, TimeSpan, DateTime>.operator checked +(DateTime left, TimeSpan right) => checked(left + right);
static DateTime IAdditionOperators<DateTime, TimeSpan, DateTime>.operator checked +(DateTime left, TimeSpan right) => left + right;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting. DateTime.MaxValue + TimeSpan.FromMilliseconds(1) will throw an exception, regardless of checked. In a generic math context, that feels a little strange, but I'm not sure there's a better answer (as long as we continue to want these only-slightly-math-related types implementing these interfaces).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is an area where we'll want to consider the implications of there now being user-defined checked operators for certain core types (like DateTime and similar).

One could imagine us "properly" supporting checked/unchecked contexts for these types now. One could also imagine us continuing to always throw on overflow.

checked(expr) doesn't necessarily mean that expr will throw on overflow; and unchecked(expr) doesn't necessarily mean that expr will not throw on overflow. It really depends on the type and what operations it decides to support.

We'll probably want to decide on guidance as well, including the implications of taking the behavioral break for already compiled dependents.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have the option of saying:

DateTime always overflows when used as a concrete type but when used in a generic math context it properly respects checked/unchecked. We can always have the public APIs and the explicit interface implementations SxS, just needing to have API review in agreement that's overall good/desirable

Copy link
Member

@stephentoub stephentoub Apr 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Seems like we basically have four options:

  1. Don't implement the generic math interfaces on these types.
  2. Leave the behavior as is. Someone using these types in a generic context might experience exceptions for "overflow" (but not actually OverflowException) even when in unchecked.
  3. Implement the new unchecked operators on these types to not throw. This means someone using these in a generic context will have more consistent behavior regardless of the concrete type being used, but it yields an inconsistency between how the type behaves when using it directly vs generically.
  4. (3) and change the existing behavior of the existing operators to be non-throwing. That's a breaking change in that someone could easily be relying on that behavior to stop "bad things" from happening.

We don't need to decide now, but please ensure there's an issue open to track making a decision around this for .NET 7.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will log an issue tracking this before merging.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


//
// IAdditiveIdentity
Expand Down Expand Up @@ -1551,9 +1551,9 @@ internal static bool TryCreate(int year, int month, int day, int hour, int minut
//

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

// /// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
// static TimeSpan ISubtractionOperators<DateTime, DateTime, TimeSpan>.operator checked -(DateTime left, DateTime right) => checked(left - right);
static TimeSpan ISubtractionOperators<DateTime, DateTime, TimeSpan>.operator checked -(DateTime left, DateTime right) => left - right;
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ public static implicit operator DateTimeOffset(DateTime dateTime) =>
//

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

//
// IAdditiveIdentity
Expand Down Expand Up @@ -907,9 +907,9 @@ public static implicit operator DateTimeOffset(DateTime dateTime) =>
//

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

// /// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
// static TimeSpan ISubtractionOperators<DateTimeOffset, DateTimeOffset, TimeSpan>.operator checked -(DateTimeOffset left, DateTimeOffset right) => checked(left - right);
static TimeSpan ISubtractionOperators<DateTimeOffset, DateTimeOffset, TimeSpan>.operator checked -(DateTimeOffset left, DateTimeOffset right) => left - right;
}
}
14 changes: 7 additions & 7 deletions src/libraries/System.Private.CoreLib/src/System/Decimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
//

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

//
// IAdditiveIdentity
Expand All @@ -1097,21 +1097,21 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
//

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

//
// IDivisionOperators
//

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

//
// IIncrementOperators
//

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

//
// IMinMaxValue
Expand All @@ -1135,7 +1135,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
//

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

//
// INumber
Expand Down Expand Up @@ -1505,13 +1505,13 @@ public static bool TryCreate<TOther>(TOther value, out decimal result)
//

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

//
// IUnaryNegationOperators
//

// /// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
// static decimal IUnaryNegationOperators<decimal, decimal>.operator checked -(decimal value) => checked(-value);
static decimal IUnaryNegationOperators<decimal, decimal>.operator checked -(decimal value) => -value;
}
}
20 changes: 10 additions & 10 deletions src/libraries/System.Private.CoreLib/src/System/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
static double IAdditionOperators<double, double, double>.operator +(double left, double right) => left + right;

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

//
// IAdditiveIdentity
Expand Down Expand Up @@ -561,7 +561,7 @@ public static bool IsPow2(double value)
static double IDecrementOperators<double>.operator --(double value) => --value;

// /// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
// static double IDecrementOperators<double>.operator checked --(double value) => checked(--value);
static double IDecrementOperators<double>.operator checked --(double value) => --value;

//
// IDivisionOperators
Expand All @@ -571,7 +571,7 @@ public static bool IsPow2(double value)
static double IDivisionOperators<double, double, double>.operator /(double left, double right) => left / right;

// /// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
// static double IDivisionOperators<double, double, double>.operator checked /(double left, double right) => checked(left / right);
static double IDivisionOperators<double, double, double>.operator checked /(double left, double right) => left / right;

//
// IExponentialFunctions
Expand Down Expand Up @@ -717,7 +717,7 @@ public static bool IsPow2(double value)
static double IIncrementOperators<double>.operator ++(double value) => ++value;

// /// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
// static double IIncrementOperators<double>.operator checked ++(double value) => checked(++value);
static double IIncrementOperators<double>.operator checked ++(double value) => ++value;

//
// ILogarithmicFunctions
Expand Down Expand Up @@ -772,10 +772,10 @@ public static bool IsPow2(double value)
//

/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_Multiply(TSelf, TOther)" />
static double IMultiplyOperators<double, double, double>.operator *(double left, double right) => (double)(left * right);
static double IMultiplyOperators<double, double, double>.operator *(double left, double right) => left * right;

// /// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
// static double IMultiplyOperators<double, double, double>.operator checked *(double left, double right) => checked((double)(left * right));
static double IMultiplyOperators<double, double, double>.operator checked *(double left, double right) => left * right;

//
// INumber
Expand Down Expand Up @@ -1153,10 +1153,10 @@ public static bool TryCreate<TOther>(TOther value, out double result)
//

/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_Subtraction(TSelf, TOther)" />
static double ISubtractionOperators<double, double, double>.operator -(double left, double right) => (double)(left - right);
static double ISubtractionOperators<double, double, double>.operator -(double left, double right) => left - right;

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

//
// ITrigonometricFunctions
Expand Down Expand Up @@ -1212,10 +1212,10 @@ public static bool TryCreate<TOther>(TOther value, out double result)
//

/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_UnaryNegation(TSelf)" />
static double IUnaryNegationOperators<double, double>.operator -(double value) => (double)(-value);
static double IUnaryNegationOperators<double, double>.operator -(double value) => -value;

// /// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
// static double IUnaryNegationOperators<double, double>.operator checked -(double value) => checked((double)(-value));
static double IUnaryNegationOperators<double, double>.operator checked -(double value) => -value;

//
// IUnaryPlusOperators
Expand Down
Loading