Skip to content

Commit 7eefb30

Browse files
committed
[interop][SwiftToCxx] emit property accessors in class types correctly
property accessors could potentially mutate the class instance, so don't make them const
1 parent c0bda06 commit 7eefb30

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxPropertyAccessorMethod(
713713
if (isDefinition)
714714
modifiers.qualifierContext = typeDeclContext;
715715
modifiers.isInline = true;
716-
modifiers.isConst = accessor->isGetter();
716+
modifiers.isConst = accessor->isGetter() && !isa<ClassDecl>(typeDeclContext);
717717
printFunctionSignature(accessor, remapPropertyName(accessor, resultTy),
718718
resultTy, FunctionSignatureKind::CxxInlineThunk, {},
719719
modifiers);

test/Interop/SwiftToCxx/properties/getter-in-cxx-execution.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,11 @@ int main() {
5353
// CHECK-NEXT: create RefCountedClass 1
5454
// CHECK-NEXT: destroy RefCountedClass 1
5555
// CHECK-NEXT: destroy RefCountedClass 0
56+
57+
auto propsInClass = createPropsInClass(-1234);
58+
assert(propsInClass.getStoredInt() == -1234);
59+
assert(propsInClass.getComputedInt() == -1235);
60+
auto smallStructFromClass = propsInClass.getSmallStruct();
61+
assert(smallStructFromClass.getX() == 1234);
5662
return 0;
5763
}

test/Interop/SwiftToCxx/properties/getter-in-cxx.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,44 @@ public struct LargeStruct {
7272
// CHECK-NEXT: });
7373
// CHECK-NEXT: }
7474

75+
public final class PropertiesInClass {
76+
public let storedInt: Int32
77+
78+
init(_ x: Int32) {
79+
storedInt = x
80+
}
81+
82+
public var computedInt: Int {
83+
return Int(storedInt) - 1
84+
}
85+
86+
public var smallStruct: FirstSmallStruct {
87+
return FirstSmallStruct(x: UInt32(-storedInt));
88+
}
89+
}
90+
91+
// CHECK: class PropertiesInClass final : public swift::_impl::RefCountedClass {
92+
// CHECK: using RefCountedClass::operator=;
93+
// CHECK-NEXT: inline int32_t getStoredInt();
94+
// CHECK-NEXT: inline swift::Int getComputedInt();
95+
// CHECK-NEXT: inline FirstSmallStruct getSmallStruct();
96+
97+
// CHECK: inline int32_t PropertiesInClass::getStoredInt() {
98+
// CHECK-NEXT: return _impl::$s10Properties0A7InClassC9storedInts5Int32Vvg(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
99+
// CHECK-NEXT: }
100+
// CHECK-NEXT: inline swift::Int PropertiesInClass::getComputedInt() {
101+
// CHECK-NEXT: return _impl::$s10Properties0A7InClassC11computedIntSivg(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
102+
// CHECK-NEXT: }
103+
// CHECK-NEXT: inline FirstSmallStruct PropertiesInClass::getSmallStruct() {
104+
// CHECK-NEXT: return _impl::_impl_FirstSmallStruct::returnNewValue([&](char * _Nonnull result) {
105+
// CHECK-NEXT: _impl::swift_interop_returnDirect_Properties_FirstSmallStruct(result, _impl::$s10Properties0A7InClassC11smallStructAA010FirstSmallE0Vvg(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this)));
106+
// CHECK-NEXT: });
107+
// CHECK-NEXT: }
108+
109+
public func createPropsInClass(_ x: Int32) -> PropertiesInClass {
110+
return PropertiesInClass(x)
111+
}
112+
75113
public struct SmallStructWithGetters {
76114
public let storedInt: UInt32
77115
public var computedInt: Int {

0 commit comments

Comments
 (0)