Skip to content

Commit 2cacb78

Browse files
[libc++] Make _Atomic(T) directly use an alias template
Previously, libc++'s `_Atomic(T)` directly expanded to `::std::atomic<T>`, while the standard wording specified that it uses an alias template. The divergence was observable and made libc++ accept some invalid code like `struct _Atomic(T) t;`. This patch makes libc++'s `_Atomic(T)` directly use an internal alias template to eliminate such divergence.
1 parent ac68dd5 commit 2cacb78

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

libcxx/include/stdatomic.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ using std::atomic_signal_fence // see below
135135
# undef _Atomic
136136
# endif
137137

138-
# define _Atomic(_Tp) ::std::atomic<_Tp>
138+
_LIBCPP_BEGIN_NAMESPACE_STD
139+
template <class _Tp>
140+
using __libcpp_atomic_alias _LIBCPP_NODEBUG = atomic<_Tp>;
141+
_LIBCPP_END_NAMESPACE_STD
142+
143+
# define _Atomic(_Tp) ::std::__libcpp_atomic_alias<_Tp>
139144

140145
using std::memory_order _LIBCPP_USING_IF_EXISTS;
141146
using std::memory_order_relaxed _LIBCPP_USING_IF_EXISTS;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++23
10+
// UNSUPPORTED: no-threads
11+
12+
// <stdatomic.h>
13+
14+
// template<class T>
15+
// using std-atomic = std::atomic<T>; // exposition only
16+
//
17+
// #define _Atomic(T) std-atomic<T>
18+
19+
// Verify that _Atomic(T) directly uses an alias template but not the std::atomic class template.
20+
// See also https://llvm.org/PR168579.
21+
22+
#include <stdatomic.h>
23+
24+
struct _Atomic(int) x;
25+
// expected-error-re@-1{{error: alias template '{{.*}}' cannot be referenced with the 'struct' specifier}}

0 commit comments

Comments
 (0)