Skip to content

Commit 424a09c

Browse files
authored
Proposed changes to ECMA 335 for checked user-defined operators (#66714)
C# now supports defining `checked` variants of the following user-defined operators so that users can opt into or out of overflow behavior as appropriate: * The `++` and `--` unary operators [§11.7.14](https://github.com/dotnet/csharpstandard/blob/draft-v6/standard/expressions.md#11714-postfix-increment-and-decrement-operators) and [§11.8.6](https://github.com/dotnet/csharpstandard/blob/draft-v6/standard/expressions.md#1186-prefix-increment-and-decrement-operators). * The `-` unary operator [§11.8.3](https://github.com/dotnet/csharpstandard/blob/draft-v6/standard/expressions.md#1183-unary-minus-operator). * The `+`, `-`, `*`, and `/` binary operators [§11.9](https://github.com/dotnet/csharpstandard/blob/draft-v6/standard/expressions.md#119-arithmetic-operators). * Explicit conversion operators. Motivation: There is no way for a user to declare a type and support both checked and unchecked versions of an operator. This makes it hard to port various algorithms to use the proposed `generic math` interfaces exposed by the libraries team. Likewise, this makes it impossible to expose a type such as `Int128` or `UInt128` without the language simultaneously shipping its own support to avoid breaking changes. This change adds names for the new checked operators to the specification.
1 parent 74f5858 commit 424a09c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

docs/design/specs/Ecma-335-Augments.md

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This is a list of additions and edits to be made in ECMA-335 specifications. It
1515
- [Unsigned data conversion with overflow detection](#unsigned-data-conversion-with-overflow-detection)
1616
- [Ref field support](#ref-fields)
1717
- [Rules for IL rewriters](#rules-for-il-rewriters)
18+
- [Checked user-defined operators](#checked-user-defined-operators)
1819

1920
## Signatures
2021

@@ -1000,3 +1001,15 @@ There are apis such as `System.Runtime.CompilerServices.RuntimeHelpers.CreateSpa
10001001
2. That in the presence of IL rewriters that the RVA remains aligned. This section descibes metadata which will be processed by IL rewriters in order to maintain the required alignment.
10011002

10021003
In order to maintain alignment, if the field needs alignment to be preserved, the field must be of a type locally defined within the module which has a Pack (§II.10.7) value of the desired alignment. Unlike other uses of the .pack directive, in this circumstance the .pack specifies a minimum alignment.
1004+
1005+
## Checked user-defined operators
1006+
1007+
Section "I.10.3.1 Unary operators" of ECMA-335 adds *op_CheckedIncrement*, *op_CheckedDecrement*, *op_CheckedUnaryNegation* as the names for methods implementing checked `++`, `--` and `-` unary operators.
1008+
1009+
Section "I.10.3.2 Binary operators" of ECMA-335 adds *op_CheckedAddition*, *op_CheckedSubtraction*,
1010+
*op_CheckedMultiply*, *op_CheckedDivision* as the names for methods implementing checked `+`, `-`, `*`, and `/` binary operators.
1011+
1012+
Section "I.10.3.3 Conversion operators" of ECMA-335 adds *op_CheckedExplicit* as the name for a method
1013+
implementing checked explicit conversion operator.
1014+
1015+
A checked user-defined operator is expected to throw an exception when the result of an operation is too large to represent in the destination type. What does it mean to be too large actually depends on the nature of the destination type. Typically the exception thrown is a System.OverflowException.

0 commit comments

Comments
 (0)