110 lines · cpp
1// RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -fcxx-exceptions -verify2 3// verify no value-dependent-assertion crash in constexpr function body and no4// bogus diagnostics.5class 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 12constexpr void test1() {13 while (invalid()) {} // expected-error {{use of undeclared identifier}}14 if (invalid()) {} // expected-error {{use of undeclared identifier}}15}16 17struct A {18 int *p = new int(invalid()); // expected-error {{use of undeclared identifier}}19 constexpr ~A() { delete p; }20};21constexpr int test2() {22 A a;23 return 1;24}25 26constexpr int test3() {27 return invalid(); // expected-error {{use of undeclared identifier}}28}29 30constexpr int test4() {31 if (invalid()) // expected-error {{use of undeclared identifier}}32 return 1;33 else34 return 0;35}36 37constexpr 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 43constexpr 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 53constexpr bool test7() {54 for (int n = 0; ; invalid()) { if (n == 1) return true; } // expected-error {{use of undeclared identifier}}55 throw "bad";56}57 58constexpr void test8() {59 do {} while (invalid()); // expected-error {{use of undeclared identifier}}60 throw "bad";61}62 63template<int x> constexpr int f(int y) { // expected-note {{candidate template ignored}}64 return x * y;65}66constexpr int test9(int x) {67 return f<1>(f<x>(1)); // expected-error {{no matching function for call to 'f'}}68}69 70constexpr int test10() { return undef(); } // expected-error {{use of undeclared identifier 'undef'}}71static_assert(test10() <= 1, "should not crash"); // expected-error {{static assertion expression is not an integral constant expression}}72 73struct X {} array[] = {undef()}; // expected-error {{use of undeclared identifier 'undef'}}74constexpr void test11() {75 for (X& e : array) {}76}77 78constexpr int test12() { return "wrong"; } // expected-error {{cannot initialize return object of type 'int'}}79constexpr 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.88TEST_EVALUATE(Switch, switch (!!){}); // expected-error + {{}}89TEST_EVALUATE(SwitchInit, switch (auto x = !!){}); // expected-error + {{}}90TEST_EVALUATE(SwitchCondValDep, switch (invalid_value) { default: break; }); // expected-error + {{}}91TEST_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}}95TEST_EVALUATE(ForRange, for (auto x : !!){}); // expected-error + {{}}96TEST_EVALUATE(While, while (!!){}); // expected-error + {{}}97TEST_EVALUATE(DoWhile, do {} while (!!);); // expected-error + {{}}98TEST_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}}100TEST_EVALUATE(If, if (!!){};); // expected-error + {{}}101TEST_EVALUATE(IfInit, if (auto x = !!; 1){};);// expected-error + {{}}102TEST_EVALUATE(ForInit, for (!!;;){};);// expected-error + {{}}103 // expected-note@-1 + {{infinite loop}}104 // expected-note@-2 {{in call}}105TEST_EVALUATE(ForCond, for (; !!;){};);// expected-error + {{}}106TEST_EVALUATE(ForInc, for (;; !!){};);// expected-error + {{}}107 // expected-note@-1 + {{infinite loop}}108 // expected-note@-2 {{in call}}109TEST_EVALUATE(ForCondUnDef, for (;some_cond;){};); // expected-error + {{}}110