Skip to content
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
9 changes: 9 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3026,8 +3026,17 @@ namespace {
decl->getIdentifier() &&
(decl->getName() == "tzdb" || decl->getName() == "time_zone_link" ||
decl->getName() == "__compressed_pair" ||
decl->getName() == "__optional_copy_assign_base" || // libc++
decl->getName() == "__optional_move_assign_base" || // libc++
decl->getName() == "time_zone"))
return nullptr;
// Bail if this is one of the base types of std::optional. Those types are
// mixins that are not designed to be used directly.
if (decl->getDeclContext()->isNamespace() && decl->isInStdNamespace() &&
decl->getIdentifier() &&
(decl->getName() == "_Optional_payload_base" ||
decl->getName() == "_Optional_payload"))
return nullptr;

auto &clangSema = Impl.getClangSema();
// Make Clang define any implicit constructors it may need (copy,
Expand Down
13 changes: 13 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/std-optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ struct HasConstexprCtor {
};
using StdOptionalHasConstexprCtor = std::optional<HasConstexprCtor>;

struct HasDeletedCopyCtor {
int value;
HasDeletedCopyCtor(int value) : value(value) {}
HasDeletedCopyCtor(const HasDeletedCopyCtor &other) = delete;
HasDeletedCopyCtor(HasDeletedCopyCtor &&other) = default;
};
using StdOptionalHasDeletedCopyCtor = std::optional<HasDeletedCopyCtor>;

struct HasDeletedMoveCtor {
int value;
HasDeletedMoveCtor(int value) : value(value) {}
Expand All @@ -29,7 +37,12 @@ inline StdOptionalInt getNonNilOptional() { return {123}; }

inline StdOptionalInt getNilOptional() { return {std::nullopt}; }

inline StdOptionalHasDeletedCopyCtor getNonNilOptionalHasDeletedCopyCtor() {
return StdOptionalHasDeletedCopyCtor(HasDeletedCopyCtor(654));
}

inline bool takesOptionalInt(std::optional<int> arg) { return (bool)arg; }
inline bool takesOptionalString(std::optional<std::string> arg) { return (bool)arg; }
inline bool takesOptionalHasDeletedCopyCtor(std::optional<HasDeletedCopyCtor> arg) { return (bool)arg; }

#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_OPTIONAL_H
3 changes: 3 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ StdOptionalTestSuite.test("pointee") {
modifiedOpt.pointee = 777
expectEqual(777, modifiedOpt.pointee)
#endif

let nonNilOptNonCopyable = getNonNilOptionalHasDeletedCopyCtor()
expectEqual(654, nonNilOptNonCopyable.pointee.value)
}

StdOptionalTestSuite.test("std::optional => Swift.Optional") {
Expand Down