Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interop: Cannot return swift::Optional<swift::String> from C++ function #76024

Open
ADKaster opened this issue Aug 21, 2024 · 1 comment
Open
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ clang importer Area → compiler: The clang importer

Comments

@ADKaster
Copy link
Contributor

Description

Is it intended that the bound types swift::Optional and swift::String cannot be used to return swift types to swift?

Attempting to do so results in the swift::Optional<T> for some T being replaced with Never

/Users/andrew/Source/test/swift-return-swift-type/exe/Helper.swift:5:11: error: 'getValue()' is unavailable: return type is unavailable in Swift
3 | func printThings()
4 | {
5 |     print(getValue())
  |           `- error: 'getValue()' is unavailable: return type is unavailable in Swift
6 |     print(getString())
7 | }

exe.getValue:2:13: note: 'getValue()' has been explicitly marked unavailable here
1 | @available(*, unavailable, message: "return type is unavailable in Swift")
2 | public func getValue() -> Never
  |             `- note: 'getValue()' has been explicitly marked unavailable here

/Users/andrew/Source/test/swift-return-swift-type/exe/Helper.swift:6:11: error: 'getString()' is unavailable: return type is unavailable in Swift
4 | {
5 |     print(getValue())
6 |     print(getString())
  |           `- error: 'getString()' is unavailable: return type is unavailable in Swift
7 | }
8 | 

exe.getString:2:13: note: 'getString()' has been explicitly marked unavailable here
1 | @available(*, unavailable, message: "return type is unavailable in Swift")
2 | public func getString() -> Never
  |             `- note: 'getString()' has been explicitly marked unavailable here

Reproduction

This test case requires a few files, which I have uploaded here:

https://github.com/ADKaster/swift-return-swift-type

The results can be reproduced with

cmake -B build -GNinja
cmake --build build
# or ninja -C build

In the example, a few things are happening:

  • We have a shared library, shlib, with one swift file "Color.swift".
  • The shlib exports a bridging header, "shlib-Swift.h"
  • We have an executable, exe, that has a helper file, Helper.h
  • The helper file exports two functions, getValue() and getString() that each return swift::Optional<T> for some T.
  • The exe has a swift file, Helper.swift, that tries to print the return values of getValue() and getString()

Helper.h

#pragma once

#include "Color.h"
#include "shlib-Swift.h"

swift::Optional<int> getValue() noexcept;
swift::Optional<swift::String> getString() noexcept;

Helper.cpp

#include "Helper.h"

swift::Optional<int> getValue() noexcept { return swift::Optional<int>::some(42); }
swift::Optional<swift::String> getString() noexcept { return swift::Optional<swift::String>::none(); }

Helper.swift

import exe

func printThings()
{
    print(getValue())
    print(getString())
}

Expected behavior

The program should compile successfully and print "42\nnone\n"

Environment

swift-driver version: 1.113 Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1)
Target: arm64-apple-macosx14.0

Additional information

No response

@ADKaster ADKaster added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Aug 21, 2024
@Xazax-hun Xazax-hun self-assigned this Sep 27, 2024
@Xazax-hun
Copy link
Contributor

I will take a look at this.

@Xazax-hun Xazax-hun added c++ interop Feature: Interoperability with C++ clang importer Area → compiler: The clang importer and removed triage needed This issue needs more specific labels labels Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ clang importer Area → compiler: The clang importer
Projects
None yet
Development

No branches or pull requests

2 participants