brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.4 KiB · af2dce8 Raw
228 lines · cpp
1// RUN: %clang_cc1 -std=c++2a -x c++ %s -verify2 3static_assert(requires { { 0 }; });4static_assert(requires { { "aaaa" }; });5static_assert(requires { { (0).da }; }); // expected-error{{member reference base type 'int' is not a structure or union}}6 7void foo() {}8static_assert(requires { { foo() }; });9 10// Substitution failure in expression11 12struct A {};13struct B {14    B operator+(const B &other) const { return other; }15};16struct C {17    C operator+(C &other) const { return other; }18};19 20template<typename T> requires requires (T a, const T& b) { { a + b }; } // expected-note{{because 'a + b' would be invalid: invalid operands to binary expression ('A' and 'const A')}} expected-note{{because 'a + b' would be invalid: invalid operands to binary expression ('C' and 'const C')}}21struct r1 {};22 23using r1i1 = r1<int>;24using r1i2 = r1<A>; // expected-error{{constraints not satisfied for class template 'r1' [with T = A]}}25using r1i3 = r1<B>;26using r1i4 = r1<C>; // expected-error{{constraints not satisfied for class template 'r1' [with T = C]}}27 28struct D { void foo() {} };29 30template<typename T> requires requires (T a) { { a.foo() }; } // expected-note{{because 'a.foo()' would be invalid: no member named 'foo' in 'A'}} expected-note{{because 'a.foo()' would be invalid: member reference base type 'int' is not a structure or union}} expected-note{{because 'a.foo()' would be invalid: 'this' argument to member function 'foo' has type 'const D', but function is not marked const}}31struct r2 {};32 33using r2i1 = r2<int>; // expected-error{{constraints not satisfied for class template 'r2' [with T = int]}}34using r2i2 = r2<A>; // expected-error{{constraints not satisfied for class template 'r2' [with T = A]}}35using r2i3 = r2<D>;36using r2i4 = r2<const D>; // expected-error{{constraints not satisfied for class template 'r2' [with T = const D]}}37 38template<typename T> requires requires { { sizeof(T) }; } // expected-note{{because 'sizeof(T)' would be invalid: invalid application of 'sizeof' to an incomplete type 'void'}} expected-note{{because 'sizeof(T)' would be invalid: invalid application of 'sizeof' to an incomplete type 'class nonexistent'}}39struct r3 {};40 41using r3i1 = r3<int>;42using r3i2 = r3<A>;43using r3i3 = r3<A &>;44using r3i4 = r3<void>; // expected-error{{constraints not satisfied for class template 'r3' [with T = void]}}45using r3i4 = r3<class nonexistent>; // expected-error{{constraints not satisfied for class template 'r3' [with T = class nonexistent]}}46 47// Non-dependent expressions48 49template<typename T> requires requires (T t) { { 0 }; { "a" }; { (void)'a' }; }50struct r4 {};51 52using r4i1 = r4<int>;53using r4i2 = r4<int[10]>;54using r4i3 = r4<int(int)>;55 56// Noexcept requirement57void maythrow() { }58static_assert(!requires { { maythrow() } noexcept; });59static_assert(requires { { 1 } noexcept; });60 61struct E { void operator++(int) noexcept; };62struct F { void operator++(int); };63 64template<typename T> requires requires (T t) { { t++ } noexcept; } // expected-note{{because 't ++' may throw an exception}}65struct r5 {};66 67using r5i1 = r5<int>;68using r5i2 = r5<E>;69using r5i2 = r5<F>; // expected-error{{constraints not satisfied for class template 'r5' [with T = F]}}70 71template<typename T> requires requires (T t) { { t.foo() } noexcept; } // expected-note{{because 't.foo()' would be invalid: no member named 'foo' in 'E'}}72struct r6 {};73 74using r6i = r6<E>; // expected-error{{constraints not satisfied for class template 'r6' [with T = E]}}75 76template<typename T, typename U>77constexpr bool is_same_v = false;78 79template<typename T>80constexpr bool is_same_v<T, T> = true;81 82template<typename T> struct remove_reference { using type = T; };83template<typename T> struct remove_reference<T&> { using type = T; };84 85template<typename T, typename U>86concept Same = is_same_v<T, U>;87 88template<typename T>89concept Large = sizeof(typename remove_reference<T>::type) >= 4;90// expected-note@-1{{because 'sizeof(typename remove_reference<short &>::type) >= 4' (2 >= 4) evaluated to false}}91 92template<typename T> requires requires (T t) { { t } -> Large; } // expected-note{{because 'short &' does not satisfy 'Large'}}93struct r7 {};94 95using r7i1 = r7<int>;96using r7i2 = r7<short>; // expected-error{{constraints not satisfied for class template 'r7' [with T = short]}}97 98template<typename T> requires requires (T t) { { t } -> Same<T&>; }99struct r8 {};100 101using r8i1 = r8<int>;102using r8i2 = r8<short*>;103 104// Substitution failure in type constraint105 106template<typename T> requires requires (T t) { { t } -> Same<typename T::type&>; }107// expected-note@-1{{because 'Same<expr-type, typename T::type &>' would be invalid: type 'int' cannot be used prior to '::' because it has no members}}108struct r9 {};109 110struct M { using type = M; };111 112using r9i1 = r9<M>;113using r9i2 = r9<int>; // expected-error{{constraints not satisfied for class template 'r9' [with T = int]}}114 115// Substitution failure in both expression and return type requirement116 117template<typename T> requires requires (T t) { { t.foo() } -> Same<typename T::type>; } // expected-note{{because 't.foo()' would be invalid: member reference base type 'int' is not a structure or union}}118struct r10 {};119 120using r10i = r10<int>; // expected-error{{constraints not satisfied for class template 'r10' [with T = int]}}121 122// Non-type concept in type constraint123 124template<int T>125concept IsEven = (T % 2) == 0;126 127template<typename T> requires requires (T t) { { t } -> IsEven; } // expected-error{{concept named in type constraint is not a type concept}}128struct r11 {};129 130// Value categories131 132template<auto a = 0>133requires requires (int b) {134  { a } -> Same<int>;135  { b } -> Same<int&>;136  { 0 } -> Same<int>;137  { static_cast<int&&>(a) } -> Same<int&&>;138} void f1() {}139template void f1<>();140 141// C++ [expr.prim.req.compound] Example142namespace std_example {143  template<typename T> concept C1 =144    requires(T x) {145      {x++};146    };147 148  template<typename T, typename U> constexpr bool is_same_v = false;149  template<typename T> constexpr bool is_same_v<T, T> = true;150 151  template<typename T, typename U> concept same_as = is_same_v<T, U>;152  // expected-note@-1 {{because 'is_same_v<int, typename std_example::T2::inner>' evaluated to false}}153 154  static_assert(C1<int>);155  static_assert(C1<int*>);156  template<C1 T> struct C1_check {};157  using c1c1 = C1_check<int>;158  using c1c2 = C1_check<int[10]>;159 160  template<typename T> concept C2 =161    requires(T x) {162      {*x} -> same_as<typename T::inner>;163      // expected-note@-1{{because 'same_as<int, typename std_example::T2::inner>' evaluated to false}}164      // expected-note@-2{{because '*x' would be invalid: indirection requires pointer operand ('int' invalid)}}165    };166 167  struct T1 {168    using inner = int;169    inner operator *() { return 0; }170  };171  struct T2 {172    using inner = int *;173    int operator *() { return 0; }174  };175  static_assert(C2<T1>);176  template<C2 T> struct C2_check {}; // expected-note{{because 'int' does not satisfy 'C2'}} expected-note{{because 'T2' does not satisfy 'C2'}}177  using c2c1 = C2_check<int>; // expected-error{{constraints not satisfied for class template 'C2_check' [with T = int]}}178  using c2c2 = C2_check<T2>; // expected-error{{constraints not satisfied for class template 'C2_check' [with T = T2]}}179 180  template<typename T>181  void g(T t) noexcept(sizeof(T) == 1) {}182 183  template<typename T> concept C5 =184    requires(T x) {185      {g(x)} noexcept; // expected-note{{because 'g(x)' may throw an exception}}186    };187 188  static_assert(C5<char>);189  template<C5 T> struct C5_check {}; // expected-note{{because 'short' does not satisfy 'C5'}}190  using c5 = C5_check<short>; // expected-error{{constraints not satisfied for class template 'C5_check' [with T = short]}}191}192 193namespace access_checks {194namespace in_return_type_requirement {195 196// https://github.com/llvm/llvm-project/issues/93788197template <typename From, typename To>198concept is_assignable = requires(From from, To to) {199  from = to;200};201 202template <typename T>203class trait {204 public:205  using public_type = int;206 private:207  using private_type = int;208};209 210template <typename T>211concept has_field = requires(T t) {212  { t.field } -> is_assignable<typename trait<T>::private_type>;  // expected-note {{'private_type' is a private member}}213};214template <typename T>215concept has_field2 = requires(T t) {216  { t.field } -> is_assignable<typename trait<T>::public_type>;217};218 219struct A {220  int field;221};222static_assert(has_field<A>); // expected-error {{static assertion failed}} \223                             // expected-note {{because 'A' does not satisfy 'has_field'}}224static_assert(has_field2<A>);225 226}  // namespace access_checks227}  // namespace in_return_type_requirement228