Skip to content

[C++ interop] swiftc not compatible with latest libc++ headers #85820

@junov-google

Description

@junov-google

Description

There was a recent refactor in libc++ where the _LIBCPP_COMPRESSED_PAIR macro was updated to use the [[__no_unique_address__]] attribute, which it seems is not handled correctly by the swift compiler.

This change is documented in the release notes here: https://releases.llvm.org/20.1.0/projects/libcxx/docs/ReleaseNotes.html#id4 (4th bullet point in the Improvements section)

Attempting to instantiate template classes that use _LIBCPP_COMPRESSED_PAIR internally, such as std::vector, leads to compiler errors due to missing member initializers in constructors for members that should not require initialization because they have zero storage.

For classes that do compile correctly despite their use of _LIBCPP_COMPRESSED_PAIR, I suspect there may be other problems down the road due to ABI incompatibilities caused by inconsistent handling of [[__no_unique_address__]] between clang and swiftc.

Reproduction

C++ Header:

#include <vector>
std::vector<int> GetOneTwoThree();

C++ Implementation:

#include "get_one_two_three.h"
std::vector<int> GetOneTwoThree() {
  return {1, 2, 3};
}

Module map:

module GetOneTwoThree {
  header "get_one_two_three.h"
  export*
}

Swift file:

import GetOneTwoThree
public func testingOneTwoThree() {
  let array = GetOneTwoThree()
  print(array.size())
}

Download a recent version of stdc++ (20.0 or later)

Use the following swiftc compiler flags, replacing all occurrences of (...) with the path to your local copy of stdc++:

-cxx-interoperability-mode=default -Xcc -nostdinc++ -Xcc -isystem/(...)/libc++/src/include
-Xcc -isystem/(...)/libc++abi/src/include -Xcc -I/(...)/libc++ -explicit-module-build -Xcc -fmodule-map-file=get_one_two_three.modulemap -Xcc -std=c++20

Note, bug is also reproducible in -std=c++23 mode.

Compile error:

libc++/src/include/__split_buffer:71:55: constructor for 'std::__split_buffer_pointer_layout<std::__split_buffer<int, std::allocator<int> &, std::__split_buffer_pointer_layout>, int, std::allocator<int> &>' must explicitly initialize the reference member '__alloc_'

Expected behavior

This should compile without errors

Environment

Apple Swift version 6.3-dev (LLVM bff1370bd79c983, Swift 57cf4ce)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.c++ interopFeature: Interoperability with C++

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions