Skip to content

[cxx-interop] Ignore mutable fields when detecting method mutability #42450

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
Apr 19, 2022
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
6 changes: 5 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5652,7 +5652,11 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,

bool ClangImporter::isCXXMethodMutating(const clang::CXXMethodDecl *method) {
return isa<clang::CXXConstructorDecl>(method) || !method->isConst() ||
method->getParent()->hasMutableFields() ||
// method->getParent()->hasMutableFields() ||
// FIXME(rdar://91961524): figure out a way to handle mutable fields
// without breaking classes from the C++ standard library (e.g.
// `std::string` which has a mutable member in old libstdc++ version
// used on CentOS 7)
(method->hasAttrs() &&
llvm::any_of(method->getAttrs(), [](clang::Attr *a) {
if (auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(a)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=MutableMembers -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s

// REQUIRES: rdar91961524

// CHECK: struct HasPublicMutableMember {
// CHECK: mutating func foo() -> Int32
// CHECK: var a: Int32
Expand Down
2 changes: 2 additions & 0 deletions test/Interop/Cxx/class/mutable-members-typechecker.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop

// REQUIRES: rdar91961524

import MutableMembers

let obj = HasPublicMutableMember(a: 42) // expected-note {{change 'let' to 'var' to make it mutable}}
Expand Down
2 changes: 2 additions & 0 deletions test/Interop/Cxx/class/mutable-members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// REQUIRES: executable_test

// REQUIRES: rdar91961524

import StdlibUnittest
import MutableMembers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ struct NoNameVoidGetter {
};

struct LongNameAllLower {
mutable int value = 42;
int value = 42;
int getfoo() const { return value; }
void setfoo(int v) const { value = v; }
void setfoo(int v) { value = v; }
};

struct LongNameAllUpper {
mutable int value = 42;
int value = 42;
int getFOO() const { return value; }
void setFOO(int v) const { value = v; }
void setFOO(int v) { value = v; }
};

struct UpperCaseMix {
mutable int value = 42;
int value = 42;
int getFoo() const { return value; }
void SetFoo(int v) { value = v; }
};

struct UpperCaseGetterSetter {
mutable int value = 42;
int value = 42;
int GetFoo() const { return value; }
void SetFoo(int v) { value = v; }
};
Expand Down Expand Up @@ -132,7 +132,7 @@ struct ConstSetter {
};

struct MultipleArgsSetter {
int getX();
int getX() const;
void setX(int a, int b);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,35 @@
// CHECK-NOT: var
// CHECK-NEXT: init()
// CHECK-NEXT: init(value: Int32)
// CHECK-NEXT: var foo: Int32 { mutating get set }
// CHECK-NEXT: mutating func getfoo() -> Int32
// CHECK-NEXT: var foo: Int32
// CHECK-NEXT: func getfoo() -> Int32
// CHECK-NEXT: mutating func setfoo(_ v: Int32)
// CHECK-NEXT: var value: Int32
// CHECK-NEXT: }

// CHECK: struct LongNameAllUpper {
// CHECK-NEXT: init()
// CHECK-NEXT: init(value: Int32)
// CHECK-NEXT: var foo: Int32 { mutating get set }
// CHECK-NEXT: mutating func getFOO() -> Int32
// CHECK-NEXT: var foo: Int32
// CHECK-NEXT: func getFOO() -> Int32
// CHECK-NEXT: mutating func setFOO(_ v: Int32)
// CHECK-NEXT: var value: Int32
// CHECK-NEXT: }

// CHECK: struct UpperCaseMix {
// CHECK-NEXT: init()
// CHECK-NEXT: init(value: Int32)
// CHECK-NEXT: var foo: Int32 { mutating get set }
// CHECK-NEXT: mutating func getFoo() -> Int32
// CHECK-NEXT: var foo: Int32
// CHECK-NEXT: func getFoo() -> Int32
// CHECK-NEXT: mutating func SetFoo(_ v: Int32)
// CHECK-NEXT: var value: Int32
// CHECK-NEXT: }

// CHECK: struct UpperCaseGetterSetter {
// CHECK-NEXT: init()
// CHECK-NEXT: init(value: Int32)
// CHECK-NEXT: var foo: Int32 { mutating get set }
// CHECK-NEXT: mutating func GetFoo() -> Int32
// CHECK-NEXT: var foo: Int32
// CHECK-NEXT: func GetFoo() -> Int32
// CHECK-NEXT: mutating func SetFoo(_ v: Int32)
// CHECK-NEXT: var value: Int32
// CHECK-NEXT: }
Expand Down Expand Up @@ -176,19 +176,20 @@
// CHECK-NEXT: var val: Int32
// CHECK-NEXT: }

// CHECK: struct ConstSetter {
// CHECK-NEXT: init()
// CHECK-NEXT: init(val: Int32)
// CHECK-NEXT: var x: Int32 { mutating get set }
// CHECK-NEXT: mutating func getX() -> Int32
// CHECK-NEXT: mutating func setX(_ v: Int32)
// CHECK-NEXT: var val: Int32
// CHECK-NEXT: }
// FIXME: rdar91961524
// TODO-CHECK: struct ConstSetter {
// TODO-CHECK-NEXT: init()
// TODO-CHECK-NEXT: init(val: Int32)
// TODO-CHECK-NEXT: var x: Int32 { mutating get set }
// TODO-CHECK-NEXT: mutating func getX() -> Int32
// TODO-CHECK-NEXT: mutating func setX(_ v: Int32)
// TODO-CHECK-NEXT: var val: Int32
// TODO-CHECK-NEXT: }

// CHECK: struct MultipleArgsSetter {
// CHECK-NEXT: init()
// CHECK-NEXT: var x: Int32 { mutating get set }
// CHECK-NEXT: mutating func getXMutating() -> Int32
// CHECK-NEXT: var x: Int32
// CHECK-NEXT: func getX() -> Int32
// CHECK-NEXT: mutating func setXMutating(_ a: Int32, _ b: Int32)
// CHECK-NEXT: }

Expand Down
2 changes: 1 addition & 1 deletion test/Interop/Cxx/stdlib/use-std-string.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import std.string
var StdStringTestSuite = TestSuite("StdString")

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