Skip to content
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

std::vector<std::string> fails to conform to CxxRandomAccessCollection #67410

Closed
Tracked by #65808
hyp opened this issue Jul 19, 2023 · 3 comments · Fixed by #67482
Closed
Tracked by #65808

std::vector<std::string> fails to conform to CxxRandomAccessCollection #67410

hyp opened this issue Jul 19, 2023 · 3 comments · Fixed by #67482
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ c++ to swift Feature → c++ interop: c++ to swift swift 5.9 triage needed This issue needs more specific labels

Comments

@hyp
Copy link
Contributor

hyp commented Jul 19, 2023

As reported here: https://forums.swift.org/t/c-interop-for-in-loop-requires-std-1-vector-element-to-conform-to-sequence/66232

Reproducer:
https://github.com/Sajjon/CxxRandomAccessCollectionNotWorking

@hyp hyp added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 19, 2023
@hyp hyp added the c++ interop Feature: Interoperability with C++ label Jul 19, 2023
@hyp
Copy link
Contributor Author

hyp commented Jul 19, 2023

over to @egorzhdan

@Sajjon
Copy link
Contributor

Sajjon commented Jul 20, 2023

Btw it works to call a method returning std::string so using string alone is not an issue, and std::vector<int> is not an issue, just the combination of vector<string> fails to compile.

@moretromain
Copy link

moretromain commented Jul 20, 2023

I'm facing the same issue, not only with std::string, but even with builtin char/int types:

On the C++ side, I do a few aliases:

using CharArray     = std::vector<char>;
using UCharArray    = std::vector<unsigned char>;
using I8Array       = std::vector<int8_t>;
using U8Array       = std::vector<uint8_t>;
using I16Array      = std::vector<int16_t>;
using U16Array      = std::vector<uint16_t>;
using I32Array      = std::vector<int32_t>;
using U32Array      = std::vector<uint32_t>;
using I64Array      = std::vector<int64_t>;
using U64Array      = std::vector<uint64_t>;

On the Swift side, for each of those, a simple function:

    public static func i8ArrayTest(_ data: I8Array) -> [Int8] {
        let ret = Array<Int8>(data)
        return ret
    }

The results are interesting:

  • int, int32_t and uint32_t are working as expected
  • all other types are not working (error: doesn't conform to Sequence or CxxConvertibleToCollection in the Array initialization)

Furthermore, if I keep only the two that work (int32_t and uint32_t), I get a link error a bit later, with multiply defined symbols:

<unknown>:0: error: multiple definitions of symbol '$sSo1soiySiSo3stdO3__1O0020___wrap_iter__udAAdDaV_AGtFTO'
<unknown>:0: error: multiple definitions of symbol '$sSo2eeoiySbSo3stdO3__1O0020___wrap_iter__udAAdDaV_AGtFTO'
<unknown>:0: error: multiple definitions of symbol '$sSo3stdO3__1O0020___wrap_iter__udAAdDaV9successorAFyF'
<unknown>:0: error: multiple definitions of symbol '$sSo3stdO3__1O0020___wrap_iter__udAAdDaV2peoiyyAFz_SitFZ'

I also tried this:

#pragma pack(push, 4)
struct FourBytes
{
    char x[4];
};
#pragma pack(pop)
using B4Array  = std::vector<FourBytes>;

Just to see if the issue was with a vector of element of sizeof(Element) != 4 (the multiply defined symbols could indicate that the generated code only cares about the size of an element, not its type), but I'm getting the same "doesn't conform" error on the Swift side, so the issue is really int/int32_t/uint32_t vs. anything else.

Swift 5.9 / Xcode 15 Beta 4, same issue when compiling from Xcode, or by invoking swiftc directly

@AnthonyLatsis AnthonyLatsis added swift 5.9 c++ to swift Feature → c++ interop: c++ to swift labels Jul 20, 2023
egorzhdan added a commit that referenced this issue Jul 24, 2023
This prevented `std::vector<std::string>` from being auto-conformed to `CxxRandomAccessCollection`.

If an iterator type is templated, and does not have an explicit instantiation via a typedef or a using-decl, its specialization will not have an owning Clang module. Make sure we treat it as a part of the Clang module that owns the template decl.

rdar://112762768 / resolves #67410
egorzhdan added a commit that referenced this issue Jul 24, 2023
This prevented `std::vector<std::string>` from being auto-conformed to `CxxRandomAccessCollection`.

If an iterator type is templated, and does not have an explicit instantiation via a typedef or a using-decl, its specialization will not have an owning Clang module. Make sure we treat it as a part of the Clang module that owns the template decl.

rdar://112762768 / resolves #67410
(cherry picked from commit af014c0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ c++ to swift Feature → c++ interop: c++ to swift swift 5.9 triage needed This issue needs more specific labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants