Skip to content

[cxx-interop] Add class template tests #33420

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
Oct 26, 2020

Conversation

hlopko
Copy link
Contributor

@hlopko hlopko commented Aug 12, 2020

In this PR we add tests testing more complicated template scenarios for #32950. Specifically:

  • class template with variadic parameter
  • class template with non-type parameter
  • class template with template template parameter

This PR depends on #32950.

@hlopko hlopko requested a review from gribozavr August 12, 2020 11:52
@hlopko
Copy link
Contributor Author

hlopko commented Aug 13, 2020

(you can use hlopko/swift@typedefs...hlopko:add_template_tests to see the diffbase on typedefs PR)

@hlopko hlopko marked this pull request as ready for review August 13, 2020 13:44
@hlopko hlopko added the c++ interop Feature: Interoperability with C++ label Aug 13, 2020
@hlopko
Copy link
Contributor Author

hlopko commented Aug 13, 2020

@swift-ci please test

1 similar comment
@hlopko
Copy link
Contributor Author

hlopko commented Aug 13, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f3dcdc2

@gribozavr
Copy link
Contributor

Could you rebase? I can open the diff view that you linked, but I can't leave comments there.

@scentini
Copy link
Contributor

scentini commented Sep 8, 2020

@gribozavr rebased, PTAL

@hlopko
Copy link
Contributor Author

hlopko commented Sep 18, 2020

@swift-ci please test

@hlopko
Copy link
Contributor Author

hlopko commented Sep 24, 2020

@swift-ci please test Windows platform

@hlopko
Copy link
Contributor Author

hlopko commented Sep 25, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 0931121

@hlopko hlopko changed the base branch from master to main September 25, 2020 07:55
@hlopko
Copy link
Contributor Author

hlopko commented Sep 25, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f97e01e

@hlopko
Copy link
Contributor Author

hlopko commented Sep 25, 2020

@swift-ci please test Windows platform

@hlopko
Copy link
Contributor Author

hlopko commented Sep 28, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 5c18ef4

@hlopko
Copy link
Contributor Author

hlopko commented Sep 29, 2020

@swift-ci please test

@hlopko
Copy link
Contributor Author

hlopko commented Sep 29, 2020

@swift-ci please test windows platform

@hlopko
Copy link
Contributor Author

hlopko commented Oct 1, 2020

Skipping class-template-variadic test on Windows, the culprit is https://bugs.swift.org/browse/SR-13129.

@hlopko
Copy link
Contributor Author

hlopko commented Oct 1, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Oct 1, 2020

Build failed
Swift Test Linux Platform
Git Sha - 30545bc

@swift-ci
Copy link
Contributor

swift-ci commented Oct 1, 2020

Build failed
Swift Test OS X Platform
Git Sha - 30545bc

@hlopko
Copy link
Contributor Author

hlopko commented Oct 1, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Oct 1, 2020

Build failed
Swift Test Linux Platform
Git Sha - e978d22

@swift-ci
Copy link
Contributor

swift-ci commented Oct 1, 2020

Build failed
Swift Test OS X Platform
Git Sha - e978d22

@hlopko
Copy link
Contributor Author

hlopko commented Oct 2, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Oct 2, 2020

Build failed
Swift Test OS X Platform
Git Sha - e978d22

…2950.

Specifically:

* class template with variadic parameter
* class template with non-type parameter
* class template with template template parameter
@hlopko hlopko force-pushed the add_template_tests branch from 3ec1658 to 96d51cc Compare October 7, 2020 09:53
hlopko added a commit to hlopko/swift that referenced this pull request Oct 7, 2020
… Swift.

Given a C++ header:

```c++
// C++ module `ClassTemplates`
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};
```

it is now possible to write in Swift:

```c++
import ClassTemplates

func x() -> MagicWrapper<MagicNumber> {
  return MagicWrapper<MagicNumber>()
}
```

This is achieved by importing C++ class templates as generic structs,
and then when Swift type checker calls applyGenericArguments we detect
when the generic struct is backed by the C++ class template and call
Clang to instantiate the template. In order to make it possible to put
class instantiations such as MagicWrapper<MagicNumber> into Swift
signatures, we have created a new XREF
(XREF_CLANG_TEMPLATE_INSTANTIATION) into serialisation and
deserialisation infrastructure.

Depends on swiftlang#33420.
Progress towards https://bugs.swift.org/browse/SR-13261.
@hlopko
Copy link
Contributor Author

hlopko commented Oct 7, 2020

@swift-ci please test

@hlopko
Copy link
Contributor Author

hlopko commented Oct 7, 2020

@swift-ci please test windows platform

@hlopko hlopko merged commit bfbeb90 into swiftlang:main Oct 26, 2020
@tbkka
Copy link
Contributor

tbkka commented Oct 26, 2020

The class-template-variadic.swift test is failing on both Linux and macOS CI.

@hlopko
Copy link
Contributor Author

hlopko commented Oct 26, 2020

Sad, I'll prepare a revert.

hlopko added a commit to hlopko/swift that referenced this pull request Oct 27, 2020
… Swift.

Given a C++ header:

```c++
// C++ module `ClassTemplates`
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};
```

it is now possible to write in Swift:

```c++
import ClassTemplates

func x() -> MagicWrapper<MagicNumber> {
  return MagicWrapper<MagicNumber>()
}
```

This is achieved by importing C++ class templates as generic structs,
and then when Swift type checker calls applyGenericArguments we detect
when the generic struct is backed by the C++ class template and call
Clang to instantiate the template. In order to make it possible to put
class instantiations such as MagicWrapper<MagicNumber> into Swift
signatures, we have created a new XREF
(XREF_CLANG_TEMPLATE_INSTANTIATION) into serialisation and
deserialisation infrastructure.

Depends on swiftlang#33420.
Progress towards https://bugs.swift.org/browse/SR-13261.
hlopko added a commit that referenced this pull request Jan 27, 2021
This PR makes it possible to instantiate C++ class templates from Swift. Given a C++ header:

```c++
// C++ module `ClassTemplates`
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};
```

it is now possible to write in Swift:

```swift
import ClassTemplates

func x() -> MagicWrapper<MagicNumber> {
  return MagicWrapper<MagicNumber>()
}
```

This is achieved by importing C++ class templates as generic structs, and then when Swift type checker calls `applyGenericArguments` we detect when the generic struct is backed by the C++ class template and call Clang to instantiate the template. In order to make it possible to put class instantiations such as `MagicWrapper<MagicNumber>` into Swift signatures, we have created a new field in `StructDecl` named `TemplateInstantiationType` where the typechecker stores the `BoundGenericType` which we serialize. Deserializer then notices that the `BoundGenericType` is actually a C++ class template and performs the instantiation logic.

Depends on #33420.
Progress towards https://bugs.swift.org/browse/SR-13261.
Fixes https://bugs.swift.org/browse/SR-13775.

Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
Co-authored-by: Rosica Dejanovska <rosica@google.com>
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.

5 participants