xref: /llvm-project/clang/test/SemaCXX/ms-constexpr-invalid.cpp (revision b3e6ff331925dde24a4707452d657da0fdf7f588)
1 // RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify %s
2 // RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++17 -verify %s
3 
4 // Check explicitly invalid code
5 
runtime()6 void runtime() {} // expected-note {{declared here}}
7 
f0()8 [[msvc::constexpr]] void f0() { runtime(); } // expected-error {{constexpr function never produces a constant expression}} \
9                                              // expected-note {{non-constexpr function 'runtime' cannot be used in a constant expression}}
f1()10 [[msvc::constexpr]] constexpr void f1() {} // expected-error {{attribute 'msvc::constexpr' cannot be applied to the constexpr function 'f1'}}
11 #if __cplusplus >= 202202L
f2()12 [[msvc::constexpr]] consteval void f2() {} // expected-error {{attribute 'msvc::constexpr' cannot be applied to the consteval function 'f1'}}
13 #endif
14 
15 struct B1 {};
16 struct D1 : virtual B1 { // expected-note {{virtual base class declared here}}
D1D117     [[msvc::constexpr]] D1() {} // expected-error {{constexpr constructor not allowed in struct with virtual base class}}
18 };
19 
20 struct [[msvc::constexpr]] S2{}; // expected-error {{'constexpr' attribute only applies to functions and return statements}}
21 
22 // Check invalid code mixed with valid code
23 
f4(int x)24 [[msvc::constexpr]] int f4(int x) { return x > 1 ? 1 + f4(x / 2) : 0; } // expected-note {{non-constexpr function 'f4' cannot be used in a constant expression}} \
25                                                                         // expected-note {{declared here}} \
26                                                                         // expected-note {{declared here}} \
27                                                                         // expected-note {{declared here}}
f5()28 constexpr bool f5() { [[msvc::constexpr]] return f4(32) == 5; } // expected-note {{in call to 'f4(32)'}}
29 static_assert(f5()); // expected-error {{static assertion expression is not an integral constant expression}} \
30                      // expected-note {{in call to 'f5()'}}
31 
f6(int x)32 int f6(int x) { [[msvc::constexpr]] return x > 1 ? 1 + f6(x / 2) : 0; } // expected-note {{declared here}} \
33                                                                         // expected-note {{declared here}}
f7()34 constexpr bool f7() { [[msvc::constexpr]] return f6(32) == 5; } // expected-error {{constexpr function never produces a constant expression}} \
35                                                                 // expected-note {{non-constexpr function 'f6' cannot be used in a constant expression}} \
36                                                                 // expected-note {{non-constexpr function 'f6' cannot be used in a constant expression}}
37 static_assert(f7()); // expected-error {{static assertion expression is not an integral constant expression}} \
38                      // expected-note {{in call to 'f7()'}}
39 
f8()40 constexpr bool f8() { // expected-error {{constexpr function never produces a constant expression}}
41     [[msvc::constexpr]] f4(32); // expected-error {{'constexpr' attribute only applies to functions and return statements}} \
42                                 // expected-note {{non-constexpr function 'f4' cannot be used in a constant expression}} \
43                                 // expected-note {{non-constexpr function 'f4' cannot be used in a constant expression}}
44     [[msvc::constexpr]] int i5 = f4(32); // expected-error {{'constexpr' attribute only applies to functions and return statements}}
45     return i5 == 5;
46 }
47 static_assert(f8()); // expected-error {{static assertion expression is not an integral constant expression}} \
48                      // expected-note {{in call to 'f8()'}}
49 
50 #if __cplusplus == 201702L
vmS151 struct S1 { [[msvc::constexpr]] virtual bool vm() const { return true; } }; // expected-error {{attribute 'msvc::constexpr' ignored, it only applies to function definitions and return statements}}
52 #endif
53