Skip to content

Commit 8b57db7

Browse files
authored
Merge pull request #42450 from apple/egorzhdan/cxx-ignore-mutable
[cxx-interop] Ignore mutable fields when detecting method mutability
2 parents c2d175d + fca494e commit 8b57db7

File tree

7 files changed

+38
-27
lines changed

7 files changed

+38
-27
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5652,7 +5652,11 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
56525652

56535653
bool ClangImporter::isCXXMethodMutating(const clang::CXXMethodDecl *method) {
56545654
return isa<clang::CXXConstructorDecl>(method) || !method->isConst() ||
5655-
method->getParent()->hasMutableFields() ||
5655+
// method->getParent()->hasMutableFields() ||
5656+
// FIXME(rdar://91961524): figure out a way to handle mutable fields
5657+
// without breaking classes from the C++ standard library (e.g.
5658+
// `std::string` which has a mutable member in old libstdc++ version
5659+
// used on CentOS 7)
56565660
(method->hasAttrs() &&
56575661
llvm::any_of(method->getAttrs(), [](clang::Attr *a) {
56585662
if (auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(a)) {

test/Interop/Cxx/class/mutable-members-module-interface.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=MutableMembers -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
22

3+
// REQUIRES: rdar91961524
4+
35
// CHECK: struct HasPublicMutableMember {
46
// CHECK: mutating func foo() -> Int32
57
// CHECK: var a: Int32

test/Interop/Cxx/class/mutable-members-typechecker.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop
22

3+
// REQUIRES: rdar91961524
4+
35
import MutableMembers
46

57
let obj = HasPublicMutableMember(a: 42) // expected-note {{change 'let' to 'var' to make it mutable}}

test/Interop/Cxx/class/mutable-members.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// REQUIRES: executable_test
44

5+
// REQUIRES: rdar91961524
6+
57
import StdlibUnittest
68
import MutableMembers
79

test/Interop/Cxx/ergonomics/Inputs/implicit-computed-properties.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,25 @@ struct NoNameVoidGetter {
2727
};
2828

2929
struct LongNameAllLower {
30-
mutable int value = 42;
30+
int value = 42;
3131
int getfoo() const { return value; }
32-
void setfoo(int v) const { value = v; }
32+
void setfoo(int v) { value = v; }
3333
};
3434

3535
struct LongNameAllUpper {
36-
mutable int value = 42;
36+
int value = 42;
3737
int getFOO() const { return value; }
38-
void setFOO(int v) const { value = v; }
38+
void setFOO(int v) { value = v; }
3939
};
4040

4141
struct UpperCaseMix {
42-
mutable int value = 42;
42+
int value = 42;
4343
int getFoo() const { return value; }
4444
void SetFoo(int v) { value = v; }
4545
};
4646

4747
struct UpperCaseGetterSetter {
48-
mutable int value = 42;
48+
int value = 42;
4949
int GetFoo() const { return value; }
5050
void SetFoo(int v) { value = v; }
5151
};
@@ -132,7 +132,7 @@ struct ConstSetter {
132132
};
133133

134134
struct MultipleArgsSetter {
135-
int getX();
135+
int getX() const;
136136
void setX(int a, int b);
137137
};
138138

test/Interop/Cxx/ergonomics/implicit-computed-properties-module-interface.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,35 @@
4141
// CHECK-NOT: var
4242
// CHECK-NEXT: init()
4343
// CHECK-NEXT: init(value: Int32)
44-
// CHECK-NEXT: var foo: Int32 { mutating get set }
45-
// CHECK-NEXT: mutating func getfoo() -> Int32
44+
// CHECK-NEXT: var foo: Int32
45+
// CHECK-NEXT: func getfoo() -> Int32
4646
// CHECK-NEXT: mutating func setfoo(_ v: Int32)
4747
// CHECK-NEXT: var value: Int32
4848
// CHECK-NEXT: }
4949

5050
// CHECK: struct LongNameAllUpper {
5151
// CHECK-NEXT: init()
5252
// CHECK-NEXT: init(value: Int32)
53-
// CHECK-NEXT: var foo: Int32 { mutating get set }
54-
// CHECK-NEXT: mutating func getFOO() -> Int32
53+
// CHECK-NEXT: var foo: Int32
54+
// CHECK-NEXT: func getFOO() -> Int32
5555
// CHECK-NEXT: mutating func setFOO(_ v: Int32)
5656
// CHECK-NEXT: var value: Int32
5757
// CHECK-NEXT: }
5858

5959
// CHECK: struct UpperCaseMix {
6060
// CHECK-NEXT: init()
6161
// CHECK-NEXT: init(value: Int32)
62-
// CHECK-NEXT: var foo: Int32 { mutating get set }
63-
// CHECK-NEXT: mutating func getFoo() -> Int32
62+
// CHECK-NEXT: var foo: Int32
63+
// CHECK-NEXT: func getFoo() -> Int32
6464
// CHECK-NEXT: mutating func SetFoo(_ v: Int32)
6565
// CHECK-NEXT: var value: Int32
6666
// CHECK-NEXT: }
6767

6868
// CHECK: struct UpperCaseGetterSetter {
6969
// CHECK-NEXT: init()
7070
// CHECK-NEXT: init(value: Int32)
71-
// CHECK-NEXT: var foo: Int32 { mutating get set }
72-
// CHECK-NEXT: mutating func GetFoo() -> Int32
71+
// CHECK-NEXT: var foo: Int32
72+
// CHECK-NEXT: func GetFoo() -> Int32
7373
// CHECK-NEXT: mutating func SetFoo(_ v: Int32)
7474
// CHECK-NEXT: var value: Int32
7575
// CHECK-NEXT: }
@@ -176,19 +176,20 @@
176176
// CHECK-NEXT: var val: Int32
177177
// CHECK-NEXT: }
178178

179-
// CHECK: struct ConstSetter {
180-
// CHECK-NEXT: init()
181-
// CHECK-NEXT: init(val: Int32)
182-
// CHECK-NEXT: var x: Int32 { mutating get set }
183-
// CHECK-NEXT: mutating func getX() -> Int32
184-
// CHECK-NEXT: mutating func setX(_ v: Int32)
185-
// CHECK-NEXT: var val: Int32
186-
// CHECK-NEXT: }
179+
// FIXME: rdar91961524
180+
// TODO-CHECK: struct ConstSetter {
181+
// TODO-CHECK-NEXT: init()
182+
// TODO-CHECK-NEXT: init(val: Int32)
183+
// TODO-CHECK-NEXT: var x: Int32 { mutating get set }
184+
// TODO-CHECK-NEXT: mutating func getX() -> Int32
185+
// TODO-CHECK-NEXT: mutating func setX(_ v: Int32)
186+
// TODO-CHECK-NEXT: var val: Int32
187+
// TODO-CHECK-NEXT: }
187188

188189
// CHECK: struct MultipleArgsSetter {
189190
// CHECK-NEXT: init()
190-
// CHECK-NEXT: var x: Int32 { mutating get set }
191-
// CHECK-NEXT: mutating func getXMutating() -> Int32
191+
// CHECK-NEXT: var x: Int32
192+
// CHECK-NEXT: func getX() -> Int32
192193
// CHECK-NEXT: mutating func setXMutating(_ a: Int32, _ b: Int32)
193194
// CHECK-NEXT: }
194195

test/Interop/Cxx/stdlib/use-std-string.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import std.string
1717
var StdStringTestSuite = TestSuite("StdString")
1818

1919
StdStringTestSuite.test("init") {
20-
var s = CxxString() // declared as `var` because of outdated libstdc++ on CentOS 7
20+
let s = CxxString()
2121
expectEqual(s.size(), 0)
2222
expectTrue(s.empty())
2323
}

0 commit comments

Comments
 (0)