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

[stdlib][SR-4818] Add overflow assignment operators #15144

Merged
merged 4 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions stdlib/public/core/Integers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Collaborator

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..."

///
/// The masking addition operator (`&+`) silently discards any overflow that
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down
3 changes: 3 additions & 0 deletions stdlib/public/core/Policy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,13 @@ infix operator || : LogicalDisjunctionPrecedence
// Compound

infix operator *= : AssignmentPrecedence
infix operator &*= : AssignmentPrecedence
infix operator /= : AssignmentPrecedence
infix operator %= : AssignmentPrecedence
infix operator += : AssignmentPrecedence
infix operator &+= : AssignmentPrecedence
infix operator -= : AssignmentPrecedence
infix operator &-= : AssignmentPrecedence
infix operator <<= : AssignmentPrecedence
infix operator &<<= : AssignmentPrecedence
infix operator >>= : AssignmentPrecedence
Expand Down
2 changes: 1 addition & 1 deletion utils/SwiftIntTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def all_integer_or_real_binary_operator_names():


def all_integer_assignment_operator_names():
return ['%=', '<<=', '>>=', '&=', '^=', '|=']
return ['%=', '<<=', '>>=', '&*=', '&=', '&+=', '&-=', '^=', '|=']
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think these changes are required here. For example, &<<= isn't present.



def all_integer_or_real_assignment_operator_names():
Expand Down