-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[stdlib][SR-4818] Add overflow assignment operators #15144
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -588,6 +588,63 @@ def assignmentOperatorComment(operator, fixedWidth): | |
/// - Parameters: | ||
/// - lhs: The value to divide. | ||
/// - rhs: The value to divide `lhs` by. `rhs` must not be zero. | ||
""", | ||
'&+': """\ | ||
/// Stores the sum of the two given values in the left-hand-size variable, discarding any overflow. | ||
/// | ||
/// The masking addition operator (`&+`) silently discards any overflow that | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please modify this text so that it talks about the assignment operator you're documenting. |
||
/// occurs during the operation. In the following example, the sum of `100` | ||
/// and `121` is greater than the maximum representable `Int8` value, so the | ||
/// result is the overflowed value: | ||
/// | ||
/// var x: Int8 = 10 | ||
/// x &+= 21 | ||
/// // x == 31 | ||
/// var y: Int8 = 100 | ||
/// y &+= 121 | ||
/// // y == -35 (after overflow) | ||
/// | ||
/// - Parameters: | ||
/// - lhs: The first value to add. | ||
/// - rhs: The second value to add. | ||
""", | ||
'&-': """\ | ||
/// Stores the difference of the two given values in the left-hand-size variable, discarding any overflow. | ||
/// | ||
/// The masking subtraction operator (`&-`) silently discards any overflow | ||
/// that occurs during the operation. In the following example, the | ||
/// difference of `10` and `21` is less than zero, the minimum representable | ||
/// `UInt` value, so the result is the overflowed value: | ||
/// | ||
/// var x: Int8 = 21l | ||
/// x &-= 10 | ||
/// // x == 11 | ||
/// var y: UInt8 = 10 | ||
/// y &-= 21 | ||
/// // y == 245 (after overflow) | ||
/// | ||
/// - Parameters: | ||
/// - lhs: A numeric value. | ||
/// - rhs: The value to subtract from `lhs`. | ||
""", | ||
'&*': """\ | ||
/// Stores the product of the two given values in the left-hand-size variable, discarding any overflow. | ||
/// | ||
/// The masking multiplication operator (`&*`) silently discards any overflow | ||
/// that occurs during the operation. In the following example, the product | ||
/// of `10` and `50` is greater than the maximum representable `Int8` value, | ||
/// so the result is the overflowed value: | ||
/// | ||
/// var x: Int8 = 10 | ||
/// x &*= 5 | ||
/// // x == 50 | ||
/// var y: Int8 = 10 | ||
/// y &*= 50 | ||
/// // y == -12 (after overflow) | ||
/// | ||
/// - Parameters: | ||
/// - lhs: The first value to multiply. | ||
/// - rhs: The second value to multiply. | ||
""", | ||
'&': """\ | ||
/// Stores the result of performing a bitwise AND operation on the two given | ||
|
@@ -2740,8 +2797,18 @@ ${unsafeOperationComment(x.operator)} | |
${operatorComment('&' + x.operator, True)} | ||
@_inlineable // FIXME(sil-serialize-all) | ||
@_transparent | ||
public static func &${x.operator} (lhs: Self, rhs: Self) -> Self { | ||
return lhs.${x.name}ReportingOverflow(${callLabel}rhs).partialValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These implementations can be left alone. No need to have the compiler do more work than is necessary to arrive at the same result. |
||
public static func &${x.operator}(lhs: Self, rhs: Self) -> Self { | ||
var lhs = lhs | ||
lhs &${x.operator}= rhs | ||
return lhs | ||
} | ||
|
||
${assignmentOperatorComment('&' + x.operator, True)} | ||
@_inlineable // FIXME(sil-serialize-all) | ||
@_transparent | ||
public static func &${x.operator}=(lhs: inout Self, rhs: Self) { | ||
let result = lhs.${x.name}ReportingOverflow(${callLabel}rhs).partialValue | ||
lhs = result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a one-liner; no need for the temporary result. |
||
} | ||
% end | ||
% end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,7 +122,7 @@ def all_integer_or_real_binary_operator_names(): | |
|
||
|
||
def all_integer_assignment_operator_names(): | ||
return ['%=', '<<=', '>>=', '&=', '^=', '|='] | ||
return ['%=', '<<=', '>>=', '&*=', '&=', '&+=', '&-=', '^=', '|='] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these changes are required here. For example, |
||
|
||
|
||
def all_integer_or_real_assignment_operator_names(): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please wrap to 80 columns and follow the style of the documentation above; that is, "Adds the two values and stores..."