Skip to content

Commit 68f9e84

Browse files
authored
Merge pull request #63492 from apple/egorzhdan/cxx-sequence-immutable
[cxx-interop] Require `begin()` and `end()` to be non-mutating for auto-conformed container types
2 parents c5ff89a + bf341ac commit 68f9e84

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void swift::conformToCxxSequenceIfNeeded(
396396
if (!cxxIteratorProto || !cxxSequenceProto)
397397
return;
398398

399-
// Check if present: `mutating func __beginUnsafe() -> RawIterator`
399+
// Check if present: `func __beginUnsafe() -> RawIterator`
400400
auto beginId = ctx.getIdentifier("__beginUnsafe");
401401
auto begins = lookupDirectWithoutExtensions(decl, beginId);
402402
if (begins.size() != 1)
@@ -406,7 +406,7 @@ void swift::conformToCxxSequenceIfNeeded(
406406
return;
407407
auto rawIteratorTy = begin->getResultInterfaceType();
408408

409-
// Check if present: `mutating func __endUnsafe() -> RawIterator`
409+
// Check if present: `func __endUnsafe() -> RawIterator`
410410
auto endId = ctx.getIdentifier("__endUnsafe");
411411
auto ends = lookupDirectWithoutExtensions(decl, endId);
412412
if (ends.size() != 1)
@@ -415,6 +415,10 @@ void swift::conformToCxxSequenceIfNeeded(
415415
if (!end)
416416
return;
417417

418+
// Check if `begin()` and `end()` are non-mutating.
419+
if (begin->isMutating() || end->isMutating())
420+
return;
421+
418422
// Check if `__beginUnsafe` and `__endUnsafe` have the same return type.
419423
auto endTy = end->getResultInterfaceType();
420424
if (!endTy || endTy->getCanonicalType() != rawIteratorTy->getCanonicalType())
@@ -468,10 +472,6 @@ void swift::conformToCxxSequenceIfNeeded(
468472
!ctx.getProtocol(KnownProtocolKind::CxxRandomAccessCollection))
469473
return false;
470474

471-
// Check if `begin()` and `end()` are non-mutating.
472-
if (begin->isMutating() || end->isMutating())
473-
return false;
474-
475475
// Check if RawIterator conforms to UnsafeCxxRandomAccessIterator.
476476
auto rawIteratorRAConformanceRef =
477477
decl->getModuleContext()->lookupConformance(rawIteratorTy,

test/Interop/Cxx/stdlib/overlay/custom-sequence-module-interface.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
// CHECK: typealias RawIterator = UnsafePointer<Int32>?
2525
// CHECK: }
2626

27-
// CHECK: struct HasMutatingBeginEnd : CxxConvertibleToCollection {
28-
// CHECK: typealias Element = ConstIterator.Pointee
29-
// CHECK: typealias Iterator = CxxIterator<HasMutatingBeginEnd>
30-
// CHECK: typealias RawIterator = ConstIterator
27+
// CHECK: struct HasMutatingBeginEnd {
28+
// CHECK-NOT: typealias Element = ConstIterator.Pointee
29+
// CHECK-NOT: typealias Iterator = CxxIterator<HasMutatingBeginEnd>
30+
// CHECK-NOT: typealias RawIterator = ConstIterator
3131
// CHECK: }
3232

3333
// CHECK: struct HasNoBeginMethod {

0 commit comments

Comments
 (0)