File tree Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -3569,6 +3569,13 @@ 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+ !typeDecl->getDeclaredType ()->isForeignReferenceType ())
3578+ func->setSelfAccessKind (SelfAccessKind::Mutating);
35723579
35733580 auto &getterAndSetter = Impl.cxxSubscripts [{ typeDecl,
35743581 parameterType }];
Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ FunctionsTestSuite.test("base member FRT subscript access") {
4949FunctionsTestSuite . test ( " base member FRT subscript accessing reference FRT " ) {
5050 let copyCounter = getCopyCounter ( ) . pointee
5151
52- let base = makeBaseReturningFRTFromSubscript ( ) !
52+ var base = makeBaseReturningFRTFromSubscript ( ) !
5353 var frt = base [ 1 ]
5454 expectEqual ( frt. getX ( ) , 1 )
5555
Original file line number Diff line number Diff line change @@ -418,4 +418,17 @@ struct DerivedFromConstIterator : public ConstIterator {};
418418
419419struct 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
Original file line number Diff line number Diff 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+
381386runAllTests ( )
You can’t perform that action at this time.
0 commit comments