Skip to content

[cxx-interop] Synthesize conformances to CxxRandomAccessCollection #61909

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
Nov 4, 2022
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
1 change: 1 addition & 0 deletions include/swift/AST/KnownProtocols.def
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ PROTOCOL(DistributedTargetInvocationDecoder)
PROTOCOL(DistributedTargetInvocationResultHandler)

// C++ Standard Library Overlay:
PROTOCOL(CxxRandomAccessCollection)
PROTOCOL(CxxSequence)
PROTOCOL(UnsafeCxxInputIterator)
PROTOCOL(UnsafeCxxRandomAccessIterator)
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/KnownStdlibTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ KNOWN_STDLIB_TYPE_DECL(ArraySlice, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(_ContiguousArrayStorage, ClassDecl, 1)
KNOWN_STDLIB_TYPE_DECL(Set, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(Sequence, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(Slice, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(Range, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(Dictionary, NominalTypeDecl, 2)
KNOWN_STDLIB_TYPE_DECL(CollectionDifference, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(AnyHashable, NominalTypeDecl, 0)
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
case KnownProtocolKind::DistributedTargetInvocationResultHandler:
M = getLoadedModule(Id_Distributed);
break;
case KnownProtocolKind::CxxRandomAccessCollection:
case KnownProtocolKind::CxxSequence:
case KnownProtocolKind::UnsafeCxxInputIterator:
case KnownProtocolKind::UnsafeCxxRandomAccessIterator:
Expand Down
46 changes: 46 additions & 0 deletions lib/ClangImporter/ClangDerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,50 @@ void swift::conformToCxxSequenceIfNeeded(
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("RawIterator"),
rawIteratorTy);
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxSequence});

// Try to conform to CxxRandomAccessCollection if possible.

auto cxxRAIteratorProto =
ctx.getProtocol(KnownProtocolKind::UnsafeCxxRandomAccessIterator);
if (!cxxRAIteratorProto ||
!ctx.getProtocol(KnownProtocolKind::CxxRandomAccessCollection))
return;

// Check if `begin()` and `end()` are non-mutating.
if (begin->isMutating() || end->isMutating())
return;

// Check if RawIterator conforms to UnsafeCxxRandomAccessIterator.
auto rawIteratorRAConformanceRef =
decl->getModuleContext()->lookupConformance(rawIteratorTy,
cxxRAIteratorProto);
if (!isConcreteAndValid(rawIteratorRAConformanceRef, module))
return;

// CxxRandomAccessCollection always uses Int as an Index.
auto indexTy = ctx.getIntType();

auto sliceTy = ctx.getSliceType();
sliceTy = sliceTy.subst(
[&](SubstitutableType *dependentType) {
if (dependentType->isEqual(cxxSequenceSelfTy))
return declSelfTy;
return Type(dependentType);
},
LookUpConformanceInModule(module));

auto indicesTy = ctx.getRangeType();
indicesTy = indicesTy.subst(
[&](SubstitutableType *dependentType) {
if (dependentType->isEqual(cxxSequenceSelfTy))
return indexTy;
return Type(dependentType);
},
LookUpConformanceInModule(module));

impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Index"), indexTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Indices"), indicesTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("SubSequence"), sliceTy);
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::CxxRandomAccessCollection});
}
1 change: 1 addition & 0 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5884,6 +5884,7 @@ SpecialProtocol irgen::getSpecialProtocolID(ProtocolDecl *P) {
case KnownProtocolKind::DistributedTargetInvocationEncoder:
case KnownProtocolKind::DistributedTargetInvocationDecoder:
case KnownProtocolKind::DistributedTargetInvocationResultHandler:
case KnownProtocolKind::CxxRandomAccessCollection:
case KnownProtocolKind::CxxSequence:
case KnownProtocolKind::UnsafeCxxInputIterator:
case KnownProtocolKind::UnsafeCxxRandomAccessIterator:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=CustomSequence -source-filename=x -I %S/Inputs -enable-experimental-cxx-interop -module-cache-path %t | %FileCheck %s

// CHECK: struct SimpleArrayWrapper : CxxRandomAccessCollection, CxxSequence {
// CHECK: typealias Element = UnsafePointer<Int32>.Pointee
// CHECK: typealias Iterator = CxxIterator<SimpleArrayWrapper>
// CHECK: typealias RawIterator = UnsafePointer<Int32>
// CHECK: }

// CHECK: struct SimpleCollectionNoSubscript : CxxRandomAccessCollection, CxxSequence {
// CHECK: typealias Element = ConstRACIterator.Pointee
// CHECK: typealias Iterator = CxxIterator<SimpleCollectionNoSubscript>
// CHECK: typealias RawIterator = SimpleCollectionNoSubscript.iterator
// CHECK: }

// CHECK: struct SimpleCollectionReadOnly : CxxRandomAccessCollection, CxxSequence {
// CHECK: typealias Element = ConstRACIteratorRefPlusEq.Pointee
// CHECK: typealias Iterator = CxxIterator<SimpleCollectionReadOnly>
// CHECK: typealias RawIterator = SimpleCollectionReadOnly.iterator
// CHECK: }
15 changes: 0 additions & 15 deletions test/Interop/Cxx/stdlib/overlay/custom-collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,18 @@ import Cxx

var CxxCollectionTestSuite = TestSuite("CxxCollection")

// === SimpleCollectionNoSubscript ===

extension SimpleCollectionNoSubscript : CxxRandomAccessCollection {
}

CxxCollectionTestSuite.test("SimpleCollectionNoSubscript as Swift.Collection") {
let c = SimpleCollectionNoSubscript()
expectEqual(c.first, 1)
expectEqual(c.last, 5)
}

// === SimpleCollectionReadOnly ===

extension SimpleCollectionReadOnly : CxxRandomAccessCollection {
}

CxxCollectionTestSuite.test("SimpleCollectionReadOnly as Swift.Collection") {
let c = SimpleCollectionReadOnly()
expectEqual(c.first, 1)
expectEqual(c.last, 5)
}

// === SimpleArrayWrapper ===

extension SimpleArrayWrapper : CxxRandomAccessCollection {
}

CxxCollectionTestSuite.test("SimpleArrayWrapper as Swift.Collection") {
let c = SimpleArrayWrapper()
expectEqual(c.first, 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
// CHECK: typealias RawIterator = ConstIteratorOutOfLineEq
// CHECK: }

// CHECK: struct SimpleArrayWrapper : CxxSequence {
// CHECK: typealias Element = UnsafePointer<Int32>.Pointee
// CHECK: typealias Iterator = CxxIterator<SimpleArrayWrapper>
// CHECK: typealias RawIterator = UnsafePointer<Int32>
// CHECK: }

// CHECK: struct SimpleArrayWrapperNullableIterators : CxxSequence {
// CHECK: typealias Element = Optional<UnsafePointer<Int32>>.Pointee
// CHECK: typealias Iterator = CxxIterator<SimpleArrayWrapperNullableIterators>
Expand Down