1 // RUN: %clang_cc1 -std=c++23 -verify %s 2 test_consteval()3void test_consteval() { 4 if consteval ({(void)1;}); // expected-error {{expected { after consteval}} 5 if consteval (void) 0; // expected-error {{expected { after consteval}} 6 if consteval { 7 (void)0; 8 } else (void)0; // expected-error {{expected { after else}} 9 10 static_assert([] { 11 if consteval { 12 return 0; 13 } 14 return 1; 15 }() == 0); 16 17 static_assert([] { 18 if consteval { 19 return 0; 20 } else { 21 return 1; 22 } 23 }() == 0); 24 25 static_assert([] { 26 if !consteval { 27 return 0; 28 } else { 29 return 1; 30 } 31 }() == 1); 32 33 static_assert([] { 34 if not consteval { 35 return 0; 36 } 37 return 1; 38 }() == 1); 39 40 if consteval [[likely]] { // expected-warning {{attribute 'likely' has no effect when annotating an 'if consteval' statement}}\ 41 // expected-note 2{{annotating the 'if consteval' statement here}} 42 43 44 } 45 else [[unlikely]] { // expected-warning {{attribute 'unlikely' has no effect when annotating an 'if consteval' statement}} 46 47 } 48 49 } 50 51 void test_consteval_jumps() { 52 if consteval { // expected-note 4{{jump enters controlled statement of consteval if}} 53 goto a; 54 goto b; // expected-error {{cannot jump from this goto statement to its label}} 55 a:; 56 } else { 57 goto b; 58 goto a; // expected-error {{cannot jump from this goto statement to its label}} 59 b:; 60 } 61 goto a; // expected-error {{cannot jump from this goto statement to its label}} 62 goto b; // expected-error {{cannot jump from this goto statement to its label}} 63 } 64 65 void test_consteval_switch() { 66 int x = 42; 67 switch (x) { 68 if consteval { // expected-note 2{{jump enters controlled statement of consteval if}} 69 case 1:; // expected-error {{cannot jump from switch statement to this case label}} 70 default:; // expected-error {{cannot jump from switch statement to this case label}} 71 } else { 72 } 73 } 74 switch (x) { 75 if consteval { // expected-note 2{{jump enters controlled statement of consteval if}} 76 } else { 77 case 2:; // expected-error {{cannot jump from switch statement to this case label}} 78 default:; // expected-error {{cannot jump from switch statement to this case label}} 79 } 80 } 81 } 82 83 consteval int f(int i) { return i; } 84 constexpr int g(int i) { 85 if consteval { 86 return f(i); 87 } else { 88 return 42; 89 } 90 } 91 static_assert(g(10) == 10); 92 93 constexpr int h(int i) { // expected-note {{declared here}} 94 if !consteval { 95 return f(i); // expected-error {{call to consteval function 'f' is not a constant expression}}\ 96 // expected-note {{cannot be used in a constant expression}} 97 } 98 return 0; 99 } 100 101 consteval void warn_in_consteval() { 102 if consteval { // expected-warning {{consteval if is always true in an immediate context}} 103 if consteval {} // expected-warning {{consteval if is always true in an immediate context}} 104 } 105 } 106 107 constexpr void warn_in_consteval2() { 108 if consteval { 109 if consteval {} // expected-warning {{consteval if is always true in an immediate context}} 110 } 111 } 112 113 auto y = []() consteval { 114 if consteval { // expected-warning {{consteval if is always true in an immediate context}} 115 if consteval {} // expected-warning {{consteval if is always true in an immediate context}} 116 } 117 }; 118 119 namespace test_transform { 120 int f(auto n) { 121 if consteval { 122 n.foo; //expected-error {{no member named}} 123 } 124 else { 125 } 126 127 if !consteval { 128 n.foo; //expected-error {{no member named}} 129 } 130 else { 131 } 132 133 return 0; 134 } 135 136 constexpr int g(auto n) { 137 if consteval { 138 } 139 else { 140 n.foo; //expected-error {{no member named}} 141 } 142 143 if !consteval { 144 } 145 else { 146 n.foo; //expected-error {{no member named}} 147 } 148 149 return 0; 150 } 151 152 struct S {}; 153 void test() { 154 f(S{}); //expected-note {{in instantiation}} 155 g(S{}); //expected-note {{in instantiation}} 156 } 157 158 } 159