Skip to content

Conversation

@susmonteiro
Copy link
Contributor

The annotationOnly flag was originally introduced to control for which types we would infer escapability, and for which we would rely solely on annotations. However, due to backward compatibility issues, this flag is currently always set to true. Therefore, this patch removes the annotationOnly flag and makes the compiler infer escapability from bases and fields only for simple records, such as aggregates.

@susmonteiro susmonteiro added the c++ interop Feature: Interoperability with C++ label Nov 21, 2025
@susmonteiro susmonteiro changed the title Susmonteiro/remove annotationonly flag [cxx-interop] Remove annotationOnly flag from Escapability request Nov 21, 2025
@susmonteiro
Copy link
Contributor Author

Note: this PR is a follow-up to #85485, which hasn't been merged yet. Only the last commit 6279d81 is a new addition

@susmonteiro
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.

I reviewed only the changes in 6279d81 and am fine with them as is, but I'm wondering what the plan is for having this analysis actually be accurate and consider structs with pointer fields as non-escaping.

type->isMemberPointerType() || type->isReferenceType()) {
// pointer and reference types are currently imported as unknown
// (importing them as non-escapable broke backward compatibility)
hasUnknown = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with this for now, to preserve the current behavior, but assuming pointers have unknown escapability seems like it could be problematic. My understanding is that, semantically, pointers should be treated as non-escapable.

Do we have a plan for how to correctly analyze these types while maintaining backwards compat?

An idea I had was making the analysis depend on whether strict memory safety is turned on or off, essentially using/abusing it as a feature flag. But that may not be a good idea for reasons that I haven't fully thought through yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a plan for how to correctly analyze these types while maintaining backwards compat?

I do not think we can maintain backward compatibility and be precise at the same time. And we do not want strict memory safety to influence the behavior of the code. Making a type suddenly non-escapable when strict memory safety is on would result in some code not compiling. Especially when one is using an API that is owned by someone else this compilation error might be impossible to resolve.

@Xazax-hun
Copy link
Contributor

but I'm wondering what the plan is for having this analysis actually be accurate and consider structs with pointer fields as non-escaping.

We would not be able to do this any time soon. I tried this and it breaks a lot of code, including code in the standard library. Unfortunately, however, non-escapable types are still not first class citizens in Swift. They are viral, so suddenly even some Swift types would become non-escapable, and many protocols have not been generalized to work with non-escapable types. So the very least we need to wait until non-escapable gets some improvements, and even after that the switch would be really expensive so we will need to demonstrate a lot of value to justify this.

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.

Overall looks good. Feel free to merge once the nits are resolved.

@susmonteiro susmonteiro force-pushed the susmonteiro/remove-annotationonly-flag branch from 6279d81 to 146726b Compare November 24, 2025 18:08
@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test

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.

Looks great, thanks!

@susmonteiro susmonteiro force-pushed the susmonteiro/remove-annotationonly-flag branch from 146726b to 27e0ae8 Compare November 25, 2025 11:36
@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test

Comment on lines +5559 to +5577
// We only infer escapability for simple types, such as aggregates and
// RecordDecls that are not CxxRecordDecls. For more complex
// CxxRecordDecls, we rely solely on escapability annotations.
Copy link
Contributor

Choose a reason for hiding this comment

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

In C++ language mode, Clang makes all RecordDecls into CXXRecordDecls. Could this cause source compatibility issues with Obj-C interop? e.g. if someone enables C++ interop for the first time, would some types have different escapability?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd expect those record decls to be aggregates in C++, so we should handle them the same way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense, thanks!

@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test macos

@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test

@susmonteiro susmonteiro force-pushed the susmonteiro/remove-annotationonly-flag branch from 27e0ae8 to b06631e Compare December 2, 2025 09:27
@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test

@susmonteiro susmonteiro enabled auto-merge December 2, 2025 14:28
@susmonteiro susmonteiro merged commit 9e216ed into swiftlang:main Dec 2, 2025
3 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