Skip to content

🍒 [6.2] [cxx-interop] Avoid unchecked recursion when importing C++ classes with circular inheritance #81212

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

Conversation

j-hui
Copy link
Contributor

@j-hui j-hui commented Apr 30, 2025

Explanation: This patch adds a check to guard against unbounded recursion when importing class templates that inherit from a specialization of themselves, a pattern appears in libc++. The unbounded recursion only happens with symbolic imports enabled, which is why this was not caught before.

Issue: rdar://148026461
Risk: low
Testing: added a new test case
Original PRs: #81188
Reviewer: @egorzhdan

…th circular inheritance

It is possible for a C++ class template to inherit from a specialization
of itself. Normally, these are imported to Swift as separate (unrelated)
types, but when symbolic import is enabled, unspecialized templates are
imported in place of their specializations, leading to circularly
inheriting classes to seemingly inherit from themselves.

This patch adds a check to guard against the most common case of
circular inheritance, when a class template directly inherits from
itself. This pattern appears in a recent version of libc++,
necessitating this patch. However, the solution here is imperfect as it
does not handle more complex/contrived circular inheritance patterns.

This patch also adds a test case exercising this pattern. The
-index-store-path flag causes swift-frontend to index the C++ module
with symbolic import enabled, without the fix in this patch, that test
triggers an assertion failure due to the circular reference (and can
infinitely recurse in the StorageVisitor when assertions are disabled).

rdar://148026461
(cherry picked from commit 1f2107f)
@j-hui j-hui requested a review from a team as a code owner April 30, 2025 20:45
@j-hui
Copy link
Contributor Author

j-hui commented Apr 30, 2025

@swift-ci please test

@j-hui
Copy link
Contributor Author

j-hui commented May 1, 2025

@swift-ci please test macos

@j-hui j-hui merged commit b7812ba into swiftlang:release/6.2 May 2, 2025
5 checks passed
@j-hui j-hui deleted the base-6.2/fix-recursive-inheritance branch May 2, 2025 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants