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] Make default implementations of bitwise operators as obsoleted #14761

Merged
merged 1 commit into from Feb 21, 2018

Conversation

moiseev
Copy link
Contributor

@moiseev moiseev commented Feb 21, 2018

Fixes: https://bugs.swift.org/browse/SR-7019

Prior to Swift 4 and the new integers, there was a protocol called
BitwiseOperations. It was deemed not needed with the introduction of
the integer protocols. But, for backward compatibility, it was left in
the standard library as _BitwiseOperations with a conditionally
available typealias BitwiseOperations. That protocol declares only the
"non-muating" operators, the mutating ones (i.e. |= and friends) are
defined as free functions. They are implemented in terms of
_BitwiseOperations protocol requirements, which is reasonable.

In the new integer protocols we established a
convention to provide default implementations for non-mutating functions
in terms of mutating ones (exactly the opposite of a much earlier
decision made for BitwiseOperations). So now, when you define a type
that conforms to the FixedWidthInteger, even though both mutating and
non-mutating bitwise operations are required, the default implementation
of | comes from extension FixedWidthInteger, and the default
implementation of |= comes from the _BitwiseOperations free functions.
And they are mutually recursive. Hence the crash.

This commit breaks the recursion by marking default implementations for
| etc. in _BitwiseOperations obsoleted from Swift 4.1 onward. The
test is also added to make sure the change helps.

Fixes: https://bugs.swift.org/browse/SR-7019

Prior to Swift 4 and the new integers, there was a protocol called
`BitwiseOperations`. It was deemed not needed with the introduction of
the integer protocols. But, for backward compatibility, it was left in
the standard library as `_BitwiseOperations` with a conditionally
available typealias `BitwiseOperations`. That protocol declares only the
"non-muating" operators, the mutating ones (i.e. `|=` and friends) are
defined as free functions. They are implemented in terms of
`_BitwiseOperations` protocol requirements, which is reasonable.

In the new integer protocols we established a
convention to provide default implementations for non-mutating functions
in terms of mutating ones (exactly the opposite of a much earlier
decision made for `BitwiseOperations`). So now, when you define a type
that conforms to the `FixedWidthInteger`, even though both mutating and
non-mutating bitwise operations are required, the default implementation
of `|` comes from extension FixedWidthInteger, and the default
implementation of `|=` comes from the _BitwiseOperations free functions.
And they are mutually recursive. Hence the crash.

This commit breaks the recursion by marking default implementations for
`|` etc. in `_BitwiseOperations` obsoleted from Swift 4.1 onward. The
test is also added to make sure the change helps.
@moiseev
Copy link
Contributor Author

moiseev commented Feb 21, 2018

@swift-ci Please smoke test

@moiseev
Copy link
Contributor Author

moiseev commented Feb 21, 2018

@swift-ci Please Test Source Compatibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant