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

[SR-12413] 5.2 regression: ClosedRange.Bound is no longer equivalent to ClosedRange.Element #54851

Open
WFT opened this issue Mar 25, 2020 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself standard library Area: Standard library umbrella

Comments

@WFT
Copy link

WFT commented Mar 25, 2020

Previous ID SR-12413
Radar rdar://problem/62200967
Original Reporter @WFT
Type Bug

Attachment: Download

Environment

Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
Target: x86_64-apple-darwin19.3.0

Additional Detail from JIRA
Votes 0
Component/s Compiler, Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 57de20a533b6bdbf83615c11ea315b34

Issue Description:

In Swft 5.1, this compiles just fine:

extension ClosedRange {
    /// Return the argument clamped to this range.
    /// Specifically, returns: `max(min(e, self.upperBound), self.lowerBound)`.
    func clamp(_ e: Element) -> Element {
        return Swift.max(Swift.min(e, upperBound), lowerBound)
    }
}

print((0...1).clamp(10) as Double)

But in Swift 5.2, these errors are shown:

closed-range.swift:4:33: error: type 'Bound' does not conform to protocol 'Strideable'
    func clamp(_ e: Element) -> Element {
                                ^
closed-range.swift:4:21: error: type 'Bound' does not conform to protocol 'Strideable'
    func clamp(_ e: Element) -> Element {
                    ^

The errors can be worked around by replacing Element with Bound in the original example. That's odd because Element is a typealias for Bound: https://developer.apple.com/documentation/swift/closedrange/element

That documentation says:
Available when Bound conforms to Strideable and Bound.Stride conforms to SignedInteger.

Not really sure what's going on here. I can understand why the compiler might prefer Bound over Element here (Element is really for the Sequence conformance) and I'm actually happier using Bound here. But I'm not sure why this has changed. I see nothing in the release notes suggesting this change. So I thought I might as well report it.

@beccadax
Copy link
Contributor

@swift-ci create

@swift-ci
Copy link
Collaborator

Comment by Kyle Macomber (JIRA)

I can't speak to why you're observing a behavior change between 5.1 and 5.2, but the typealias is only available when "Bound conforms to Strideable and Bound.Stride conforms to SignedInteger", so this works:

extension ClosedRange where Bound: Strideable, Bound.Stride: SignedInteger {
    /// Return the argument clamped to this range.
    /// Specifically, returns: `max(min(e, self.upperBound), self.lowerBound)`.
    func clamp(_ e: Element) -> Element {
        return Swift.max(Swift.min(e, upperBound), lowerBound)
    }
}

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants