diff --git a/lib/ClangImporter/bridging b/lib/ClangImporter/bridging index 27bb6678aafb0..5ab29661abace 100644 --- a/lib/ClangImporter/bridging +++ b/lib/ClangImporter/bridging @@ -133,6 +133,13 @@ #define SWIFT_COMPUTED_PROPERTY \ __attribute__((swift_attr("import_computed_property"))) +/// Specifies that a specific **constant** C++ member function should be imported as +/// `mutating` Swift method. This annotation should be added to constant C++ member functions +/// that mutate `mutable` fields in a C++ object, to let Swift know that this function is still mutating +/// and thus that it should become a `mutating` method in Swift. +#define SWIFT_MUTATING \ + __attribute__((swift_attr("mutating"))) + #else // #if _CXX_INTEROP_HAS_ATTRIBUTE(swift_attr) // Empty defines for compilers that don't support `attribute(swift_attr)`. @@ -144,6 +151,7 @@ #define SWIFT_NAME(_name) #define SWIFT_CONFORMS_TO_PROTOCOL(_moduleName_protocolName) #define SWIFT_COMPUTED_PROPERTY +#define SWIFT_MUTATING #endif // #if _CXX_INTEROP_HAS_ATTRIBUTE(swift_attr) diff --git a/test/Interop/Cxx/class/Inputs/mutable-members.h b/test/Interop/Cxx/class/Inputs/mutable-members.h index 9ebe807afb55b..e40871642ea97 100644 --- a/test/Interop/Cxx/class/Inputs/mutable-members.h +++ b/test/Interop/Cxx/class/Inputs/mutable-members.h @@ -1,10 +1,19 @@ #ifndef TEST_INTEROP_CXX_CLASS_INPUTS_MUTABLE_MEMBERS_H #define TEST_INTEROP_CXX_CLASS_INPUTS_MUTABLE_MEMBERS_H +#ifdef USE_MUTATING +// Note: in actuality, this will be included +// as , but in this test we include +// it directly. +#include "bridging" +#else +#define SWIFT_MUTATING +#endif + struct HasPublicMutableMember { mutable int a = 0; - int foo() const { + int foo() const SWIFT_MUTATING { a++; return a; } @@ -15,7 +24,7 @@ struct HasPrivateMutableMember { mutable int a = 0; public: - void bar() const { a++; } + void bar() const SWIFT_MUTATING { a++; } }; #endif // TEST_INTEROP_CXX_CLASS_INPUTS_MUTABLE_MEMBERS_H diff --git a/test/Interop/Cxx/class/mutable-members-with-mutating-annotation-module-interface.swift b/test/Interop/Cxx/class/mutable-members-with-mutating-annotation-module-interface.swift new file mode 100644 index 0000000000000..71d76fccf4b2d --- /dev/null +++ b/test/Interop/Cxx/class/mutable-members-with-mutating-annotation-module-interface.swift @@ -0,0 +1,10 @@ +// RUN: %target-swift-ide-test -print-module -module-to-print=MutableMembers -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop -Xcc -DUSE_MUTATING -I %swift_src_root/lib/ClangImporter | %FileCheck %s + +// CHECK: struct HasPublicMutableMember { +// CHECK: mutating func foo() -> Int32 +// CHECK: var a: Int32 +// CHECK: } + +// CHECK: struct HasPrivateMutableMember { +// CHECK: mutating func bar() +// CHECK: }