Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Swiftify] Handle anonymous parameters #81384

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
May 9, 2025

Conversation

hnrklssn
Copy link
Contributor

@hnrklssn hnrklssn commented May 8, 2025

_SwiftifyImport would expand with syntax errors if applied to a function with anonymous parameters, because it would try to refer to parameters using the name _. Detect these cases and create names for unnamed parameters.

rdar://150955944

@hnrklssn
Copy link
Contributor Author

hnrklssn commented May 8, 2025

@swift-ci please smoke test

_SwiftifyImport would expand with syntax errors if applied to a function
with anonymous parameters, because it would try to refer to parameters
using the name `_`. Detect these cases and create names for unnamed
parameters.

rdar://150955944
@hnrklssn hnrklssn force-pushed the swiftify-anonymous-params branch from 73cfb77 to 0a37a5d Compare May 9, 2025 00:13
@hnrklssn
Copy link
Contributor Author

hnrklssn commented May 9, 2025

@swift-ci please smoke test

Comment on lines +430 to +432
let param = e.with(\.type, (argTypes[i] ?? e.type)!)
let name = param.secondName ?? param.firstName
if name.trimmed.text == "_" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the redundant logic to extract the parameter name and check for _?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this is needed for general swift syntax, e.g. if you have func foo(bar: T). If you just blindly do .with(\.secondName, getParamName(param, i)), then you end up with func foo(bar bar: T)). Which I guess is not actually a problem, just redundant. But I also don't think we can end up with that syntax from an imported C function, so that's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait no that redundancy is actually an error:

   | `- note: in expansion of macro '_SwiftifyImport' on global function 'ptrNamed(ptr:_:)' here
 6 | func ptrNamed(ptr: UnsafePointer<CInt>, _ len: CInt) {
 7 | }
   +--- macro expansion @_SwiftifyImport -------------------------------
   |1 | @_alwaysEmitIntoClient
   |2 | func ptrNamed(ptr ptr: UnsafeBufferPointer<CInt>) {
   |  |               `- error: extraneous duplicate parameter name; 'ptr' already has an argument label
   |3 |     return unsafe ptrNamed(ptr: ptr.baseAddress!, CInt(exactly: ptr.count)!)
   |4 | }
   +--------------------------------------------------------------------

I don't know of any way a C function can import to that syntax, but it doesn't hurt to handle it correctly.

@hnrklssn hnrklssn merged commit 8e27947 into swiftlang:main May 9, 2025
3 checks passed
hnrklssn added a commit to hnrklssn/swift that referenced this pull request May 9, 2025
_SwiftifyImport would expand with syntax errors if applied to a function
with anonymous parameters, because it would try to refer to parameters
using the name `_`. Detect these cases and create names for unnamed
parameters.

rdar://150955944
(cherry picked from commit 8e27947)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants