Skip to content

Commit c872e30

Browse files
committed
[cxx-interop] make sure a const operator [] is imported as a mutable setter
1 parent 446dfbf commit c872e30

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,6 +3569,12 @@ namespace {
35693569
if (parameter->isInOut())
35703570
// Subscripts with inout parameters are not allowed in Swift.
35713571
return nullptr;
3572+
// Subscript setter is marked as mutating in Swift even if the
3573+
// C++ `operator []` is `const`.
3574+
if (importedName.getAccessorKind() ==
3575+
ImportedAccessorKind::SubscriptSetter &&
3576+
!dc->isModuleScopeContext())
3577+
func->setSelfAccessKind(SelfAccessKind::Mutating);
35723578

35733579
auto &getterAndSetter = Impl.cxxSubscripts[{ typeDecl,
35743580
parameterType }];

test/Interop/Cxx/operators/Inputs/member-inline.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,17 @@ struct DerivedFromConstIterator : public ConstIterator {};
418418

419419
struct DerivedFromConstIteratorPrivately : private ConstIterator {};
420420

421+
class SubscriptSetterConst {
422+
public:
423+
using T = int;
424+
425+
SubscriptSetterConst() : p(new T[10]) {}
426+
427+
T& operator[](int i) const {
428+
return p[i];
429+
}
430+
private:
431+
T *p;
432+
};
433+
421434
#endif

test/Interop/Cxx/operators/member-inline.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,9 @@ OperatorsTestSuite.test("DerivedFromConstIterator.pointee") {
378378
expectEqual(234, res)
379379
}
380380

381+
OperatorsTestSuite.test("SubscriptSetterConst") {
382+
var setterConst = SubscriptSetterConst()
383+
setterConst[0] = 10
384+
}
385+
381386
runAllTests()

0 commit comments

Comments
 (0)