Skip to content

C++ struct holding std::vector<std::unique_ptr<int>> can't be used in Swift: get obscure template errors instead of being imported as ~Copyable #83151

@maddyadams

Description

@maddyadams

Description

No response

Reproduction

#include <memory>
#include <vector>

struct UniqueIntVector {
    UniqueIntVector() = default;
private:
    std::vector<std::unique_ptr<int>> _x;
};
let a = UniqueIntVector()
/Users/maddyadams/Desktop/Xcode/VectorOfNonCopyableRepro/Sources/CxxTarget/include/CppImpl.hpp:11:2: note: in module 'std_memory' imported from /Users/maddyadams/Desktop/Xcode/VectorOfNonCopyableRepro/Sources/CxxTarget/include/CppImpl.hpp:11:
#include <memory>
 ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/memory:940:2: note: in module 'std_private_memory_allocator' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/memory:940:
#include <__memory/allocator.h>
 ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/__memory/allocator.h:15:2: note: in module 'std_private_memory_allocate_at_least' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/__memory/allocator.h:15:
#include <__memory/allocate_at_least.h>
 ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/__memory/allocate_at_least.h:13:2: note: in module 'std_private_memory_allocator_traits' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/__memory/allocate_at_least.h:13:
#include <__memory/allocator_traits.h>
 ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/__memory/allocator_traits.h:328:5: error: no matching function for call to '__construct_at'
    std::__construct_at(__p, std::forward<_Args>(__args)...);
    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/__memory/uninitialized_algorithms.h:552:31: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<std::unique_ptr<int>>>::construct<std::unique_ptr<int>, std::unique_ptr<int> &, void, 0>' requested here
    allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__first2), *__first1);
                              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/__memory/uninitialized_algorithms.h:595:33: note: in instantiation of function template specialization 'std::__uninitialized_allocator_copy_impl<std::allocator<std::unique_ptr<int>>, std::unique_ptr<int> *, std::unique_ptr<int> *, std::unique_ptr<int> *>' requested here
  auto __result          = std::__uninitialized_allocator_copy_impl(
                                ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/vector:1158:22: note: in instantiation of function template specialization 'std::__uninitialized_allocator_copy<std::allocator<std::unique_ptr<int>>, std::unique_ptr<int> *, std::unique_ptr<int> *, std::unique_ptr<int> *>' requested here
  __tx.__pos_ = std::__uninitialized_allocator_copy(__alloc(), __first, __last, __tx.__pos_);
                     ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/vector:800:7: note: in instantiation of function template specialization 'std::vector<std::unique_ptr<int>>::__construct_at_end<std::unique_ptr<int> *, std::unique_ptr<int> *>' requested here
      __construct_at_end(__first, __last, __n);
      ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/vector:1238:3: note: in instantiation of function template specialization 'std::vector<std::unique_ptr<int>>::__init_with_size<std::unique_ptr<int> *, std::unique_ptr<int> *>' requested here
  __init_with_size(__x.__begin_, __x.__end_, __x.size());
  ^
/Users/maddyadams/Desktop/Xcode/VectorOfNonCopyableRepro/Sources/CxxTarget/include/CppImpl.hpp:14:8: note: in instantiation of member function 'std::vector<std::unique_ptr<int>>::vector' requested here
struct UniqueIntVector {
       ^
<unknown>:0: note: in implicit copy constructor for 'UniqueIntVector' first required here
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/__memory/construct_at.h:47:58: note: candidate template ignored: substitution failure [with _Tp = std::unique_ptr<int>, _Args = <std::unique_ptr<int> &>]: call to implicitly-deleted copy constructor of 'std::unique_ptr<int>'
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* __construct_at(_Tp* __location, _Args&&... __args) {
                                                         ^

Expected behavior

I expected that UniqueIntVector would be imported as a ~Copyable type in Swift.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

rdar://151870709

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions