Skip to content

Commit f37f42f

Browse files
committed
[cxx-interop] Conform std::set to ExpressibleByArrayLiteral
Swift's own `Set` conforms to `ExpressibleByArrayLiteral`. This change conforms instantiations of C++ `std::set` to `ExpressibleByArrayLiteral` as well. This makes it possible to pass an array literal as an argument to a function that takes a `std::set` as parameter. rdar://137126325
1 parent fe796b4 commit f37f42f

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl,
883883

884884
impl.addSynthesizedTypealias(decl, ctx.Id_Element,
885885
valueType->getUnderlyingType());
886+
impl.addSynthesizedTypealias(decl, ctx.Id_ArrayLiteralElement,
887+
valueType->getUnderlyingType());
886888
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Size"),
887889
sizeType->getUnderlyingType());
888890
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("InsertionResult"),

stdlib/public/Cxx/CxxSet.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// `std::multiset` conform to this protocol.
1717
///
1818
/// - SeeAlso: `CxxUniqueSet`
19-
public protocol CxxSet<Element> {
19+
public protocol CxxSet<Element>: ExpressibleByArrayLiteral {
2020
associatedtype Element
2121
associatedtype Size: BinaryInteger
2222

@@ -47,6 +47,11 @@ extension CxxSet {
4747
}
4848
}
4949

50+
@inlinable
51+
public init(arrayLiteral elements: Element...) {
52+
self.init(elements)
53+
}
54+
5055
/// Returns a Boolean value that indicates whether the given element exists
5156
/// in the set.
5257
@inlinable

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,51 @@ StdSetTestSuite.test("UnorderedSetOfCInt.init()") {
7575
expectTrue(s.contains(3))
7676
}
7777

78+
StdSetTestSuite.test("SetOfCInt as ExpressibleByArrayLiteral") {
79+
let s: SetOfCInt = [1, 3, 5]
80+
expectTrue(s.contains(1))
81+
expectFalse(s.contains(2))
82+
expectTrue(s.contains(3))
83+
84+
func takesSetOfCInt(_ s: SetOfCInt) {
85+
expectTrue(s.contains(1))
86+
expectTrue(s.contains(2))
87+
expectFalse(s.contains(3))
88+
}
89+
90+
takesSetOfCInt([1, 2])
91+
}
92+
93+
StdSetTestSuite.test("UnorderedSetOfCInt as ExpressibleByArrayLiteral") {
94+
let s: UnorderedSetOfCInt = [1, 3, 5]
95+
expectTrue(s.contains(1))
96+
expectFalse(s.contains(2))
97+
expectTrue(s.contains(3))
98+
99+
func takesUnorderedSetOfCInt(_ s: UnorderedSetOfCInt) {
100+
expectTrue(s.contains(1))
101+
expectTrue(s.contains(2))
102+
expectFalse(s.contains(3))
103+
}
104+
105+
takesUnorderedSetOfCInt([1, 2])
106+
}
107+
108+
StdSetTestSuite.test("MultisetOfCInt as ExpressibleByArrayLiteral") {
109+
let s: MultisetOfCInt = [1, 1, 3]
110+
expectTrue(s.contains(1))
111+
expectFalse(s.contains(2))
112+
expectTrue(s.contains(3))
113+
114+
func takesMultisetOfCInt(_ s: MultisetOfCInt) {
115+
expectTrue(s.contains(1))
116+
expectTrue(s.contains(2))
117+
expectFalse(s.contains(3))
118+
}
119+
120+
takesMultisetOfCInt([1, 1, 2])
121+
}
122+
78123
StdSetTestSuite.test("SetOfCInt.insert") {
79124
var s = SetOfCInt()
80125
expectFalse(s.contains(123))

0 commit comments

Comments
 (0)