swift icon indicating copy to clipboard operation
swift copied to clipboard

[cxx-interop] Generic Swift `enum` with associated value fails to build in C++

Open mrousavy opened this issue 1 year ago • 2 comments

Description

While an enum with associated values works and can be represented in C++ perfectly, the same enum with one generic parameter introduced to it will somehow be exposed to C++, but fails to build due to seemingly different errors.

Without generic parameter

public enum ValueOrError {
  case value(Int)
  case error(String)
}

public func compute() -> ValueOrError {
  return .value(5)
}

This can be called from C++ just fine:

SwiftModule::ValueOrError value = SwiftModule::getValue();
swift::Int i = value.getValue();

With generic parameter

public enum ValueOrError<T> {
  case value(T)
  case error(String)
}

public func compute() -> ValueOrError<Int> {
  return .value(5)
}

Xcode shows me that the type exists, but the same code fails to build:

SwiftModule::ValueOrError<swift::Int> value = SwiftModule::getValue();
swift::Int i = value.getValue();

Screenshot 2024-07-17 at 20 30 41

...it fails to build with the error "Out-of-line definition of 'compute' does not match any declaration in ...":

Screenshot 2024-07-17 at 20 31 21

Reproduction

Swift

public enum ValueOrError<T> {
  case value(T)
  case error(String)
}

public func compute() -> ValueOrError<Int> {
  return .value(5)
}

C++

SwiftModule::ValueOrError<swift::Int> value = SwiftModule::getValue();
swift::Int i = value.getValue();

Expected behavior

I expect the generic enum to be exposed to C++ as a templated class.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4) Target: arm64-apple-macosx14.0

Additional information

No response

mrousavy avatar Jul 17 '24 18:07 mrousavy

Thanks for reporting this issue! I believe https://github.com/swiftlang/swift/pull/75372 might have fixed this, at least I could no longer reproduce the problem with the minimal code snippet attached to the report. Could you give it a try with a recent checkout of the main branch to verify if the problem is resolved for you? Alternatively, if you do not want to build the toolchain yourself, you can download a trunk development snapshot from here: https://www.swift.org/download/

Xazax-hun avatar Jul 29 '24 09:07 Xazax-hun

Wow, nice stuff! I'll try that when I'm back in the office - thanks ! :)

mrousavy avatar Jul 29 '24 09:07 mrousavy

I am closing the issue for now because I did not see any activity. Feel free to reopen if you still experience the issue.

Xazax-hun avatar Aug 05 '24 11:08 Xazax-hun

@Xazax-hun how do releases in Swift compiler work, will #75372 be released with the next Xcode update?

mrousavy avatar Aug 12 '24 13:08 mrousavy

Hey @mrousavy, unfortunately, I cannot share any details about future xcode releases or their schedules. :( As far as the open source Swift compiler is concerned, I would expect this fix to be part of the release after 6.0.

Xazax-hun avatar Aug 12 '24 14:08 Xazax-hun