Skip to content

[cxx-interop] Import nullability of templated function parameters correctly #82161

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
Jun 17, 2025

Conversation

egorzhdan
Copy link
Contributor

This teaches ClangImporter to respect the _Nonnull/_Nullable arguments on templated function parameters.

Previously Swift would only import a non-annotated function overload. Using an overload that has either _Nonnull or _Nullable would result in a compiler error. The non-annotated overload would get imported with incorrect nullability: Swift would always assume non-null pointers, which was inconsistent with non-templated function parameters, which are mapped to implicitly unwrapped optionals.

With this change all three possible overloads are imported, and all of them get the correct nullability in Swift.

rdar://151939344

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

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 looks great!

I'm wondering what happens when a function returns a pointer type, whether it be nullable, non-null, or unannotated. That said, importing return types seem to be handled separately, so maybe that's beyond the scope of this patch.

@@ -2731,8 +2749,6 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
}

bool knownNonNull = !nonNullArgs.empty() && nonNullArgs[index];
// Specialized templates need to match the args/result exactly.
knownNonNull |= clangDecl->isFunctionTemplateSpecialization();
Copy link
Contributor

Choose a reason for hiding this comment

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

With this removed, you might have fixed some of the crashes for the commented-out cases in template-instantiation-irgen.swift, e.g.,

// FIXME: this crashes because this round-trips to UnsafeMutablePointer<FRT?>
// func takesMutPtrToFRT(x: UnsafeMutablePointer<FRT>) { takesValue(x) }

Could you see if those are fixed by your patch?

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 great point, thanks! Let me uncomment some of these.

Copy link
Contributor

Choose a reason for hiding this comment

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

(related: rdar://142850017)

Copy link
Contributor

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

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

LG, thanks!

@egorzhdan egorzhdan force-pushed the egorzhdan/template-param-nullability branch from 111a028 to 16b1d5b Compare June 11, 2025 18:39
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/template-param-nullability branch 3 times, most recently from 0b7c2a9 to 74e0d79 Compare June 12, 2025 17:19
@egorzhdan egorzhdan force-pushed the egorzhdan/template-param-nullability branch from 74e0d79 to 496571b Compare June 12, 2025 17:24
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

…rectly

This teaches ClangImporter to respect the `_Nonnull`/`_Nullable` arguments on templated function parameters.

Previously Swift would only import a non-annotated function overload. Using an overload that has either  `_Nonnull` or `_Nullable` would result in a compiler error. The non-annotated overload would get imported with incorrect nullability: Swift would always assume non-null pointers, which was inconsistent with non-templated function parameters, which are mapped to implicitly unwrapped optionals.

With this change all three possible overloads are imported, and all of them get the correct nullability in Swift.

rdar://151939344
@egorzhdan egorzhdan force-pushed the egorzhdan/template-param-nullability branch from 496571b to 607dd4a Compare June 13, 2025 17:16
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test macOS

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test Linux

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

1 similar comment
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan merged commit f6e6172 into main Jun 17, 2025
3 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/template-param-nullability branch June 17, 2025 15:51
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