Files
swift-mirror/test/Interop/Cxx/stdlib/Inputs/std-optional.h
Gabor Horvath 8e8e5c4aae [cxx-interop] Support rvalue-ref qualified methods
We already imported methods that have && as qualifiers but did not
support them properly. When in the C++ code there is overloading based
on the reference qualifiers the imported Swift functions are ambiguous.
This patch append "Consuming" to the && qualified methods' names to
avoid the ambiguity. It also makes sure these methods are actually
imported as consuming.

In case of operators, however, Swift does not have a way to distinguish
between the consuming and non-consuming self. This patch prevents
importing the consuming variants. This fixes some compiler crashes
during deserialization that we hit due to the ambiguities.

Fixes #83801

rdar://158675235
2026-01-21 12:09:05 +00:00

59 lines
2.1 KiB
C++

#ifndef TEST_INTEROP_CXX_STDLIB_INPUTS_STD_OPTIONAL_H
#define TEST_INTEROP_CXX_STDLIB_INPUTS_STD_OPTIONAL_H
#include <optional>
#include <string>
using StdOptionalInt = std::optional<int>;
using StdOptionalBool = std::optional<bool>;
using StdOptionalString = std::optional<std::string>;
using StdOptionalOptionalInt = std::optional<std::optional<int>>;
struct HasConstexprCtor {
int value;
constexpr HasConstexprCtor(int value) : value(value) {}
constexpr HasConstexprCtor(const HasConstexprCtor &other) = default;
constexpr HasConstexprCtor(HasConstexprCtor &&other) = default;
};
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) {}
HasDeletedMoveCtor(const HasDeletedMoveCtor &other) : value(other.value) {}
HasDeletedMoveCtor(HasDeletedMoveCtor &&other) = delete;
};
using StdOptionalHasDeletedMoveCtor = std::optional<HasDeletedMoveCtor>;
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; }
struct DerivedFromOptional : std::optional<std::string> {
using std::optional<std::string>::optional;
};
struct ReturnsOptionalString {
ReturnsOptionalString() {}
std::optional<std::string> getStr() const { return "foo"; }
DerivedFromOptional getStrDerived() const { return "foo"; }
};
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_OPTIONAL_H