xref: /llvm-project/clang/test/AST/ByteCode/invalid.cpp (revision 0bc8168070677ef08d58864477d95ebdef917d0c)
1 // RUN: %clang_cc1 -fcxx-exceptions -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
2 // RUN: %clang_cc1 -fcxx-exceptions -std=c++20 -verify=ref,both %s
3 
4 namespace Throw {
5 
6   constexpr int ConditionalThrow(bool t) {
7     if (t)
8       throw 4; // both-note {{subexpression not valid in a constant expression}}
9 
10     return 0;
11   }
12 
13   static_assert(ConditionalThrow(false) == 0, "");
14   static_assert(ConditionalThrow(true) == 0, ""); // both-error {{not an integral constant expression}} \
15                                                   // both-note {{in call to 'ConditionalThrow(true)'}}
16 
17   constexpr int Throw() { // both-error {{never produces a constant expression}}
18     throw 5; // both-note {{subexpression not valid in a constant expression}}
19     return 0;
20   }
21 
22   constexpr int NoSubExpr() { // both-error {{never produces a constant expression}}
23     throw; // both-note 2{{subexpression not valid}}
24     return 0;
25   }
26   static_assert(NoSubExpr() == 0, ""); // both-error {{not an integral constant expression}} \
27                                        // both-note {{in call to}}
28 }
29 
30 namespace Asm {
31   constexpr int ConditionalAsm(bool t) {
32     if (t)
33       asm(""); // both-note {{subexpression not valid in a constant expression}}
34 
35     return 0;
36   }
37   static_assert(ConditionalAsm(false) == 0, "");
38   static_assert(ConditionalAsm(true) == 0, ""); // both-error {{not an integral constant expression}} \
39                                                 // both-note {{in call to 'ConditionalAsm(true)'}}
40 
41 
42   constexpr int Asm() { // both-error {{never produces a constant expression}}
43     __asm volatile(""); // both-note {{subexpression not valid in a constant expression}}
44     return 0;
45   }
46 }
47 
48 namespace Casts {
49   constexpr int a = reinterpret_cast<int>(12); // both-error {{must be initialized by a constant expression}} \
50                                                // both-note {{reinterpret_cast is not allowed}}
51 
52   void func() {
53     struct B {};
54     B b;
55     (void)*reinterpret_cast<void*>(&b); // both-error {{indirection not permitted on operand of type 'void *'}}
56   }
57 
58   /// Just make sure this doesn't crash.
59   float PR9558 = reinterpret_cast<const float&>("asd");
60 }
61