Skip to content

Commit 3b47e15

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents ab2facf + d85b286 commit 3b47e15

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

stdlib/public/Cxx/CxxOptional.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
public protocol CxxOptional<Wrapped> {
13+
public protocol CxxOptional<Wrapped>: ExpressibleByNilLiteral {
1414
associatedtype Wrapped
1515

16+
init()
17+
1618
func __convertToBool() -> Bool
1719

1820
var pointee: Wrapped { get }
1921
}
2022

2123
extension CxxOptional {
24+
public init(nilLiteral: ()) {
25+
self.init()
26+
}
27+
2228
@inlinable
2329
public var hasValue: Bool {
2430
get {

test/Interop/Cxx/stdlib/Inputs/std-optional.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
#define TEST_INTEROP_CXX_STDLIB_INPUTS_STD_OPTIONAL_H
33

44
#include <optional>
5+
#include <string>
56

67
using StdOptionalInt = std::optional<int>;
8+
using StdOptionalString = std::optional<std::string>;
79

810
inline StdOptionalInt getNonNilOptional() { return {123}; }
911

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

14+
inline bool takesOptionalInt(std::optional<int> arg) { return (bool)arg; }
15+
inline bool takesOptionalString(std::optional<std::string> arg) { return (bool)arg; }
16+
1217
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_OPTIONAL_H

test/Interop/Cxx/stdlib/use-std-optional.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ StdOptionalTestSuite.test("std::optional hasValue/value") {
4141
expectNil(nilOpt.value)
4242
}
4343

44+
StdOptionalTestSuite.test("std::optional as ExpressibleByNilLiteral") {
45+
let res1 = takesOptionalInt(nil)
46+
expectFalse(res1)
47+
48+
let res2 = takesOptionalString(nil)
49+
expectFalse(res2)
50+
}
51+
4452
runAllTests()

0 commit comments

Comments
 (0)