| 1 |
// RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -fcxx-exceptions -verify |
| 2 |
|
| 3 |
// verify no value-dependent-assertion crash in constexpr function body and no |
| 4 |
// bogus diagnostics. |
| 5 |
class Foo { |
| 6 |
constexpr Foo() { |
| 7 |
while (invalid()) {} // expected-error {{use of undeclared identifier}} |
| 8 |
if (invalid()) {} // expected-error {{use of undeclared identifier}} |
| 9 |
} |
| 10 |
}; |
| 11 |
|
| 12 |
constexpr void test1() { |
| 13 |
while (invalid()) {} // expected-error {{use of undeclared identifier}} |
| 14 |
if (invalid()) {} // expected-error {{use of undeclared identifier}} |
| 15 |
} |
| 16 |
|
| 17 |
struct A { |
| 18 |
int *p = new int(invalid()); // expected-error {{use of undeclared identifier}} |
| 19 |
constexpr ~A() { delete p; } |
| 20 |
}; |
| 21 |
constexpr int test2() { |
| 22 |
A a; |
| 23 |
return 1; |
| 24 |
} |
| 25 |
|
| 26 |
constexpr int test3() { |
| 27 |
return invalid(); // expected-error {{use of undeclared identifier}} |
| 28 |
} |
| 29 |
|
| 30 |
constexpr int test4() { |
| 31 |
if (invalid()) // expected-error {{use of undeclared identifier}} |
| 32 |
return 1; |
| 33 |
else |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
constexpr int test5() { // expected-error {{constexpr function never produce}} |
| 38 |
for (;; a++); // expected-error {{use of undeclared identifier}} \ |
| 39 |
expected-note {{constexpr evaluation hit maximum step limit; possible infinite loop?}} |
| 40 |
return 1; |
| 41 |
} |
| 42 |
|
| 43 |
constexpr int test6() { // expected-error {{constexpr function never produce}} |
| 44 |
int n = 0; |
| 45 |
switch (n) { |
| 46 |
for (;; a++) { // expected-error {{use of undeclared identifier}} |
| 47 |
case 0:; // expected-note {{constexpr evaluation hit maximum step limit; possible infinite loop?}} |
| 48 |
} |
| 49 |
} |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
constexpr bool test7() { |
| 54 |
for (int n = 0; ; invalid()) { if (n == 1) return true; } // expected-error {{use of undeclared identifier}} |
| 55 |
throw "bad"; |
| 56 |
} |
| 57 |
|
| 58 |
constexpr void test8() { |
| 59 |
do {} while (invalid()); // expected-error {{use of undeclared identifier}} |
| 60 |
throw "bad"; |
| 61 |
} |
| 62 |
|
| 63 |
template<int x> constexpr int f(int y) { // expected-note {{candidate template ignored}} |
| 64 |
return x * y; |
| 65 |
} |
| 66 |
constexpr int test9(int x) { |
| 67 |
return f<1>(f<x>(1)); // expected-error {{no matching function for call to 'f'}} |
| 68 |
} |
| 69 |
|
| 70 |
constexpr int test10() { return undef(); } // expected-error {{use of undeclared identifier 'undef'}} |
| 71 |
static_assert(test10() <= 1, "should not crash"); // expected-error {{static assertion expression is not an integral constant expression}} |
| 72 |
|
| 73 |
struct X {} array[] = {undef()}; // expected-error {{use of undeclared identifier 'undef'}} |
| 74 |
constexpr void test11() { |
| 75 |
for (X& e : array) {} |
| 76 |
} |
| 77 |
|
| 78 |
constexpr int test12() { return "wrong"; } // expected-error {{cannot initialize return object of type 'int'}} |
| 79 |
constexpr int force12 = test12(); // expected-error {{must be initialized by a constant}} |
| 80 |
|
| 81 |
#define TEST_EVALUATE(Name, X) \ |
| 82 |
constexpr int testEvaluate##Name() { \ |
| 83 |
X return 0; \ |
| 84 |
} \ |
| 85 |
constexpr int forceEvaluate##Name = testEvaluate##Name() |
| 86 |
// Check that a variety of broken loops don't crash constant evaluation. |
| 87 |
// We're not checking specific recovery here so don't assert diagnostics. |
| 88 |
TEST_EVALUATE(Switch, switch (!!){}); // expected-error + {{}} |
| 89 |
TEST_EVALUATE(SwitchInit, switch (auto x = !!){}); // expected-error + {{}} |
| 90 |
TEST_EVALUATE(SwitchCondValDep, switch (invalid_value) { default: break; }); // expected-error + {{}} |
| 91 |
TEST_EVALUATE(For, for (!!){}); // expected-error + {{}} |
| 92 |
// FIXME: should bail out instead of looping. |
| 93 |
// expected-note@-2 + {{infinite loop}} |
| 94 |
// expected-note@-3 {{in call}} |
| 95 |
TEST_EVALUATE(ForRange, for (auto x : !!){}); // expected-error + {{}} |
| 96 |
TEST_EVALUATE(While, while (!!){}); // expected-error + {{}} |
| 97 |
TEST_EVALUATE(DoWhile, do {} while (!!);); // expected-error + {{}} |
| 98 |
TEST_EVALUATE(DoWhileCond, do {} while (some_cond < 10);); // expected-error {{use of undeclared identifier}} \ |
| 99 |
// expected-error {{constexpr variable 'forceEvaluateDoWhileCond' must be initialized by a constant expression}} |
| 100 |
TEST_EVALUATE(If, if (!!){};); // expected-error + {{}} |
| 101 |
TEST_EVALUATE(IfInit, if (auto x = !!; 1){};);// expected-error + {{}} |
| 102 |
TEST_EVALUATE(ForInit, for (!!;;){};);// expected-error + {{}} |
| 103 |
// expected-note@-1 + {{infinite loop}} |
| 104 |
// expected-note@-2 {{in call}} |
| 105 |
TEST_EVALUATE(ForCond, for (; !!;){};);// expected-error + {{}} |
| 106 |
TEST_EVALUATE(ForInc, for (;; !!){};);// expected-error + {{}} |
| 107 |
// expected-note@-1 + {{infinite loop}} |
| 108 |
// expected-note@-2 {{in call}} |
| 109 |
TEST_EVALUATE(ForCondUnDef, for (;some_cond;){};); // expected-error + {{}} |