Skip to content

Compilation error for generic class with async function: Missing 'selfTypePointer$' #892

Description

@vgritsenko

Commit hash: 0dd772b

Context:
While testing swiftly's support of existing Swift code, noticed that a code using generic classes with async functions is not compiling. Source code without generics, or without async functions, can be compiled; code that uses both fails to compile with "cannot find 'selfTypePointer$' in scope" error. Looks like it's trying to pass non-existent selfTypePointer$ into the Task, but it is left unused after that.

import Foundation

public final class Tester<T: Comparable>: Sendable {
    public func fetch() async { }
}

Results in the following compilation error:

.../.build/plugins/outputs/lib/SwiftTestLib/destination/JExtractSwiftPlugin/Sources/Sample+SwiftJava.swift:35:56: error: cannot find 'selfTypePointer$' in scope
 33 |     nonisolated(unsafe) let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
 34 |     nonisolated(unsafe) let selfPointerSendable$ = selfPointer$
 35 |     nonisolated(unsafe) let selfTypePointerSendable$ = selfTypePointer$
    |                                                        `- error: cannot find 'selfTypePointer$' in scope
 36 |     var task: Task<Void, Never>? = nil
 37 |     #if swift(>=6.2)

Portion of the generated swift file:

extension Tester: _SwiftTestLib_Tester_opener {
  static func _fetch(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong, result_future: jobject?) {
    assert(selfPointer != 0, "selfPointer memory address was null")
    let selfPointerBits$ = Int(Int64(fromJNI: selfPointer, in: environment))
    let selfPointer$ = UnsafeMutablePointer<Tester<T>>(bitPattern: selfPointerBits$)
    guard let selfPointer$ else {
      fatalError("selfPointer memory address was null in call to \(#function)!")
    }
    nonisolated(unsafe) let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
    nonisolated(unsafe) let selfPointerSendable$ = selfPointer$
    nonisolated(unsafe) let selfTypePointerSendable$ = selfTypePointer$
    var task: Task<Void, Never>? = nil
    #if swift(>=6.2)
      if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) {
        task = Task.immediate {
          var environment = try! JavaVirtualMachine.shared().environment()
          let selfPointer$ = selfPointerSendable$
          let selfTypePointer$ = selfTypePointerSendable$
          defer {
            let deferEnvironment = try! JavaVirtualMachine.shared().environment()
            deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
          } // printTaskBody(printer:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:2017
          await selfPointer$.pointee.fetch()
          environment = try! JavaVirtualMachine.shared().environment()
          _ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
        } // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:2046
      } // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:2045
    #endif // end of swift(>=6.2) // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:2044
    if task == nil {
      task = Task {
        var environment = try! JavaVirtualMachine.shared().environment()
        let selfPointer$ = selfPointerSendable$
        let selfTypePointer$ = selfTypePointerSendable$
        defer {
          let deferEnvironment = try! JavaVirtualMachine.shared().environment()
          deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
        } // printTaskBody(printer:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:2017
        await selfPointer$.pointee.fetch()
        environment = try! JavaVirtualMachine.shared().environment()
        _ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
      } // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:2055
    } // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:2054
    
  } // printFunctionDecl(_:decl:skipMethodBody:) @ JExtractSwiftLib/JNISwift2JavaGenerator+SwiftThunkPrinting.swift:1159
} // printOpenerProtocol(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+SwiftThunkPrinting.swift:1177

Environment: Swift 6.3.3 on macOS.

$ swiftly run swift --version
Apple Swift version 6.3.3 (swift-6.3.3-RELEASE)
Target: arm64-apple-macosx26.0

$ uname -a
Darwin it-C01241 25.6.0 Darwin Kernel Version 25.6.0: Sat Jul 11 15:26:21 PDT 2026; root:xnu-12377.161.13~4/RELEASE_ARM64_T6000 arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions