Skip to content

jextract/jni: Fix compilation of nested generic types#738

Merged
ktoso merged 2 commits into
swiftlang:mainfrom
sidepelican:fix_nested_generic
May 12, 2026
Merged

jextract/jni: Fix compilation of nested generic types#738
ktoso merged 2 commits into
swiftlang:mainfrom
sidepelican:fix_nested_generic

Conversation

@sidepelican
Copy link
Copy Markdown
Contributor

Currently, nested generic types cause compilation errors because the generator fails to include necessary namespaces and generic parameters.

// MARK: original declaration
public enum NamespaceEnum {
  public struct GenericType<T> {
    public var value: Int
  }
}

// MARK: generated code

//        ↓ missing namespace
extension GenericType: _MySwiftLibrary_GenericType_opener {
  static func _get_value(...) -> jlong {
    ...
    let selfPointer$ = UnsafeMutablePointer<NamespaceEnum.GenericType>(bitPattern: selfPointerBits$)
    //                                    missing type parameter <T> ↑
    ...
  }

Based on my experiments, Swift handles type inference differently for nested generics compared to top-level generics.
While top-level generic parameters can often be inferred within an extension, nested generic types require explicit type parameters when referenced inside the extension body.

struct TopLevelGeneric<T> {}

extension TopLevelGeneric {
    static func foo() -> String {
        "\(TopLevelGeneric.self)" // OK
    }
}

enum Nested {
    struct Generic<T> {}
}

extension Nested.Generic {
    static func foo() -> String {
        "\(Nested.Generic.self)" // error: generic parameter 'T' could not be inferred
    }
}

To resolve this, I have updated the generator to:

  1. Use the qualified name for extension declarations.
  2. Use the full type name including generic parameters within function bodies.
    The root cause was that SwiftType in SwiftFunctionSignature was missing its generic parameters.
    It appears that ImportedNominalType.swiftType was incorrectly stripping these parameters during the type conversion process.
    This PR fixes that logic to ensure generic information is preserved.

@sidepelican sidepelican requested a review from ktoso as a code owner May 12, 2026 06:41
Comment thread Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/NestedTypes.swift Outdated
…stedTypes.swift

Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
@ktoso ktoso merged commit 70ab891 into swiftlang:main May 12, 2026
64 checks passed
@sidepelican sidepelican deleted the fix_nested_generic branch May 12, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants