Skip to content

[SR-14110] Allowing generics on @convention(c) functions #56496

Closed as not planned
Closed as not planned
@swift-ci

Description

@swift-ci
Previous ID SR-14110
Radar rdar://problem/73742487
Original Reporter dead_bug (JIRA User)
Type New Feature
Status Resolved
Resolution Won't Do
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels New Feature
Assignee None
Priority Medium

md5: 2adde8221da0f95bd9b4e43d48094398

Issue Description:

I have code that could use some threading to speed things up. None of existing primitives on osx seems sufficient for my task, except for POSIX threads, because this mechanism provides a fork-join model. To my great disappointment and surprise, I cannot pass anything polymorphic into a @convention(c) function. Could this be improved, please? I kinda need it asap, thanks.

This roughly how my original code looks like.

final class MyThing<Result> {
  var thread: pthread_t?
  var result: Result? = nil
     final class ThreadArgs {
      let target: Async<Result>
      let fn: () -> Result
      init(target:Async<Result>,fn:@escaping () -> Result) {
         self.target = target
         self.fn = fn
      }
   }
  init (_ block: @escaping () -> Result){
    let work: @convention(c) (UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? = { input in
      let args = input.load(as: ThreadArgs.self)
         args.target.result = args.fn ()
         return nil
    }
  }
  func await () -> Result {
    if result == nil {
      _ = pthread_join(thread!, nil)
    }
    return result!
}

The error is:

A C function pointer cannot be formed from a local closure that captures generic parameters

Metadata

Metadata

Assignees

No one assigned

    Labels

    @conventionFeature → attributes: the @convention attributeattributesFeature: Declaration and type attributesc interopFeature: Interoperability with CcompilerThe Swift compiler itself

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions