252 lines · cpp
1// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++98 -pedantic-errors -verify=expected,cxx98 %s2// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++11 -pedantic-errors -verify=expected %s3// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++14 -pedantic-errors -verify=expected %s4// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++17 -pedantic-errors -verify=expected %s5// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++20 -pedantic-errors -verify=expected,since-cxx20 %s6// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -pedantic-errors -verify=expected,since-cxx20,since-cxx23 %s7// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++2c -pedantic-errors -verify=expected,since-cxx20,since-cxx23,since-cxx26 %s8 9#if __cplusplus == 199711L10#define static_assert(...) __extension__ _Static_assert(__VA_ARGS__)11// cxx98-error@-1 {{variadic macros are a C99 feature}}12#endif13 14#if __cplusplus == 199711L15#define __enable_constant_folding(x) (__builtin_constant_p(x) ? (x) : (x))16#else17#define __enable_constant_folding18#endif19 20namespace std {21#if __cplusplus >= 202002L22 struct strong_ordering {23 int n;24 constexpr operator int() const { return n; }25 static const strong_ordering less, equal, greater;26 };27 constexpr strong_ordering strong_ordering::less{-1},28 strong_ordering::equal{0}, strong_ordering::greater{1};29#endif30} // namespace std31 32namespace cwg2707 { // cwg2707: 2033 34#if __cplusplus >= 202002L35 36template <class T, unsigned N> struct A { // #cwg2707-A37 T value[N];38};39 40template <typename... T>41A(T...) -> A<int, sizeof...(T)> requires (sizeof...(T) == 2); // #cwg2707-guide-A42 43// Brace elision is not allowed for synthesized CTAD guides if the array size44// is value-dependent.45// So this should pick up our explicit deduction guide.46A a = {1, 2};47 48A b = {3, 4, 5};49// since-cxx20-error@-1 {{no viable constructor or deduction guide}}50// since-cxx20-note@#cwg2707-A {{candidate function template not viable}}51// since-cxx20-note@#cwg2707-A {{implicit deduction guide}}52// since-cxx20-note@#cwg2707-guide-A {{constraints not satisfied}}53// since-cxx20-note@#cwg2707-guide-A {{because 'sizeof...(T) == 2' (3 == 2) evaluated to false}}54// since-cxx20-note@#cwg2707-A {{candidate function template not viable}}55// since-cxx20-note@#cwg2707-A {{implicit deduction guide}}56 57#endif58 59} // namespace cwg270760 61namespace cwg2718 { // cwg2718: 2.762struct B {};63struct D;64 65void f(B b) {66 static_cast<D&>(b);67 // expected-error@-1 {{non-const lvalue reference to type 'D' cannot bind to a value of unrelated type 'B'}}68}69 70struct D : B {};71} // namespace cwg271872 73namespace cwg2749 { // cwg2749: 2074 75extern int x[2];76struct Y {77 int i;78 int j;79};80extern Y y[2];81 82static_assert(__enable_constant_folding(static_cast<void*>(&x[0]) < static_cast<void*>(&x[1])), "");83static_assert(__enable_constant_folding(static_cast<void*>(&y[0].i) < static_cast<void*>(&y[0].j)), "");84static_assert(__enable_constant_folding(static_cast<void*>(&y[0].j) < static_cast<void*>(&y[1].i)), "");85 86#if __cplusplus >= 202002L87static_assert((static_cast<void*>(&x[0]) <=> static_cast<void*>(&x[1])) == std::strong_ordering::less);88static_assert((static_cast<void*>(&y[0].i) <=> static_cast<void*>(&y[0].j)) == std::strong_ordering::less);89static_assert((static_cast<void*>(&y[0].j) <=> static_cast<void*>(&y[1].i)) == std::strong_ordering::less);90#endif91 92} // namespace cwg274993 94namespace cwg2759 { // cwg2759: 1995#if __cplusplus >= 201103L96 97struct CStruct {98 int one;99 int two;100};101 102struct CEmptyStruct {};103struct CEmptyStruct2 {};104 105struct CStructNoUniqueAddress {106 int one;107 [[no_unique_address]] int two;108};109 110struct CStructNoUniqueAddress2 {111 int one;112 [[no_unique_address]] int two;113};114 115union UnionLayout {116 int a;117 double b;118 CStruct c;119 [[no_unique_address]] CEmptyStruct d;120 [[no_unique_address]] CEmptyStruct2 e;121};122 123union UnionLayout2 {124 CStruct c;125 int a;126 CEmptyStruct2 e;127 double b;128 [[no_unique_address]] CEmptyStruct d;129};130 131union UnionLayout3 {132 CStruct c;133 int a;134 double b;135 [[no_unique_address]] CEmptyStruct d;136};137 138struct StructWithAnonUnion {139 union {140 int a;141 double b;142 CStruct c;143 [[no_unique_address]] CEmptyStruct d;144 [[no_unique_address]] CEmptyStruct2 e;145 };146};147 148struct StructWithAnonUnion2 {149 union {150 CStruct c;151 int a;152 CEmptyStruct2 e;153 double b;154 [[no_unique_address]] CEmptyStruct d;155 };156};157 158struct StructWithAnonUnion3 {159 union {160 CStruct c;161 int a;162 CEmptyStruct2 e;163 double b;164 [[no_unique_address]] CEmptyStruct d;165 } u;166};167 168static_assert(__is_layout_compatible(CStruct, CStructNoUniqueAddress) != bool(__has_cpp_attribute(no_unique_address)), "");169static_assert(__is_layout_compatible(CStructNoUniqueAddress, CStructNoUniqueAddress2) != bool(__has_cpp_attribute(no_unique_address)), "");170static_assert(!__is_layout_compatible(UnionLayout, UnionLayout2), "");171static_assert(!__is_layout_compatible(UnionLayout, UnionLayout3), "");172static_assert(!__is_layout_compatible(StructWithAnonUnion, StructWithAnonUnion2), "");173static_assert(!__is_layout_compatible(StructWithAnonUnion, StructWithAnonUnion3), "");174#endif175} // namespace cwg2759176 177namespace cwg2770 { // cwg2770: 20 open 2023-07-14178#if __cplusplus >= 202002L179template<typename T>180struct B {181 static_assert(sizeof(T) == 1);182 using type = int;183};184 185template<typename T>186int f(T t, typename B<T>::type u) requires (sizeof(t) == 1);187 188template<typename T>189int f(T t, long);190 191int i = f(1, 2);192int j = f('a', 2);193 194#endif195} // namespace cwg2770196 197namespace cwg2789 { // cwg2789: 18198#if __cplusplus >= 202302L199template <typename T = int>200struct Base {201 constexpr void g(); // #cwg2789-g1202};203 204template <typename T = int>205struct Base2 {206 constexpr void g() requires true; // #cwg2789-g2207};208 209template <typename T = int>210struct S : Base<T>, Base2<T> {211 constexpr void f();212 constexpr void f(this S&) requires true{};213 214 using Base<T>::g;215 using Base2<T>::g;216};217 218void test() {219 S<> s;220 s.f();221 s.g();222 // since-cxx23-error@-1 {{call to member function 'g' is ambiguous}}223 // since-cxx23-note@#cwg2789-g1 {{candidate function}}224 // since-cxx23-note@#cwg2789-g2 {{candidate function}}225}226#endif227} // namespace cwg2789228 229namespace cwg2798 { // cwg2798: 17230#if __cplusplus > 202302L231struct string {232 constexpr string() {233 data_ = new char[6]();234 __builtin_memcpy(data_, "Hello", 5);235 data_[5] = 0;236 }237 constexpr ~string() { delete[] data_; }238 constexpr unsigned long size() const { return 5; };239 constexpr const char *data() const { return data_; }240 241 char *data_;242};243struct X {244 string s;245};246consteval X f() { return {}; }247 248static_assert(false, f().s);249// since-cxx26-error@-1 {{static assertion failed: Hello}}250#endif251} // namespace cwg2798252