Skip to content

[cxx-interop] Check for unsafe types of default arguments consistently #71181

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

Merged
merged 1 commit into from
Jan 29, 2024
Merged
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
3 changes: 2 additions & 1 deletion lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2540,7 +2540,8 @@ bool ClangImporter::Implementation::isDefaultArgSafeToImport(
// default expression, since we cannot guarantee the lifetime of the
// pointee value.
if (auto paramRecordDecl = param->getType()->getAsCXXRecordDecl()) {
if (isViewType(paramRecordDecl))
if (isViewType(paramRecordDecl) &&
!paramRecordDecl->hasUserDeclaredCopyConstructor())
return false;
}
// If the parameter is a const reference, check if the expression
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
8 changes: 8 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/std-string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#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; }
32 changes: 31 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 Down Expand Up @@ -392,4 +396,30 @@ StdStringTestSuite.test("std::string from C string") {
expectEqual(str, std.string("abc"))
}

#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)
}
#endif
#endif

runAllTests()