Skip to content

Commit 663ba2e

Browse files
committed
[Swiftify] properly forward inout parameters using &
While we handled prepending & to MutableSpan parameters, regular parameters that were unchanged during the transformation were not checked for inout-ness.
1 parent 439b9e4 commit 663ba2e

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9552,8 +9552,10 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
95529552
isa<clang::CXXMethodDecl>(ClangDecl) &&
95539553
!isa<clang::CXXConstructorDecl>(ClangDecl) &&
95549554
cast<clang::CXXMethodDecl>(ClangDecl)->isInstance();
9555+
size_t swiftNumParams = MappedDecl->getParameters()->size() -
9556+
(ClangDecl->isVariadic() ? 1 : 0);
95559557
ASSERT((MappedDecl->isImportAsInstanceMember() == isClangInstanceMethod) ==
9556-
(ClangDecl->getNumParams() == MappedDecl->getParameters()->size()));
9558+
(ClangDecl->getNumParams() == swiftNumParams));
95579559

95589560
size_t selfParamIndex = MappedDecl->isImportAsInstanceMember()
95599561
? MappedDecl->getSelfIndex()

lib/Macros/Sources/SwiftMacros/SwiftifyImportMacro.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,14 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
468468
let functionRef = DeclReferenceExprSyntax(baseName: base.name)
469469
let args: [ExprSyntax] = base.signature.parameterClause.parameters.enumerated()
470470
.map { (i: Int, param: FunctionParameterSyntax) in
471-
return pointerArgs[i] ?? ExprSyntax("\(param.name)")
471+
if let overrideArg = pointerArgs[i] {
472+
return overrideArg
473+
}
474+
if isInout(getParam(base.signature, i).type) {
475+
return ExprSyntax("&\(param.name)")
476+
} else {
477+
return ExprSyntax("\(param.name)")
478+
}
472479
}
473480
let labels: [TokenSyntax?] = base.signature.parameterClause.parameters.map { param in
474481
let firstName = param.firstName.trimmed

test/Interop/Cxx/swiftify-import/import-as-instance-method.swift

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func foo(_ p: inout MutableSpan<CInt>, a: A, aa: inout A, s: IntSpan, cs: ConstI
1818
aa.refSelf(&p)
1919
aa.namespaced(&p)
2020
b.decapseman(&p)
21+
baz.bar(&aa, &p)
22+
baz.this(&aa, &p)
2123

2224
let _: IntSpan = unsafe s.spanSelf()
2325
let _: MutableSpan<CInt> = unsafe s.spanSelf()
@@ -62,6 +64,8 @@ ConstIntSpan constSpanSelf(ConstIntSpan p __lifetimebound) __attribute__((swift_
6264
namespace baz {
6365
void namespaced(A *a, IntSpan p __noescape) __attribute__((swift_name("A.namespaced(self:_:)")));
6466
struct B {};
67+
void renamed(A &a, IntSpan p __noescape) __attribute__((swift_name("baz.bar(_:_:)")));
68+
void that(A &a, IntSpan p __noescape) __attribute__((swift_name("this(_:_:)")));
6569
}
6670

6771
void decapseman(baz::B *b, IntSpan p __noescape) __attribute__((swift_name("baz.B.decapseman(self:_:)")));
@@ -169,7 +173,7 @@ public mutating func refSelf(_ p: inout MutableSpan<CInt>) {
169173
@available(swift, obsoleted: 3, renamed: "A.refSelf(self:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload
170174
public func refSelf(_ a: inout A, _ p: inout MutableSpan<CInt>) {
171175
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in
172-
return unsafe refSelf(a, IntSpan(_pPtr))
176+
return unsafe refSelf(&a, IntSpan(_pPtr))
173177
}
174178
}
175179
------------------------------
@@ -213,6 +217,46 @@ public func decapseman(_ b: UnsafeMutablePointer<baz.B>!, _ p: inout MutableSpan
213217
}
214218
}
215219
------------------------------
220+
@__swiftmacro_So3bazO8InstanceE3bar15_SwiftifyImportfMp_.swift
221+
------------------------------
222+
/// This is an auto-generated wrapper for safer interop
223+
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload
224+
public static func bar(_ a: inout A, _ p: inout MutableSpan<CInt>) {
225+
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in
226+
return unsafe bar(&a, IntSpan(_pPtr))
227+
}
228+
}
229+
------------------------------
230+
@__swiftmacro_So3bazO7renamed15_SwiftifyImportfMp_.swift
231+
------------------------------
232+
/// This is an auto-generated wrapper for safer interop
233+
@available(swift, obsoleted: 3, renamed: "baz.bar(_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload
234+
public static func renamed(_ a: inout A, _ p: inout MutableSpan<CInt>) {
235+
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in
236+
return unsafe renamed(&a, IntSpan(_pPtr))
237+
}
238+
}
239+
------------------------------
240+
@__swiftmacro_So3bazO4this15_SwiftifyImportfMp_.swift
241+
------------------------------
242+
/// This is an auto-generated wrapper for safer interop
243+
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload
244+
public static func this(_ a: inout A, _ p: inout MutableSpan<CInt>) {
245+
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in
246+
return unsafe this(&a, IntSpan(_pPtr))
247+
}
248+
}
249+
------------------------------
250+
@__swiftmacro_So3bazO4that15_SwiftifyImportfMp_.swift
251+
------------------------------
252+
/// This is an auto-generated wrapper for safer interop
253+
@available(swift, obsoleted: 3, renamed: "this(_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload
254+
public static func that(_ a: inout A, _ p: inout MutableSpan<CInt>) {
255+
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in
256+
return unsafe that(&a, IntSpan(_pPtr))
257+
}
258+
}
259+
------------------------------
216260
@__swiftmacro_So3stdO3__1O0056spanCInt_CUnsignedLong_18446744073709551615_syGJqopasxheV8InstanceE8spanSelf15_SwiftifyImportfMp_.swift
217261
------------------------------
218262
/// This is an auto-generated wrapper for safer interop

0 commit comments

Comments
 (0)