Skip to content

[cxx-interop] Conform std::set to ExpressibleByArrayLiteral #76830

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
Oct 3, 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
2 changes: 2 additions & 0 deletions lib/ClangImporter/ClangDerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,8 @@ void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl,

impl.addSynthesizedTypealias(decl, ctx.Id_Element,
valueType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.Id_ArrayLiteralElement,
valueType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Size"),
sizeType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("InsertionResult"),
Expand Down
7 changes: 6 additions & 1 deletion stdlib/public/Cxx/CxxSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// `std::multiset` conform to this protocol.
///
/// - SeeAlso: `CxxUniqueSet`
public protocol CxxSet<Element> {
public protocol CxxSet<Element>: ExpressibleByArrayLiteral {
associatedtype Element
associatedtype Size: BinaryInteger

Expand Down Expand Up @@ -47,6 +47,11 @@ extension CxxSet {
}
}

@inlinable
public init(arrayLiteral elements: Element...) {
self.init(elements)
}

/// Returns a Boolean value that indicates whether the given element exists
/// in the set.
@inlinable
Expand Down
45 changes: 45 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-set.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,51 @@ StdSetTestSuite.test("UnorderedSetOfCInt.init()") {
expectTrue(s.contains(3))
}

StdSetTestSuite.test("SetOfCInt as ExpressibleByArrayLiteral") {
let s: SetOfCInt = [1, 3, 5]
expectTrue(s.contains(1))
expectFalse(s.contains(2))
expectTrue(s.contains(3))

func takesSetOfCInt(_ s: SetOfCInt) {
expectTrue(s.contains(1))
expectTrue(s.contains(2))
expectFalse(s.contains(3))
}

takesSetOfCInt([1, 2])
}

StdSetTestSuite.test("UnorderedSetOfCInt as ExpressibleByArrayLiteral") {
let s: UnorderedSetOfCInt = [1, 3, 5]
expectTrue(s.contains(1))
expectFalse(s.contains(2))
expectTrue(s.contains(3))

func takesUnorderedSetOfCInt(_ s: UnorderedSetOfCInt) {
expectTrue(s.contains(1))
expectTrue(s.contains(2))
expectFalse(s.contains(3))
}

takesUnorderedSetOfCInt([1, 2])
}

StdSetTestSuite.test("MultisetOfCInt as ExpressibleByArrayLiteral") {
let s: MultisetOfCInt = [1, 1, 3]
expectTrue(s.contains(1))
expectFalse(s.contains(2))
expectTrue(s.contains(3))

func takesMultisetOfCInt(_ s: MultisetOfCInt) {
expectTrue(s.contains(1))
expectTrue(s.contains(2))
expectFalse(s.contains(3))
}

takesMultisetOfCInt([1, 1, 2])
}

StdSetTestSuite.test("SetOfCInt.insert") {
var s = SetOfCInt()
expectFalse(s.contains(123))
Expand Down