<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/138927>138927</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [libc++] macos: when -mmacosx-version-min < 11, std::latch compiles, although __cpp_lib_latch is undefined
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          asakovets
      </td>
    </tr>
</table>

<pre>
    This issue was discovered using Apple toolchain, but its roots is in the upstream.

Steps to reproduce:

Consider a code snippet:
```cpp
#include <iostream>
#include <latch>
#include <version>

int main()
{
#if !__cpp_lib_latch
        std::latch this_must_not_compile {42};
        std::cout << "std::latch compiles, although __cpp_lib_latch is undefined\n";
#endif
}
```

Using Apple clang:
```bash
% xcrun clang++ --version
Apple clang version 16.0.0 (clang-1600.0.26.4)
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
% xcrun clang++ -std=c++20 -mmacosx-version-min=10.15 latch.cpp
% ./a.out
std::latch compiles, although __cpp_lib_latch is undefined
```

Using upstream clang:
Reproducing the issue with upstream clang is not as straightforward. We must tell the compiler that vendor availability annotations are enabled (even if they are not). Passing `-D_LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS=1` does not work. The easiest solution, though a little messy, is to modify `${llvm_prefix}/include/c++/v1/__config_site` file and set `#define _LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS 1`. This should suffice to reproduce the issue:

```bash
% /opt/homebrew/Cellar/llvm/20.1.4/bin/clang++ --version 
Homebrew clang version 20.1.4
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/llvm/20.1.4/bin
Configuration file: /opt/homebrew/etc/clang/arm64-apple-darwin24.cfg
% /opt/homebrew/Cellar/llvm/20.1.4/bin/clang++ -std=c++20 -mmacosx-version-min=10.15 latch.cpp
% ./a.out
std::latch compiles, although __cpp_lib_latch is undefined
```

-------------------------------------------------------------------------------------------------------------

My little bits of investigation:

`__cpp_lib_latch` is defined when both `_LIBCPP_HAS_THREADS` and `_LIBCPP_AVAILABILITY_HAS_SYNC` are true:

https://github.com/llvm/llvm-project/blob/0db040576d4ccb313fc58a90e1b4149f7589cc8c/libcxx/include/version#L438-L440

However, the definition of `std::latch` is only protected by `_LIBCPP_HAS_THREADS` macro:

https://github.com/llvm/llvm-project/blob/32752913b12103431dc392242c3c808afb70bd15/libcxx/include/latch#L48-L50

When compiling for MacOS < 11, if availability annotations are enabled, i.e. `_LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS = 1`, then `_LIBCPP_INTRODUCED_IN_LLVM_11` is defined as 0:

https://github.com/llvm/llvm-project/blob/960221623f54b87c0ce786d551111573810c8ac8/libcxx/include/__configuration/availability.h#L222-L226

This macro in turn is used to define `_LIBCPP_AVAILABILITY_HAS_SYNC`:

https://github.com/llvm/llvm-project/blob/960221623f54b87c0ce786d551111573810c8ac8/libcxx/include/__configuration/availability.h#L308

As a result, when `-mmacosx-version-min < 11` and vendor availability annotations are enabled, then `_LIBCPP_HAS_THREADS = 1` and `_LIBCPP_AVAILABILITY_HAS_SYNC = 0`. This makes `__cpp_lib_latch` undefined, but std::latch compiles nevertheless.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUV11v2zgW_TX0y4UFivqyH_wg2xUawJMUjaez8yRQFGVxS5MCSTnJv1-Qlhs368y26GCxawQIJFJX955z7rkUtVYcFOcrlK1Rtp3R0fXarKilX_WJOztrdPuy2vfCgrB25PBELbTCMn3ihrcwWqEOUA6D5OC0lqynQiGygWZ0IJwFo7Xzz4JQ4HoO42Cd4fQYIVwiXD46PlhwGgwfjG5HxlFSnpc2WlnRcgMUmG45WCWGgbtpPcfnPzYM_pIkQjE5thxQshH6_A6UfP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