Skip to content

[cxx-interop] Generate safe overloads for non-escapable spans #78422

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

Conversation

Xazax-hun
Copy link
Contributor

A previous PR already added support to the SwiftifyImport macro to generate safe wrappers. This PR makes ClangImporter emit the macro to do the transformation.

func getUnqualifiedStdName(_ type: String) -> String? {
if (type.hasPrefix("std.")) {
var ty = type.dropFirst(4)
if (ty.hasPrefix("__1.")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Will it always be __1., or could it also be __2.?

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 do not anticipate a change in this, it is the same namespace for the last decade or so. But it is true that we are somewhat closely coupled to this implementation detail.

Copy link
Member

Choose a reason for hiding this comment

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

Should we make this adjustment on the C++ side where we can reason about things like inline namespaces?

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 think it would be reasonable to skip inline namespaces in the Swift names, especially since these names do leak into the diagnostics occasionally. It would be more user friendly. I am not sure if this could ever create name collisions, hopefully not. But I'd address this in a separate PR.

How closely coupled are the compiler and the standard library? In case this code supposed to work with older compiler versions, we'd want to keep the skipping logic here.

@Xazax-hun Xazax-hun force-pushed the gaborh/cxx-span-overload-importer branch from 6570674 to 25facce Compare January 3, 2025 14:34
@@ -580,7 +580,8 @@ void importer::getNormalInvocationArguments(
}
}

if (LangOpts.hasFeature(Feature::SafeInteropWrappers))
if (LangOpts.hasFeature(Feature::SafeInteropWrappers) &&
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 saw some crashes in Clang when this option was used in C++ mode.

Copy link
Member

Choose a reason for hiding this comment

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

That's fine for now, but we should dig into these crashes to see what's going on.

Copy link
Contributor

Choose a reason for hiding this comment

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

-fbounds-safety support for C++ in Clang is currently experimental, but -fexperimental-bounds-safety-attributes is used for C/C++ interop also, so that one shouldn't crash. It is a relatively new flag though, so there might be some gaps. CC @patrykstefanski for awareness.

@Xazax-hun Xazax-hun force-pushed the gaborh/cxx-span-overload-importer branch 2 times, most recently from a09d1f0 to 487e154 Compare January 7, 2025 11:20
Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

A few comments, but this LGTM

@@ -580,7 +580,8 @@ void importer::getNormalInvocationArguments(
}
}

if (LangOpts.hasFeature(Feature::SafeInteropWrappers))
if (LangOpts.hasFeature(Feature::SafeInteropWrappers) &&
Copy link
Member

Choose a reason for hiding this comment

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

That's fine for now, but we should dig into these crashes to see what's going on.

func getUnqualifiedStdName(_ type: String) -> String? {
if (type.hasPrefix("std.")) {
var ty = type.dropFirst(4)
if (ty.hasPrefix("__1.")) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we make this adjustment on the C++ side where we can reason about things like inline namespaces?

A previous PR already added support to the SwiftifyImport macro to
generate safe wrappers. This PR makes ClangImporter emit the macro to do
the transformation.
@Xazax-hun Xazax-hun force-pushed the gaborh/cxx-span-overload-importer branch from 487e154 to 4846c56 Compare January 8, 2025 11:37
@Xazax-hun Xazax-hun enabled auto-merge January 8, 2025 11:37
@Xazax-hun
Copy link
Contributor Author

@swift-ci please smoke test

@Xazax-hun Xazax-hun merged commit b31b90d into main Jan 9, 2025
3 checks passed
@Xazax-hun Xazax-hun deleted the gaborh/cxx-span-overload-importer branch January 9, 2025 12:10
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.

3 participants