Skip to content

GenericSpecializer: don't pre-specialize C++ reference type for AnyObject #83090

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

Conversation

eeckstein
Copy link
Contributor

Reference counting is not compatible.
Fixes a runtime crash if a C++ reference type is put into an Array.

#83082
rdar://155919841

@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci apple silicon benchmark

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.

Thank you! Just one comment on the tests.


@inline(never)
func go() {
let x: RefType = RefType.makeRefType()
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: can we test this in test/Interop/Cxx/foreign-reference/reference-counted.swift? It already has some testing for arrays of foreign reference types (ReferenceCountedTestSuite.test("Global array")).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed offline, I moved the test to test/Interop/Cxx/foreign-reference

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Jul 16, 2025
@eeckstein eeckstein force-pushed the fix-pre-specialization branch from d3ad3fa to 40e0360 Compare July 16, 2025 10:55
@eeckstein
Copy link
Contributor Author

@swift-ci test

@@ -3163,7 +3163,8 @@ static bool usePrespecialized(

if (lowered.isBuiltinBridgeObject() && layout->isBridgeObject()) {
newSubs.push_back(genericParam->getASTContext().TheBridgeObjectType);
} else if (lowered.hasRetainablePointerRepresentation()) {
} else if (lowered.hasRetainablePointerRepresentation() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn’t this mean that hasRetainablePointerRepresentation() should be changed? I’m pretty sure other callers also assume AnyObject compatibility when this returns true.

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'm not sure. hasRetainablePointerRepresentation is used in the type checker but arc for imported c++ classes is mostly about code generation...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@slavapestov Actually, I agree. It's safer to change hasRetainablePointerRepresentation. I also added a comment.

@eeckstein
Copy link
Contributor Author

@swift-ci smoke test macos

…ject

Reference counting is not compatible.
Fixes a runtime crash if a C++ reference type is put into an Array.

swiftlang#83082
rdar://155919841
@eeckstein eeckstein force-pushed the fix-pre-specialization branch from 40e0360 to f962d76 Compare July 16, 2025 19:30
@eeckstein
Copy link
Contributor Author

@swift-ci smoke test

Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

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

Thanks!

@eeckstein eeckstein merged commit ef798d5 into swiftlang:main Jul 17, 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.

3 participants