Skip to content

[cxx-interop] Builtin functions should ignore return type nullability… #74494

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 3 commits into from
Jun 26, 2024

Conversation

hyp
Copy link
Contributor

@hyp hyp commented Jun 17, 2024

… to be compatible with C interop

In C interop mode, the return type of builtin functions like 'memcpy' drops any nullability specifiers from their return type, and preserves it on the declared return type. Thus, in C mode the imported return type of such functions is always optional. However, in C++ interop mode, the return type of builtin functions can preseve the nullability specifiers on their return type, and thus the imported return type of such functions can be non-optional, if the type is annotated with _Nonnull. The difference between these two modes can break cross-module Swift serialization, as Swift will no longer be able to resolve an x-ref such as 'memcpy' from a Swift module that uses C interop, within a Swift context that uses C++ interop. In order to avoid the x-ref resolution failure, normalize the return type's nullability for builtin functions in C++ interop mode, to match the imported type in C interop mode.

This fixed an issue when using 'memcpy' from the Android NDK in a x-module context, like between Foundation (that inlines it) and another user module.

@hyp
Copy link
Contributor Author

hyp commented Jun 17, 2024

@swift-ci please smoke test

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Jun 18, 2024
@hyp hyp force-pushed the eng/android-memcpy-nonnull-me-timbers branch from ce9493d to 5976241 Compare June 20, 2024 17:20
@hyp
Copy link
Contributor Author

hyp commented Jun 20, 2024

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Jun 20, 2024

@swift-ci please test source compatibility

@hyp hyp force-pushed the eng/android-memcpy-nonnull-me-timbers branch from 5976241 to 8426df1 Compare June 20, 2024 17:21
@hyp
Copy link
Contributor Author

hyp commented Jun 20, 2024

@swift-ci please test source compatibility

@hyp
Copy link
Contributor Author

hyp commented Jun 20, 2024

@swift-ci please test

@hyp hyp force-pushed the eng/android-memcpy-nonnull-me-timbers branch from 8426df1 to 5a28cdb Compare June 20, 2024 17:22
@hyp
Copy link
Contributor Author

hyp commented Jun 20, 2024

@swift-ci please test source compatibility

@hyp
Copy link
Contributor Author

hyp commented Jun 20, 2024

@swift-ci please test

@hyp hyp force-pushed the eng/android-memcpy-nonnull-me-timbers branch from 5a28cdb to b92fcc2 Compare June 20, 2024 22:34
@hyp
Copy link
Contributor Author

hyp commented Jun 20, 2024

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Jun 20, 2024

@swift-ci please test source compatibility

@hyp
Copy link
Contributor Author

hyp commented Jun 21, 2024

@swift-ci please test windows platform

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

…re return type nullability to be compatible with C interop

In C interop mode, the return type of builtin functions like 'memcpy' from headers like 'string.h' drops any nullability
specifiers from their return type, and preserves it on the declared return type. Thus, in C
mode the imported return type of such functions is always optional. However, in C++ interop mode, the
return type of builtin functions can preseve the nullability specifiers on their return type,
and thus the imported return type of such functions can be non-optional, if the type is annotated with
`_Nonnull`. The difference between these two modes can break cross-module Swift serialization, as
Swift will no longer be able to resolve an x-ref such as 'memcpy' from a Swift module that uses
C interop, within a Swift context that uses C++ interop. In order to avoid the x-ref resolution
failure, normalize the return type's nullability for builtin functions in C++ interop mode, to
match the imported type in C interop mode.

This fixed an issue when using 'memcpy' from the Android NDK in a x-module context, like
between Foundation (that inlines it) and another user module.
@hyp hyp force-pushed the eng/android-memcpy-nonnull-me-timbers branch from b92fcc2 to e4ad3c1 Compare June 21, 2024 18:05
@hyp
Copy link
Contributor Author

hyp commented Jun 21, 2024

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Jun 21, 2024

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Jun 25, 2024

@swift-ci please test windows platform

@hyp
Copy link
Contributor Author

hyp commented Jun 25, 2024

@swift-ci please test windows platform

1 similar comment
@hyp
Copy link
Contributor Author

hyp commented Jun 25, 2024

@swift-ci please test windows platform

@hyp
Copy link
Contributor Author

hyp commented Jun 25, 2024

@swift-ci please test

@hyp hyp enabled auto-merge June 26, 2024 02:11
@hyp hyp merged commit 466ae71 into swiftlang:main Jun 26, 2024
4 of 5 checks passed
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