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

Skip to content

Conversation

@hnrklssn
Copy link
Member

@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
Member 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
Member 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
Member 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
Member 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
Member 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)
Catfish-Man pushed a commit that referenced this pull request May 15, 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
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