Skip to content

[cxx-interop] Do not attempt to export read accessors to C++ #76170

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
Aug 30, 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
7 changes: 4 additions & 3 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2069,9 +2069,10 @@ class DeclAndTypePrinter::Implementation
if (outputLang == OutputLanguageMode::Cxx) {
if (!SD->isInstanceMember())
return;
auto *getter = SD->getOpaqueAccessor(AccessorKind::Get);
printAbstractFunctionAsMethod(getter, false,
/*isNSUIntegerSubscript=*/false, SD);
// TODO: support read accessors.
if (auto *getter = SD->getOpaqueAccessor(AccessorKind::Get))
printAbstractFunctionAsMethod(getter, false,
/*isNSUIntegerSubscript=*/false, SD);
return;
}
assert(SD->isInstanceMember() && "static subscripts not supported");
Expand Down
13 changes: 13 additions & 0 deletions test/Interop/SwiftToCxx/functions/swift-operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ public struct IntBox {
}
}

public struct CustomArray<Element> where Element : ~Copyable {
private var buffer: UnsafeMutableBufferPointer<Element>

public subscript(index: Int) -> Element {
_read {
yield buffer[index]
}
nonmutating _modify {
yield &buffer[index]
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also add a test where a Swift subscript has both get and _read accessors? I think we would want the get accessor to be used in that case, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried adding both and got this error:

error: subscript cannot provide both a 'read' accessor and a getter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah alright, I thought that is allowed, my bad

// CHECK: #if __cplusplus >= 202302L
// CHECK-NEXT: SWIFT_INLINE_THUNK int operator [](int x, int _2) const SWIFT_SYMBOL("s:9Operators6IntBoxVys5Int32VAE_AEtcig");
// CHECK-NEXT: #endif // #if __cplusplus >= 202302L
Expand Down