Skip to content

[cxx-interop] Support forward declared records inside other records. #34864

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
Feb 23, 2021

Conversation

zoecarver
Copy link
Contributor

The pattern:

    struct X { friend struct Y; }; struct Y {};

is fairly common. But before this commit, we would crash while attempting to add "Y" as a child of "X". This commit simply checks if the child record is a declaration or definition. If the former, it bails (and the "child" record will be imported where it's defined).

@zoecarver zoecarver added the c++ interop Feature: Interoperability with C++ label Nov 23, 2020
@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test

if (auto record = dyn_cast<clang::RecordDecl>(nd)) {
// If this record only being declared inside of "decl" then simply
// ignore it because it's a forward declaration.
if (record->getDefinition() != record)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a test for forward declaration without definition?

Are you only interested in skipping friend declarations? If so we could make the check more specific.

Will your change do the right thing (which I guess is importing the definition) for this snippet?

struct Foo {
  struct Bar;
};

struct Foo::Bar {
  int a;
};

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'll keep the current test and add this one. I think it's beneficial to have both because the friend declaration isn't scoped.

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 tried adding that test but it, unfortunately, requires #34869. Once that lands, I'll update this patch with your suggested test.

@zoecarver
Copy link
Contributor Author

After rebasing this is now a test-only change.

@zoecarver
Copy link
Contributor Author

This is now crashing in a different place. #35085 should fix the new crash, so I'm just going to wait for that to land.


struct ForwardDeclaresFriend {
friend struct ForwardDeclaredFriend;
friend void takesFriend(struct ForwardDeclaredFriend f);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
friend void takesFriend(struct ForwardDeclaredFriend f);
friend void takesFriend(ForwardDeclaredFriend f);

Not needed in C++.

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 know. This was part of the reproducer, IIRC it didn't crash without it. I can't verify this now that it no longer crashes, but I think we should keep it.

…rds.

The pattern:

    struct X { friend struct Y; }; struct Y {};

is fairly common. But before this commit we would crash while attempting
to add "Y" as a child of "X". This commit simply checks if the child
record is a declaration or definition. If the former, it bails (and the
"child" record will be imported where it's defined).
@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test macOS.

@zoecarver
Copy link
Contributor Author

@swift-ci please test Windows.

@zoecarver
Copy link
Contributor Author

Another Java error.

@swift-ci please test Windows.

@zoecarver
Copy link
Contributor Author

The windows test failed because of an unrelated error. I'm going to go ahead and land this because it's just a NFC and it passed on the windows CI (even though another, unrelated test failed).

@zoecarver zoecarver merged commit 43c7ad7 into swiftlang:main Feb 23, 2021
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