|
| 1 | +// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++11 %s |
| 2 | +// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++20 %s |
| 3 | +// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++2c %s |
| 4 | + |
| 5 | +template <typename T> |
| 6 | +struct Invalid { static_assert(false, "instantiated Invalid"); }; // #err-invalid |
| 7 | + |
| 8 | +template <typename T> |
| 9 | +int f(T a, Invalid<T> = {}); // #note-f |
| 10 | + |
| 11 | +// sanity check |
| 12 | +int e1 = f(0); |
| 13 | +//expected-error@#err-invalid {{static assertion failed: instantiated Invalid}} |
| 14 | +//expected-note@-2 {{in instantiation of default function argument expression for 'f<int>' required here}} |
| 15 | +//expected-note@#note-f {{in instantiation of template class 'Invalid<int>' requested here}} |
| 16 | +//expected-note@#note-f {{passing argument to parameter here}} |
| 17 | + |
| 18 | +int f(int); |
| 19 | +int ok1 = f(0); |
| 20 | +int e4 = f((const int&)(ok1)); |
| 21 | + |
| 22 | +int f(int, int = 0); |
| 23 | +int ok2 = f(0, 0); |
| 24 | + |
| 25 | +int e2 = f(0L); |
| 26 | +//expected-error@#err-invalid {{static assertion failed: instantiated Invalid}} |
| 27 | +//expected-note@-2 {{in instantiation of default function argument expression for 'f<long>' required here}} |
| 28 | +//expected-note@#note-f {{in instantiation of template class 'Invalid<long>' requested here}} |
| 29 | +//expected-note@#note-f {{passing argument to parameter here}} |
| 30 | + |
| 31 | +int f(long); |
| 32 | +int ok3 = f(0L); |
| 33 | + |
| 34 | +template <typename T> |
| 35 | +struct Invalid2 { static_assert(false, "instantiated Invalid2"); }; // #err-qualifiers |
| 36 | + |
| 37 | +template <typename T> |
| 38 | +int ref(T a, Invalid2<T> = {}); // expected-note 2{{here}} |
| 39 | +int ref(int&); |
| 40 | +int ref1 = ref(ok3); |
| 41 | +int ref2 = ref((const int&)ok3); // expected-note {{here}} |
| 42 | +//expected-error@#err-qualifiers {{static assertion failed: instantiated Invalid2}} |
| 43 | + |
| 44 | + |
| 45 | +template <typename T> |
| 46 | +int f_alias(T a, Invalid<T> = {}); |
| 47 | +using Alias = int; |
| 48 | +int f_alias(Alias); |
| 49 | +int ok4 = f_alias(0); |
| 50 | + |
| 51 | +#if __cplusplus >= 202002 |
| 52 | + |
| 53 | +struct Copyable { |
| 54 | + template <typename T> |
| 55 | + requires __is_constructible(Copyable, T) |
| 56 | + explicit Copyable(T op) noexcept; // #1 |
| 57 | + Copyable(const Copyable&) noexcept = default; // #2 |
| 58 | +}; |
| 59 | +static_assert(__is_constructible(Copyable, const Copyable&)); |
| 60 | + |
| 61 | +struct ImplicitlyCopyable { |
| 62 | + template <typename T> |
| 63 | + requires __is_constructible(ImplicitlyCopyable, T) |
| 64 | + explicit ImplicitlyCopyable(T op) = delete; // #1 |
| 65 | +}; |
| 66 | +static_assert(__is_constructible(ImplicitlyCopyable, const ImplicitlyCopyable&)); |
| 67 | + |
| 68 | + |
| 69 | +struct Movable { |
| 70 | + template <typename T> |
| 71 | + requires __is_constructible(Movable, T) // #err-self-constraint-1 |
| 72 | + explicit Movable(T op) noexcept; // #1 |
| 73 | + Movable(Movable&&) noexcept = default; // #2 |
| 74 | +}; |
| 75 | +static_assert(__is_constructible(Movable, Movable&&)); |
| 76 | +static_assert(__is_constructible(Movable, const Movable&)); |
| 77 | +// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(Movable, const Movable &)'}} |
| 78 | + |
| 79 | +static_assert(__is_constructible(Movable, int)); |
| 80 | +// expected-error@-1{{static assertion failed due to requirement '__is_constructible(Movable, int)'}} \ |
| 81 | +// expected-note@-1 2{{}} |
| 82 | +// expected-error@#err-self-constraint-1{{satisfaction of constraint '__is_constructible(Movable, T)' depends on itself}} |
| 83 | +// expected-note@#err-self-constraint-1 4{{}} |
| 84 | + |
| 85 | +template <typename T> |
| 86 | +struct Members { |
| 87 | + constexpr auto f(auto) { |
| 88 | + static_assert(false, ""); |
| 89 | + } |
| 90 | + constexpr auto f(int) { return 1; } |
| 91 | + constexpr auto f(int) requires true { return 2; } |
| 92 | + |
| 93 | + constexpr auto g(auto) { |
| 94 | + static_assert(false, "instantiated member"); //#err-qualified-member |
| 95 | + return 0; |
| 96 | + } |
| 97 | + constexpr auto g(int) & { return 1; } |
| 98 | + |
| 99 | + static constexpr auto s(auto) { |
| 100 | + static_assert(false, ""); |
| 101 | + } |
| 102 | + static constexpr auto s(int) { |
| 103 | + return 1; |
| 104 | + } |
| 105 | + static constexpr auto s(int) requires true { |
| 106 | + return 2; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +static_assert(Members<int[1]>{}.f(0) == 2); |
| 111 | +static_assert(Members<int[2]>{}.g(0) == 0); |
| 112 | +// expected-error@#err-qualified-member {{static assertion failed: instantiated member}} \ |
| 113 | +// expected-note@-1{{in instantiation of function template specialization 'Members<int[2]>::g<int>' }} |
| 114 | +Members<int[3]> m1; |
| 115 | +static_assert(m1.g(0) == 1); |
| 116 | +static_assert(Members<int[3]>{}.s(0) == 2); |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +namespace GH62096 { |
| 121 | +template <typename T> |
| 122 | +struct Oops { |
| 123 | + static_assert(sizeof(T) == 0); // #GH62096-err |
| 124 | + static constexpr bool value = true; |
| 125 | +}; |
| 126 | + |
| 127 | +template <class OPERATOR> |
| 128 | +concept Operator = Oops<OPERATOR>::value; // #GH62096-note1 |
| 129 | + |
| 130 | +template <Operator OP> void f(OP op); // // #GH62096-note2 |
| 131 | +void f(int); |
| 132 | + |
| 133 | +void g(int n) { f(n); } // OK |
| 134 | +void h(short n) { f(n); } |
| 135 | +// expected-error@#GH62096-err {{static assertion failed due to requirement 'sizeof(short) == 0'}} \ |
| 136 | +// expected-note@-1{{in instantiation of function template specialization}} \ |
| 137 | +// expected-note@-1{{while checking constraint satisfaction for template}} |
| 138 | +// expected-note@#GH62096-note1{{in instantiation}} |
| 139 | +// expected-note@#GH62096-note1{{while substituting template arguments into constraint expression here}} |
| 140 | +// expected-note@#GH62096-note2{{while substituting template arguments into constraint expression here}} |
| 141 | +// expected-note@#GH62096-note2{{while checking the satisfaction of concept}} |
| 142 | +// expected-note@#GH62096-err {{expression evaluates}} |
| 143 | +} |
| 144 | + |
| 145 | +#endif |
0 commit comments