Skip to content

Conversation

Xazax-hun
Copy link
Contributor

@Xazax-hun Xazax-hun commented Jun 30, 2025

This can help work around problems when the names of a C++ declaration and a Swift declaration would collide, or to temporarily work around compiler bugs.

rdar://152838988&140802127&158843666

133)

// Unused '134'
SIMPLE_DECL_ATTR(_swiftPrivate, SwiftPrivate,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We also have a swift_private attribute on the C++ side. Let me know if you think this would make this confusing (and potentially suggest an alternative name like doNotExpose, swiftOnly, disableInterop).

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we could reuse the existing @_expose attribute here with a slightly extended syntax, e.g. @_expose(!Cxx). Let's have a discussion about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

First I disliked @_expose(!Cxx) as I was wondering if it could be interpreted as "this declaration is exposed to everything expect for C++", so as if we wrote @_expose(Wasm). I thought about something like @expose(None) but that is problematic as we might want to support exposing something to Webassembly but disable exposing to C++.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DougGregor Was wondering if you have some opinions here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An alternative idea is to have "Nil" instead of the function name as a way to say not to expose it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like the @_expose(Wasm) has a slightly different meaning. It means that the function symbol should be exported from the generated shared library. This is required because in wasm everything is internal by default. So it is also possible that the Wasm and Cxx case should be divorced into two separate attributes and the Wasm one should be called something like export.

Copy link
Member

Choose a reason for hiding this comment

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

Personally, I like @_expose(!Cxx) because it puts the "expose to another language" functionality under one attribute that we can document.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any opinion on !Cxx, NotCxx, or ~Cxx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the PR to use !Cxx.

@Xazax-hun Xazax-hun force-pushed the gaborh/attr-hide-swift-decls branch from 099b2d7 to 291a1f3 Compare July 9, 2025 10:29
Copy link
Contributor

@j-hui j-hui left a comment

Choose a reason for hiding this comment

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

This LGTM.

I do have a question about an edge case though: what happens if someone writes @_expose(!Cxx, "nonsense")? This name is nonsensical since we're saying to not expose the declaration, so we're not going to use it. It's probably harmless, but I'm wondering if we should throw a warning or error if we encounter this.

@Xazax-hun Xazax-hun force-pushed the gaborh/attr-hide-swift-decls branch from 291a1f3 to 326580e Compare July 25, 2025 15:04
@Xazax-hun
Copy link
Contributor Author

we should throw a warning or error if we encounter this.

Good point, added a warning, thanks!

@Xazax-hun Xazax-hun force-pushed the gaborh/attr-hide-swift-decls branch from 326580e to 62a2a6a Compare July 25, 2025 16:11
@Xazax-hun
Copy link
Contributor Author

@swift-ci please smoke test

This can help work around problems when the names of a C++ declaration
and a Swift declaration would collide, or to temporarily work around
compiler bugs.

rdar://152838988&140802127&158843666
@Xazax-hun Xazax-hun force-pushed the gaborh/attr-hide-swift-decls branch from 62a2a6a to 7ac9a81 Compare September 1, 2025 11:30
@Xazax-hun
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

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

LGTM! Would this need a companion swift-syntax change?

@Xazax-hun
Copy link
Contributor Author

Would this need a companion swift-syntax change?

Good point! Worked for me locally out of the box but started a CI test to see if that is universally the case: swiftlang/swift-syntax#3145

@Xazax-hun Xazax-hun enabled auto-merge September 1, 2025 15:54
@Xazax-hun Xazax-hun merged commit ade5c6a into main Sep 1, 2025
3 checks passed
@Xazax-hun Xazax-hun deleted the gaborh/attr-hide-swift-decls branch September 1, 2025 17:15
Xazax-hun added a commit to Xazax-hun/swift that referenced this pull request Sep 2, 2025
Explanation: We generate declarations in the C++ interop header with
"unavailable" annotations when we cannot export something to C++. These
declarations can collide with existing names. Previously, there were no
ways to resolve these name collisions. This PR introduces a new
attribute to hide declarations from the interop header.
Issues: rdar://158843666
Original PRs: swiftlang#82616
Risk: Low, this adds a new, straightforward code path.
Testing: Added a compiler test.
Reviewers: @egorzhdan
kateinoigakukun added a commit to kateinoigakukun/swift that referenced this pull request Sep 11, 2025
swiftlang#82616 added a new exposure kind
for the `@_expose` attribute, but did not update the serialization
format to account for the new kind. This caused a crash when
serializing a module that used `@_expose(wasm)`.
kateinoigakukun added a commit to kateinoigakukun/swift that referenced this pull request Sep 22, 2025
swiftlang#82616 added a new exposure kind
for the `@_expose` attribute, but did not update the serialization
format to account for the new kind. This caused a crash when
serializing a module that used `@_expose(wasm)`.
kateinoigakukun added a commit to kateinoigakukun/swift that referenced this pull request Sep 22, 2025
swiftlang#82616 added a new exposure kind
for the `@_expose` attribute, but did not update the serialization
format to account for the new kind. This caused a crash when
serializing a module that used `@_expose(wasm)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants