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-1856] prefix() chained after sorted() and before map() confuses type inference in 3.0 #44465

Closed
swift-ci opened this issue Jun 21, 2016 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression standard library Area: Standard library umbrella swift 3.0 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1856
Radar rdar://problem/27033993
Original Reporter mluisbrown (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

macOS 10.11.5, Xcode 8 beta (8S128d) with Apple Swift version 3.0 (swiftlang-800.0.30 clang-800.0.24)

Additional Detail from JIRA
Votes 4
Component/s Compiler, Standard Library
Labels Bug, 3.0Regression, TypeChecker
Assignee @rudkx
Priority Medium

md5: 0cf780bcb513fd0c75d36886d36e650d

duplicates:

  • SR-851 Result of dropFirst differs between 2.1.1 and 2.2

relates to:

  • SR-4279 Eager prefix(while:) on sequence(...).lazy instance

Issue Description:

In Swift 3.0 the following code:

let datesWithCount: [(Date, Int)] = [(Date(), 1), (Date(), 2), (Date(), 3)]

let dates: [Date] = datesWithCount.sorted {
    $0.0 < $1.0
}.prefix(1).map {
    return $0.0
}

Causes the compiler to error with "Ambiguous use of 'prefix'", with the caret pointing to sorted.

The same code compiles fine in Swift 2.2

The only way to make it compile is either to force the result of prefix into an Array:

let dates: [Date] = Array(datesWithCount.sorted {
    $0.0 < $1.0
}.prefix(1)).map {
    return $0.0
}

..or to split the map onto a separate line:

let sortedDatesWithCount = datesWithCount.sorted {
    $0.0 < $1.0
}.prefix(1)

let mappedDates = sortedDatesWithCount.map {
    return $0.0
}
@swift-ci
Copy link
Collaborator Author

swift-ci commented Sep 9, 2016

Comment by Blixt (JIRA)

I just encountered this same issue with the Swift compiler that comes with Xcode 8 GM seed. Here's a simpler repro case:

[1, 2, 3, 4, 5].prefix(3).map { $0 * $0 } // error: ambiguous use of 'prefix'

This works:

let items = [1, 2, 3, 4, 5].prefix(3) // [1, 2, 3]
items.map { $0 * $0 } // [1, 4, 9]

And so does this:

let items = Array([1, 2, 3, 4, 5].prefix(3)).map { $0 * $0 }

PS. I noticed there is a claim that this duplicates SR-851, but this bug appeared first for us when we upgraded code to Swift 3.0, and did not occur with Swift 2.2.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Sep 9, 2016

Comment by Michael Brown (JIRA)

blixt (JIRA User) you can also work around your example as follows:

let items: [Int64] = [1, 2, 3, 4, 5]
Array(items.prefix(3)).map { $0 * $0 } 

ie, as I mentioned above you can avoid splitting across multiple lines by wrapping the result of prefix into an Array

EDIT: I see you beat me to it 🙂

@xedin
Copy link
Member

xedin commented Nov 17, 2017

This is resolved by #12520 going to prefer SubSequence over AnySequence. mluisbrown (JIRA User) please verify and resolve.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 regression standard library Area: Standard library umbrella swift 3.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants