diff --git a/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/GenericType.swift b/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/GenericType.swift index 9ae54875a..a37904672 100644 --- a/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/GenericType.swift +++ b/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/GenericType.swift @@ -88,6 +88,8 @@ public func makeIntGenericEnum() -> GenericEnum { if Bool.random() { return .foo } else { return .bar } } +public typealias IntGenericEnum = GenericEnum // This specialization is not supported yet. + public enum GenericEnumWithValue { case some(T) case none diff --git a/Sources/SwiftExtract/ExtractedDecls.swift b/Sources/SwiftExtract/ExtractedDecls.swift index cdfe8185a..f4686e74e 100644 --- a/Sources/SwiftExtract/ExtractedDecls.swift +++ b/Sources/SwiftExtract/ExtractedDecls.swift @@ -191,6 +191,13 @@ public final class ExtractedNominalType: ExtractedSwiftDecl { message: "Missing type arguments for: \(missingParams) when specializing \(baseTypeName) as \(specializedName)" ) } + + if swiftNominal.kind == .enum { + throw SpecializationError( + message: "Specialization for enums are not yet supported: '\(baseTypeName)' as '\(specializedName)'" + ) + } + return ExtractedNominalType( base: self, specializedTypeName: specializedName, diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md index 75dbb06c0..bbb6e1b0b 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md @@ -465,6 +465,10 @@ public final class FishBox ... { > NOTE: Currently no helpers are available to convert between unspecialized types to specialized ones, but this can be offered > as additional `box.as(FishBox.class)` conversion methods in the future. + +> NOTE: Currently specialization for generic enums are not yet supported. + + ### Evaluating `#if` In jextract, `#if` branches are evaluated using [SwiftIfConfig](https://github.com/swiftlang/swift-syntax/blob/main/Sources/SwiftIfConfig/SwiftIfConfig.docc/SwiftIfConfig.md). diff --git a/Tests/JExtractSwiftTests/SpecializationTests.swift b/Tests/JExtractSwiftTests/SpecializationTests.swift index bfdff6d02..41f99374b 100644 --- a/Tests/JExtractSwiftTests/SpecializationTests.swift +++ b/Tests/JExtractSwiftTests/SpecializationTests.swift @@ -364,15 +364,38 @@ struct SpecializationTests { let fish = try #require(translator.extractedTypes["Fish"]) #expect(!fish.swiftNominal.isGeneric) - #expect(throws: SpecializationError.self) { - _ = try fish.specialize(as: "FancyFish", with: ["T": "Int"]) - } - do { _ = try fish.specialize(as: "FancyFish", with: ["T": "Int"]) + Issue.record("Error not thrown") } catch let error as SpecializationError { #expect(error.message.contains("Unable to specialize non-generic type")) #expect(error.message.contains("Fish")) } } + + @Test("Specializing a enum type throws an error") + func specializeEnumTypeThrows() throws { + var config = Configuration() + config.swiftModule = "SwiftModule" + let translator = makeSwiftJavaAnalyzer(config: config) + try translator.analyze( + path: "/fake/Fake.swiftinterface", + text: """ + public enum Foo { + case foo + } + """, + ) + + let foo = try #require(translator.extractedTypes["Foo"]) + #expect(foo.swiftNominal.kind == .enum) + + do { + _ = try foo.specialize(as: "IntFoo", with: ["T": "Int"]) + Issue.record("Error not thrown") + } catch let error as SpecializationError { + #expect(error.message.contains("Specialization for enums are not yet supported")) + #expect(error.message.contains("Foo")) + } + } }