-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Swiftify] Add support for free functions imported as instance methods #84507
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
Open
hnrklssn
wants to merge
11
commits into
swiftlang:main
Choose a base branch
from
hnrklssn:swiftify-import-as-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+596
−11
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c1bf742
[Swiftify] Add support for free functions imported as instance methods
hnrklssn f3cb55d
fix tests in CI
hnrklssn 98465bf
add namespace tests
hnrklssn 0fff307
use swift/bridging in test
hnrklssn eff46b4
[Swiftify] properly forward inout parameters using `&`
hnrklssn a287803
try to debug CI diff failure
hnrklssn 9427c90
[Swiftify] Reorder RUN lines (NFC)
hnrklssn cd4a287
deduplicate path
hnrklssn fd2d625
re-export std::span regardless of module name
hnrklssn 557ae3c
DO NOT MERGE: debug CI failure
hnrklssn 51c8c99
does this fix linux crash?
hnrklssn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,7 +468,14 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder { | |
let functionRef = DeclReferenceExprSyntax(baseName: base.name) | ||
let args: [ExprSyntax] = base.signature.parameterClause.parameters.enumerated() | ||
.map { (i: Int, param: FunctionParameterSyntax) in | ||
return pointerArgs[i] ?? ExprSyntax("\(param.name)") | ||
if let overrideArg = pointerArgs[i] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this is a fix we want to backport to 6.2. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that's probably a good idea |
||
return overrideArg | ||
} | ||
if isInout(getParam(base.signature, i).type) { | ||
return ExprSyntax("&\(param.name)") | ||
} else { | ||
return ExprSyntax("\(param.name)") | ||
} | ||
} | ||
let labels: [TokenSyntax?] = base.signature.parameterClause.parameters.map { param in | ||
let firstName = param.firstName.trimmed | ||
|
212 changes: 212 additions & 0 deletions
212
test/Interop/C/swiftify-import/import-as-instance-method.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
// REQUIRES: swift_feature_SafeInteropWrappers | ||
|
||
// RUN: %empty-directory(%t) | ||
// RUN: split-file %s %t | ||
|
||
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Test.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -verify -verify-additional-file %t/Inputs/instance.h %t/test.swift -I %bridging-path -DVERIFY | ||
// RUN: env SWIFT_BACKTRACE="" %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Test.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror %t/test.swift -dump-macro-expansions -I %bridging-path 2> %t/out.txt | ||
// RUN: diff --strip-trailing-cr %t/out.txt %t/out.expected | ||
|
||
//--- test.swift | ||
import Instance | ||
|
||
@available(macOS 13.3.0, *) | ||
func foo(_ p: inout MutableSpan<CInt>, a: A, aa: inout A, c: C, b: B, bb: inout B) { | ||
aa.basic(&p) | ||
aa.bar(&p) | ||
a.constSelf(&p) | ||
a.valSelf(&p) | ||
let _: MutableSpan<CInt> = a.lifetimeBoundSelf(3) | ||
c.refSelf(&p) | ||
b.nonescaping(&p) | ||
let _: MutableSpan<CInt> = bb.nonescapingLifetimebound(73) | ||
|
||
#if VERIFY | ||
aa.countedSelf(&p) | ||
a.basic(&p) // expected-error{{cannot use mutating member on immutable value: 'a' is a 'let' constant}} | ||
#endif | ||
} | ||
|
||
//--- Inputs/instance.h | ||
#include <ptrcheck.h> | ||
#include <lifetimebound.h> | ||
#include <swift/bridging> | ||
|
||
struct A {}; | ||
struct SWIFT_NONESCAPABLE B {}; | ||
struct SWIFT_IMMORTAL_REFERENCE C {}; | ||
|
||
void basic(struct A *a, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("A.basic(self:_:_:)"))); | ||
|
||
void renamed(struct A *a, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("A.bar(self:_:_:)"))); | ||
|
||
void countedSelf(struct A * __counted_by(len) | ||
a, // expected-warning{{bounds attribute '__counted_by' ignored on parameter mapped to 'self'}} | ||
int * __counted_by(len) p __noescape, int len) | ||
__attribute__(( | ||
swift_name // expected-note{{swift_name maps free function to instance method here}} | ||
("A.countedSelf(self:_:_:)"))); | ||
|
||
void constSelf(const struct A *a, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("A.constSelf(self:_:_:)"))); | ||
|
||
void valSelf(struct A a, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("A.valSelf(self:_:_:)"))); | ||
|
||
void refSelf(struct C *c, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("C.refSelf(self:_:_:)"))); | ||
|
||
int * __counted_by(len) lifetimeBoundSelf(struct A a __lifetimebound, int len) __attribute__((swift_name("A.lifetimeBoundSelf(self:_:)"))); | ||
|
||
void nonescaping(const struct B *d, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("B.nonescaping(self:_:_:)"))); | ||
|
||
int * __counted_by(len) nonescapingLifetimebound(struct B *d __lifetimebound, int len) __attribute__((swift_name("B.nonescapingLifetimebound(self:_:)"))); | ||
|
||
//--- Inputs/module.modulemap | ||
module Instance { | ||
header "instance.h" | ||
} | ||
|
||
//--- out.expected | ||
@__swiftmacro_So1AV5basic15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public mutating func basic(_ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe basic(_pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So5basic15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@available(swift, obsoleted: 3, renamed: "A.basic(self:_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public func basic(_ a: UnsafeMutablePointer<A>!, _ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe basic(a, _pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So1AV3bar15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public mutating func bar(_ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe bar(_pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So7renamed15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@available(swift, obsoleted: 3, renamed: "A.bar(self:_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public func renamed(_ a: UnsafeMutablePointer<A>!, _ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe renamed(a, _pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So1AV9constSelf15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public func constSelf(_ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe constSelf(_pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So9constSelf15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@available(swift, obsoleted: 3, renamed: "A.constSelf(self:_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public func constSelf(_ a: UnsafePointer<A>!, _ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe constSelf(a, _pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So1AV7valSelf15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public func valSelf(_ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe valSelf(_pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So7valSelf15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@available(swift, obsoleted: 3, renamed: "A.valSelf(self:_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public func valSelf(_ a: A, _ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe valSelf(a, _pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So1AV17lifetimeBoundSelf15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(borrow self) @_disfavoredOverload | ||
public func lifetimeBoundSelf(_ len: Int32) -> MutableSpan<Int32> { | ||
return unsafe _swiftifyOverrideLifetime(MutableSpan<Int32>(_unsafeStart: unsafe lifetimeBoundSelf(len), count: Int(len)), copying: ()) | ||
} | ||
------------------------------ | ||
@__swiftmacro_So17lifetimeBoundSelf15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@available(swift, obsoleted: 3, renamed: "A.lifetimeBoundSelf(self:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(borrow a) @_disfavoredOverload | ||
public func lifetimeBoundSelf(_ a: A, _ len: Int32) -> MutableSpan<Int32> { | ||
return unsafe _swiftifyOverrideLifetime(MutableSpan<Int32>(_unsafeStart: unsafe lifetimeBoundSelf(a, len), count: Int(len)), copying: ()) | ||
} | ||
------------------------------ | ||
@__swiftmacro_So1CV7refSelf15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public func refSelf(_ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe refSelf(_pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So7refSelf15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@available(swift, obsoleted: 3, renamed: "C.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 | ||
public func refSelf(_ c: C!, _ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe refSelf(c, _pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So1BV11nonescaping15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload | ||
public func nonescaping(_ p: inout MutableSpan<Int32>) { | ||
let len = Int32(exactly: p.count)! | ||
return unsafe p.withUnsafeMutableBufferPointer { _pPtr in | ||
return unsafe nonescaping(_pPtr.baseAddress!, len) | ||
} | ||
} | ||
------------------------------ | ||
@__swiftmacro_So1BV24nonescapingLifetimebound15_SwiftifyImportfMp_.swift | ||
------------------------------ | ||
/// This is an auto-generated wrapper for safer interop | ||
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(copy self) @_disfavoredOverload | ||
public mutating func nonescapingLifetimebound(_ len: Int32) -> MutableSpan<Int32> { | ||
return unsafe _swiftifyOverrideLifetime(MutableSpan<Int32>(_unsafeStart: unsafe nonescapingLifetimebound(len), count: Int(len)), copying: ()) | ||
} | ||
------------------------------ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.