Skip to content

C++ Interop: improve skipping already imported struct members #38439

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 19, 2021

Conversation

egorzhdan
Copy link
Contributor

This fixes the issue that prevents std::string::size_type from being imported into Swift: size_type is imported before its parent struct, and we're skipping it when importing the struct afterwards.

This is caused by an out-of-line decl for std::string::npos:

template<class _CharT, class _Traits, class _Allocator>
_LIBCPP_FUNC_VIS
const typename basic_string<_CharT, _Traits, _Allocator>::size_type
               basic_string<_CharT, _Traits, _Allocator>::npos;

When importing npos, we first import size_type, which triggers the issue.

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Jul 16, 2021
@egorzhdan egorzhdan requested a review from zoecarver July 16, 2021 18:02
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

This LGTM. I think you have the infra setup locally to import the standard library. I assume you've already done this, but can you please make sure we are still able to import the standard library without crashing after this patch is applied? (We should really have this be part of the CI/test suite now...)

// CHECK: extension NS1 {
// CHECK: struct __CxxTemplateInstN3NS14DeclIiEE {
// CHECK: typealias MyInt = Int32
// CHECK: var fwd: NS1.__CxxTemplateInstN3NS115ForwardDeclaredIiEE
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we check these class template specializations too? Might as well, these module interface tests are pretty easy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Amusingly, we would need #37695 for that :) Currently the lookup logic gets confused & only the contents of the first NS1 redecl end up in the module interface. Applying #37695 locally solves this (all the contents get included into the module interface), but I guess it would be better to get this PR merged first, so it's too early to add a // CHECK for those decls now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good. Maybe if you think of it, you could fix this as a follow up. But it's not super important. The test is good as is.

@@ -3578,6 +3573,12 @@ namespace {
continue;
}

// If we've already added this decl to the resulting struct, skip it so
// we don't add the same member twice.
if (!allMembers.insert(member).second) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't want to do this before L3570/3565 when we try to import the decl?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm yes, that makes sense. Fixed.

@zoecarver
Copy link
Contributor

Just so we're on the same page here, the problem is:

  1. We import size_type
  2. We import its parent basic_string
  3. We already cached size_type, so we don't add it as a parent to basic_string?


namespace NS1 {

template <typename T>
Copy link
Contributor

Choose a reason for hiding this comment

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

Come to think of it, I bet you could get this behavior without using a template. Can you please try that :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm that's weird, but it doesn't seem reproducible without a template. I'll need a bit more time to figure out why.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is interesting. It would be good to understand why we need this to be a template. But in the mean time, I don't think this patch should be blocked on that.

This fixes the issue that prevents `std::string::size_type` from being imported into Swift: `size_type` is imported before its parent struct, and  we're skipping it when importing the struct afterwards.

This is caused by an out-of-line decl for `std::string::npos`:
```cpp
template<class _CharT, class _Traits, class _Allocator>
_LIBCPP_FUNC_VIS
const typename basic_string<_CharT, _Traits, _Allocator>::size_type
               basic_string<_CharT, _Traits, _Allocator>::npos;

```
When importing `npos`, we first import `size_type`, which triggers the issue.
@egorzhdan
Copy link
Contributor Author

I think you have the infra setup locally to import the standard library. I assume you've already done this, but can you please make sure we are still able to import the standard library without crashing after this patch is applied?

Yeap, I've tried it and it works for me, definitely doesn't crash.

We should really have this be part of the CI/test suite now

Absolutely, I'm waiting for #37806 to land, and after that I'll submit a patch with a test for stdlib import.
#37806 will allow us to test this without having hardcoded paths to the stdlib.

Just so we're on the same page here, the problem is

Not quite (my bad, I should've described this in more detail)

  1. We begin importing basic_string
  2. We import its decl context, which is a namespace
  3. The namespace contains the npos decl, so we import that, along with it's type size_type
  4. When collecting the members of basic_string, we see that we've already imported size_type so we skip it.

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

🛳 it

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan merged commit 60a195e into swiftlang:main Jul 19, 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.

2 participants