Skip to content

Cxx to Swift interop - issue with Array and Int type - "type cannot be used in a Swift generic context" #63452

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

Closed
Tracked by #65808
litewrap opened this issue Feb 6, 2023 · 2 comments · Fixed by #67241
Assignees
Labels
Array Area → standard library: The `Array` type bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ compiler The Swift compiler itself standard library Area: Standard library umbrella

Comments

@litewrap
Copy link

litewrap commented Feb 6, 2023

Description

Issue when working with Swift Array from C++.
"type cannot be used in a Swift generic context"

Steps to reproduce

// Swift

@_expose(Cxx, "makeArrayInt")
public func makeArrayInt() -> [Int] {
    return [1, 2, 3, 4, 5]
}
// C++

auto result = MySwiftLib::makeArrayInt();
./MySwiftLib-Swift.h:785:1: error: static_assert failed due to requirement 'swift::isUsableInGenericContext<long>' "type cannot be used in a Swift generic context"
static_assert(swift::isUsableInGenericContext<T_0_0>, "type cannot be used in a Swift generic context");
^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./MySwiftLib-Swift.h:4064:33: note: in instantiation of template class 'Swift::Array<long>' requested here
inline Swift::Array<swift::Int> makeArrayInt() noexcept SWIFT_WARN_UNUSED_RESULT {
                                ^
./MySwiftLib-Swift.h:1031:1: error: static_assert failed due to requirement 'swift::isUsableInGenericContext<long>' "type cannot be used in a Swift generic context"
static_assert(swift::isUsableInGenericContext<T_0_0>, "type cannot be used in a Swift generic context");
^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./MySwiftLib-Swift.h:4065:24: note: in instantiation of template class 'Swift::_impl::_impl_Array<long>' requested here
  return Swift::_impl::_impl_Array<swift::Int>::returnNewValue([&](char * _Nonnull result) {
                       

Compiling C++ with -std=c++20

In file included from main.cpp:7:
./MySwiftLib-Swift.h:4064:15: error: constraints not satisfied for class template 'Array' [with T_0_0 = long]
inline Swift::Array<swift::Int> makeArrayInt() noexcept SWIFT_WARN_UNUSED_RESULT {
              ^~~~~~~~~~~~~~~~~
./MySwiftLib-Swift.h:780:10: note: because 'swift::isUsableInGenericContext<long>' evaluated to false
requires swift::isUsableInGenericContext<T_0_0>
         ^
./MySwiftLib-Swift.h:4065:24: error: constraints not satisfied for class template '_impl_Array' [with T_0_0 = long]
  return Swift::_impl::_impl_Array<swift::Int>::returnNewValue([&](char * _Nonnull result) {
                       ^~~~~~~~~~~~~~~~~~~~~~~
./MySwiftLib-Swift.h:1026:10: note: because 'swift::isUsableInGenericContext<long>' evaluated to false
requires swift::isUsableInGenericContext<T_0_0>
         ^

Note that using Int64 instead of Int in the Swift code will work calling from C++.

@litewrap litewrap added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Feb 6, 2023
@AnthonyLatsis
Copy link
Collaborator

@egorzhdan Should this be qualified as a bug or feature request?

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Feb 6, 2023
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself standard library Area: Standard library umbrella Array Area → standard library: The `Array` type and removed triage needed This issue needs more specific labels labels Feb 6, 2023
@hyp
Copy link
Contributor

hyp commented Jul 12, 2023

Potentially fixed in - #67241

hyp added a commit to hyp/swift that referenced this issue Jul 12, 2023
hyp added a commit to hyp/swift that referenced this issue Jul 13, 2023
hyp added a commit to hyp/swift that referenced this issue Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Array Area → standard library: The `Array` type bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ compiler The Swift compiler itself standard library Area: Standard library umbrella
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants