| 1 |
// RUN: %clang_cc1 -std=c++2a -x c++ %s -verify |
| 2 |
|
| 3 |
static_assert(requires { requires true; }); |
| 4 |
|
| 5 |
template<typename T> requires requires { requires false; } // expected-note{{because 'false' evaluated to false}} |
| 6 |
struct r1 {}; |
| 7 |
|
| 8 |
using r1i = r1<int>; // expected-error{{constraints not satisfied for class template 'r1' [with T = int]}} |
| 9 |
|
| 10 |
template<typename T> requires requires { requires sizeof(T) == 0; } // expected-note{{because 'sizeof(int) == 0' (4 == 0) evaluated to false}} |
| 11 |
struct r2 {}; |
| 12 |
|
| 13 |
using r2i = r2<int>; // expected-error{{constraints not satisfied for class template 'r2' [with T = int]}} |
| 14 |
|
| 15 |
template<typename T> requires requires (T t) { requires sizeof(t) == 0; } // expected-note{{because 'sizeof (t) == 0' (4 == 0) evaluated to false}} |
| 16 |
struct r3 {}; |
| 17 |
|
| 18 |
using r3i = r3<int>; // expected-error{{constraints not satisfied for class template 'r3' [with T = int]}} |
| 19 |
|
| 20 |
template<typename T> |
| 21 |
struct X { |
| 22 |
template<typename U> requires requires (U u) { requires sizeof(u) == sizeof(T); } // expected-note{{because 'sizeof (u) == sizeof(T)' would be invalid: invalid application of 'sizeof' to an incomplete type 'void'}} |
| 23 |
struct r4 {}; |
| 24 |
}; |
| 25 |
|
| 26 |
using r4i = X<void>::r4<int>; // expected-error{{constraints not satisfied for class template 'r4' [with U = int]}} |
| 27 |
|
| 28 |
// C++ [expr.prim.req.nested] Examples |
| 29 |
namespace std_example { |
| 30 |
template<typename U> concept C1 = sizeof(U) == 1; // expected-note{{because 'sizeof(int) == 1' (4 == 1) evaluated to false}} |
| 31 |
template<typename T> concept D = |
| 32 |
requires (T t) { |
| 33 |
requires C1<decltype (+t)>; // expected-note{{because 'decltype(+t)' (aka 'int') does not satisfy 'C1'}} |
| 34 |
}; |
| 35 |
|
| 36 |
struct T1 { char operator+() { return 'a'; } }; |
| 37 |
static_assert(D<T1>); |
| 38 |
template<D T> struct D_check {}; // expected-note{{because 'short' does not satisfy 'D'}} |
| 39 |
using dc1 = D_check<short>; // expected-error{{constraints not satisfied for class template 'D_check' [with T = short]}} |
| 40 |
|
| 41 |
template<typename T> |
| 42 |
concept C2 = requires (T a) { |
| 43 |
requires sizeof(a) == 4; // OK |
| 44 |
requires a == 0; // expected-error{{substitution into constraint expression resulted in a non-constant expression}} |
| 45 |
// expected-note@-1{{while checking the satisfaction of nested requirement requested here}} |
| 46 |
// expected-note@-2{{while checking the satisfaction of nested requirement requested here}} |
| 47 |
// expected-note@-5{{while substituting template arguments into constraint expression here}} |
| 48 |
// expected-note@-4{{function parameter 'a' with unknown value cannot be used in a constant expression}} |
| 49 |
// expected-note@-7{{declared here}} |
| 50 |
}; |
| 51 |
static_assert(C2<int>); // expected-error{{static assertion failed}} |
| 52 |
// expected-note@-1{{while checking the satisfaction of concept 'C2<int>' requested here}} |
| 53 |
// expected-note@-2{{because 'int' does not satisfy 'C2'}} |
| 54 |
} |
| 55 |
|
| 56 |
template<typename T> |
| 57 |
concept K = requires (T::Type X) { |
| 58 |
X.next(); |
| 59 |
}; |
| 60 |
|
| 61 |
namespace SubstitutionFailureNestedRequires { |
| 62 |
template<class T> concept True = true; |
| 63 |
template<class T> concept False = false; |
| 64 |
|
| 65 |
struct S { double value; }; |
| 66 |
|
| 67 |
template <class T> |
| 68 |
concept Pipes = requires (T x) { |
| 69 |
requires True<decltype(x.value)> || True<T> || False<T>; |
| 70 |
requires False<T> || True<T> || True<decltype(x.value)>; |
| 71 |
}; |
| 72 |
|
| 73 |
template <class T> |
| 74 |
concept Amps1 = requires (T x) { |
| 75 |
requires True<decltype(x.value)> && True<T> && !False<T>; // #Amps1 |
| 76 |
}; |
| 77 |
template <class T> |
| 78 |
concept Amps2 = requires (T x) { |
| 79 |
requires True<T> && True<decltype(x.value)>; |
| 80 |
}; |
| 81 |
|
| 82 |
static_assert(Pipes<S>); |
| 83 |
static_assert(Pipes<double>); |
| 84 |
|
| 85 |
static_assert(Amps1<S>); |
| 86 |
static_assert(Amps1<double>); |
| 87 |
|
| 88 |
static_assert(Amps2<S>); |
| 89 |
static_assert(Amps2<double>); |
| 90 |
|
| 91 |
template<class T> |
| 92 |
void foo1() requires requires (T x) { |
| 93 |
requires |
| 94 |
True<decltype(x.value)> |
| 95 |
&& True<T>; |
| 96 |
} {} |
| 97 |
template<class T> void fooPipes() requires Pipes<T> {} |
| 98 |
template<class T> void fooAmps1() requires Amps1<T> {} |
| 99 |
void foo() { |
| 100 |
foo1<S>(); |
| 101 |
foo1<int>(); |
| 102 |
fooPipes<S>(); |
| 103 |
fooPipes<int>(); |
| 104 |
fooAmps1<S>(); |
| 105 |
fooAmps1<int>(); |
| 106 |
} |
| 107 |
|
| 108 |
template<class T> |
| 109 |
concept HasNoValue = requires (T x) { |
| 110 |
requires !True<decltype(x.value)> && True<T>; |
| 111 |
}; |
| 112 |
// FIXME: 'int' does not satisfy 'HasNoValue' currently since `!True<decltype(x.value)>` is an invalid expression. |
| 113 |
// But, in principle, it should be constant-evaluated to true. |
| 114 |
// This happens also for requires expression and is not restricted to nested requirement. |
| 115 |
static_assert(!HasNoValue<int>); |
| 116 |
static_assert(!HasNoValue<S>); |
| 117 |
|
| 118 |
template<class T> constexpr bool NotAConceptTrue = true; |
| 119 |
template <class T> |
| 120 |
concept SFinNestedRequires = requires (T x) { |
| 121 |
// SF in a non-concept specialisation should also be evaluated to false. |
| 122 |
requires NotAConceptTrue<decltype(x.value)> || NotAConceptTrue<T>; |
| 123 |
}; |
| 124 |
static_assert(SFinNestedRequires<int>); |
| 125 |
static_assert(SFinNestedRequires<S>); |
| 126 |
template <class T> |
| 127 |
void foo() requires SFinNestedRequires<T> {} |
| 128 |
void bar() { |
| 129 |
foo<int>(); |
| 130 |
foo<S>(); |
| 131 |
} |
| 132 |
namespace ErrorExpressions_NotSF { |
| 133 |
template<typename T> struct X { static constexpr bool value = T::value; }; // #X_Value |
| 134 |
struct True { static constexpr bool value = true; }; |
| 135 |
struct False { static constexpr bool value = false; }; |
| 136 |
template<typename T> concept C = true; |
| 137 |
template<typename T> concept F = false; |
| 138 |
|
| 139 |
template<typename T> requires requires(T) { requires C<T> || X<T>::value; } void foo(); |
| 140 |
|
| 141 |
template<typename T> requires requires(T) { requires C<T> && X<T>::value; } void bar(); // #bar |
| 142 |
template<typename T> requires requires(T) { requires F<T> || (X<T>::value && C<T>); } void baz(); |
| 143 |
|
| 144 |
void func() { |
| 145 |
foo<True>(); |
| 146 |
foo<False>(); |
| 147 |
foo<int>(); |
| 148 |
|
| 149 |
bar<True>(); |
| 150 |
bar<False>(); |
| 151 |
// expected-error@-1 {{no matching function for call to 'bar'}} |
| 152 |
// expected-note@#bar {{while substituting template arguments into constraint expression here}} |
| 153 |
// expected-note@#bar {{while checking the satisfaction of nested requirement requested here}} |
| 154 |
// expected-note@#bar {{candidate template ignored: constraints not satisfied [with T = False]}} |
| 155 |
// expected-note@#bar {{because 'X<SubstitutionFailureNestedRequires::ErrorExpressions_NotSF::False>::value' evaluated to false}} |
| 156 |
|
| 157 |
bar<int>(); |
| 158 |
// expected-error@-1 {{no matching function for call to 'bar'}} \ |
| 159 |
// expected-note@-1 {{while checking constraint satisfaction for template 'bar<int>' required here}} \ |
| 160 |
// expected-note@-1 {{while substituting deduced template arguments into function template 'bar' [with T = int]}} \ |
| 161 |
// expected-note@#bar {{in instantiation of static data member}} |
| 162 |
// expected-note@#bar {{while checking the satisfaction of nested requirement requested here}} |
| 163 |
// expected-note@#bar {{while substituting template arguments into constraint expression here}} |
| 164 |
// expected-note@#bar {{candidate template ignored}} |
| 165 |
// expected-error@#X_Value {{type 'int' cannot be used prior to '::' because it has no members}} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
namespace no_crash_D138914 { |
| 171 |
// https://reviews.llvm.org/D138914 |
| 172 |
template <class a, a> struct b; |
| 173 |
template <bool c> using d = b<bool, c>; |
| 174 |
template <class a, class e> using f = d<__is_same(a, e)>; |
| 175 |
template <class a, class e> |
| 176 |
concept g = f<a, e>::h; |
| 177 |
template <class a, class e> |
| 178 |
concept i = g<e, a>; |
| 179 |
template <typename> class j { |
| 180 |
template <typename k> |
| 181 |
requires requires { requires i<j, k>; } |
| 182 |
j(); |
| 183 |
}; |
| 184 |
template <> j(); // expected-error {{deduction guide declaration without trailing return type}} |
| 185 |
} |