Skip to content

Commit 3bf02d1

Browse files
Xazax-hunGabor Horvath
authored andcommitted
🍒[6.0][cxx-interop] Do not attempt to export read accessors to C++
The reverse interop code assumed that there is always a get accessor for subscript which is not always the case. This PR prevents the crash by not exporting subscript when get accessor is not availableA. Scope: C++ reverse interop. Risk: Low, we just skip problematic operators. Testing: Regression test added. Issue: rdar://134425834 Reviewer: @egorzhdan Original PR: #76170
1 parent f8e35f0 commit 3bf02d1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,9 +2072,10 @@ class DeclAndTypePrinter::Implementation
20722072
if (outputLang == OutputLanguageMode::Cxx) {
20732073
if (!SD->isInstanceMember())
20742074
return;
2075-
auto *getter = SD->getOpaqueAccessor(AccessorKind::Get);
2076-
printAbstractFunctionAsMethod(getter, false,
2077-
/*isNSUIntegerSubscript=*/false, SD);
2075+
// TODO: support read accessors.
2076+
if (auto *getter = SD->getOpaqueAccessor(AccessorKind::Get))
2077+
printAbstractFunctionAsMethod(getter, false,
2078+
/*isNSUIntegerSubscript=*/false, SD);
20782079
return;
20792080
}
20802081
assert(SD->isInstanceMember() && "static subscripts not supported");

test/Interop/SwiftToCxx/functions/swift-operators.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616

1717
public struct IntBox { var x: CInt }
1818

19+
public struct CustomArray<Element> where Element : ~Copyable {
20+
private var buffer: UnsafeMutableBufferPointer<Element>
21+
22+
public subscript(index: Int) -> Element {
23+
_read {
24+
yield buffer[index]
25+
}
26+
nonmutating _modify {
27+
yield &buffer[index]
28+
}
29+
}
30+
}
31+
1932
public func -(lhs: IntBox, rhs: IntBox) -> CInt {
2033
return lhs.x - rhs.x
2134
}

0 commit comments

Comments
 (0)