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-5251] Generic Sequence Function Compilation Fails #47826

Closed
stephencelis opened this issue Jun 18, 2017 · 11 comments
Closed

[SR-5251] Generic Sequence Function Compilation Fails #47826

stephencelis opened this issue Jun 18, 2017 · 11 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@stephencelis
Copy link
Contributor

Previous ID SR-5251
Radar rdar://problem/33142121
Original Reporter @stephencelis
Type Bug
Status Closed
Resolution Duplicate
Environment

Swift 4 (Xcode 9 beta)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @rudkx
Priority Medium

md5: 06429d9b5bd72cef8ad6037d87c94e0f

blocks:

  • SR-5422 Regression: Call to Optional flatMap/map segfault
  • SR-5586 'rethrows' function used as value requires 'try'

Issue Description:

The following code should compile, but doesn't:

infix operator <*>
public func <*> <S: Sequence, T: Sequence, B> (fs: S, xs: T) -> [B] where S.Element == ((T.Element) -> B) {
    return fs.flatMap { f in xs.map(f) }
}

The error message:

error: 'flatMap' produces '[SegmentOfResult.Element]', not the expected contextual result type '[B]'

@belkadan
Copy link
Contributor

cc @rudkx, @xedin

@rudkx
Copy link
Member

rudkx commented Jul 5, 2017

@swift-ci create

@stephencelis
Copy link
Contributor Author

FWIW it appears to be the point-free style causing issues. This works:

-return fs.flatMap { f in xs.map(f) }
+return fs.flatMap { f in xs.map { x in f(x) } }

Also broken:

return fs.flatMap(xs.map)

See also:

https://bugs.swift.org/browse/SR-5422

@stephencelis
Copy link
Contributor Author

Another thing that fails, but shouldn't:

public extension Sequence {
  public func apply<S: Sequence, A>(_ fs: S) -> [A] where S.Element == ((Element) -> A) {
    return fs.flatMap { f in self.map { x in f(x) } }
  }

  public static func <*> <S: Sequence, A>(fs: S, xs: Self) -> [A] where S.Element == ((Element) -> A) {
    return xs.apply(fs)
    // Generic parameter 'A' could not be inferred
  }
}

public func apply<S: Sequence, T: Sequence, A>(_ fs: S) -> (T) -> [A] where S.Element == ((T.Element) -> A) {
  return { xs in
    fs <*> xs
    // Type of expression is ambiguous without more context
  }
}

@xedin
Copy link
Member

xedin commented Jul 18, 2017

Portion of SE-0110 has been rolled back via #10414 so the code from description compiles just fine now.

@xedin
Copy link
Member

xedin commented Jul 18, 2017

@stephencelis Please verify and move to closed.

@stephencelis
Copy link
Contributor Author

All of these issues (the main one and those listed in my follow-up comment) still persist in Xcode 9 beta 4.

@xedin
Copy link
Member

xedin commented Jul 25, 2017

@stephencelis Interesting, the code in the description and in the follow-up compiles just fine with master branch, maybe some of the changes related to this didn't make it to Beta 4, I'll double check and let you know!

@rudkx
Copy link
Member

rudkx commented Jul 25, 2017

@stephencelis I found that your original example is fixed in Xcode 9 beta 3 and Xcode 9 beta 4.

The follow-on examples work with the nightly from 'master' but not with the nightly from 'swift-4.0-branch.'

@stephencelis
Copy link
Contributor Author

@rudkx Checked on the latest snapshot and everything is fixed in the latest snapshot except for:

public extension Sequence {
  public func apply<S: Sequence, A>(_ fs: S) -> [A] where S.Element == ((Element) -> A) {
    return fs.flatMap(self.map)
  }
}

Call can throw, but it is not marked with 'try' and the error is not handled

Related issue: https://bugs.swift.org/browse/SR-5586

Thanks!

@rudkx
Copy link
Member

rudkx commented Jan 19, 2018

This one remaining issue is just a dup of the other issue, so I'm going to close this out.

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants