Skip to content

[cxx-interop] Treat std::string as an owned type after libc++ change #71071

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7217,6 +7217,12 @@ static bool hasSwiftAttribute(const clang::Decl *decl, StringRef attr) {
}

static bool hasOwnedValueAttr(const clang::RecordDecl *decl) {
// HACK: this logic belongs in std.apinotes, but it got broken by a libc++
// modulemap change
if (decl->getNameAsString() == "basic_string" ||
decl->getNameAsString() == "vector")
return true;

return hasSwiftAttribute(decl, "import_owned");
}

Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ module StdSet {
export *
}

module StdString {
header "std-string.h"
requires cplusplus
export *
}

module StdPair {
header "std-pair.h"
requires cplusplus
Expand Down
9 changes: 9 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/std-string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <string>

struct HasMethodThatReturnsString {
int value = 111;
std::string getString() const { return std::to_string(value); }
};

inline std::string takesStringWithDefaultArg(std::string s = "abc") { return s; }
inline std::string takesStringConstRefWithDefaultArg(const std::string& s = "def") { return s; }
40 changes: 39 additions & 1 deletion test/Interop/Cxx/stdlib/use-std-string.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -D USE_CUSTOM_STRING_API)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift -D SUPPORTS_DEFAULT_ARGUMENTS -D USE_CUSTOM_STRING_API)
//
// REQUIRES: executable_test

import StdlibUnittest
import CxxStdlib
#if USE_CUSTOM_STRING_API
import StdString
#endif

var StdStringTestSuite = TestSuite("StdString")

Expand All @@ -22,4 +26,38 @@ StdStringTestSuite.test("push back") {
expectEqual(s[0], 42)
}

#if USE_CUSTOM_STRING_API
StdStringTestSuite.test("get from a method") {
let box = HasMethodThatReturnsString()
let str = box.getString()
expectEqual(str.size(), 3)
expectEqual(str, std.string("111"))
}

StdStringTestSuite.test("pass as an argument") {
let s = std.string("a")
let res = takesStringWithDefaultArg(s)
expectEqual(res.size(), 1)
expectEqual(res[0], 97)
}

#if SUPPORTS_DEFAULT_ARGUMENTS
StdStringTestSuite.test("pass as a default argument") {
let res = takesStringWithDefaultArg()
expectEqual(res.size(), 3)
expectEqual(res[0], 97)
expectEqual(res[1], 98)
expectEqual(res[2], 99)
}

StdStringTestSuite.test("pass as a const ref default argument") {
let res = takesStringConstRefWithDefaultArg()
expectEqual(res.size(), 3)
expectEqual(res[0], 100)
expectEqual(res[1], 101)
expectEqual(res[2], 102)
}
#endif
#endif

runAllTests()