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 {
3569
3569
if (parameter->isInOut ())
3570
3570
// Subscripts with inout parameters are not allowed in Swift.
3571
3571
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);
3572
3579
3573
3580
auto &getterAndSetter = Impl.cxxSubscripts [{ typeDecl,
3574
3581
parameterType }];
Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ FunctionsTestSuite.test("base member FRT subscript access") {
49
49
FunctionsTestSuite . test ( " base member FRT subscript accessing reference FRT " ) {
50
50
let copyCounter = getCopyCounter ( ) . pointee
51
51
52
- let base = makeBaseReturningFRTFromSubscript ( ) !
52
+ var base = makeBaseReturningFRTFromSubscript ( ) !
53
53
var frt = base [ 1 ]
54
54
expectEqual ( frt. getX ( ) , 1 )
55
55
Original file line number Diff line number Diff line change @@ -418,4 +418,17 @@ struct DerivedFromConstIterator : public ConstIterator {};
418
418
419
419
struct DerivedFromConstIteratorPrivately : private ConstIterator {};
420
420
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
+
421
434
#endif
Original file line number Diff line number Diff line change @@ -378,4 +378,9 @@ OperatorsTestSuite.test("DerivedFromConstIterator.pointee") {
378
378
expectEqual ( 234 , res)
379
379
}
380
380
381
+ OperatorsTestSuite . test ( " SubscriptSetterConst " ) {
382
+ var setterConst = SubscriptSetterConst ( )
383
+ setterConst [ 0 ] = 10
384
+ }
385
+
381
386
runAllTests ( )
You can’t perform that action at this time.
0 commit comments