xref: /llvm-project/clang/test/SemaCXX/cxx2a-consteval-default-params.cpp (revision d773c00e52f1acd68267c6f2f5bfa269b73810a0)
1ca619613SCorentin Jabot // RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
2ba15d186SMark de Wever // RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 %s
3ca619613SCorentin Jabot 
446768852SCorentin Jabot consteval int undefined();  // expected-note 2 {{declared here}}
5ca619613SCorentin Jabot 
check_lambdas_unused(int a=[](int no_error=undefined ()){}(0),int b=[](int defaulted=undefined ()){}())6ca619613SCorentin Jabot void check_lambdas_unused(
746768852SCorentin Jabot     int a = [](int no_error = undefined()) {
8ca619613SCorentin Jabot         return no_error;
9ca619613SCorentin Jabot     }(0),
__anon87c0ec230202(int defaulted = undefined()) 1046768852SCorentin Jabot     int b = [](int defaulted = undefined()) {
11ca619613SCorentin Jabot         return defaulted;
12ca619613SCorentin Jabot     }()
13ca619613SCorentin Jabot ) {}
14ca619613SCorentin Jabot 
check_lambdas_used(int b=[](int no_error=undefined ()){}(0),int c=[](int defaulted=undefined ()){}(),int d=[](int defaulted=sizeof (undefined ())){}())15ca619613SCorentin Jabot int check_lambdas_used(
16ca619613SCorentin Jabot     int b = [](int no_error = undefined()) {
17ca619613SCorentin Jabot         return no_error;
18ca619613SCorentin Jabot     }(0),
__anon87c0ec230402(int defaulted = undefined()) 19ca619613SCorentin Jabot     int c = [](int defaulted = undefined()) { // expected-error {{not a constant expression}} \
20ca619613SCorentin Jabot                               // expected-note  {{declared here}} \
21ca619613SCorentin Jabot                               // expected-note  {{undefined function 'undefined'}}
22ca619613SCorentin Jabot         return defaulted;
235f038e0eSCorentin Jabot     }(),  // expected-note {{in the default initializer of 'defaulted'}}
__anon87c0ec230502(int defaulted = sizeof(undefined())) 24ca619613SCorentin Jabot     int d = [](int defaulted = sizeof(undefined())) {
25ca619613SCorentin Jabot         return defaulted;
26ca619613SCorentin Jabot     }()
27ca619613SCorentin Jabot ) {
28ca619613SCorentin Jabot     return 0;
29ca619613SCorentin Jabot }
30ca619613SCorentin Jabot 
31ca619613SCorentin Jabot int test_check_lambdas_used = check_lambdas_used();
32ca619613SCorentin Jabot 
33ca619613SCorentin Jabot struct UnusedInitWithLambda {
__anon87c0ec230602UnusedInitWithLambda34ca619613SCorentin Jabot     int a = [] {
3546768852SCorentin Jabot         return undefined(); // never evaluated because immediate escalating
36ca619613SCorentin Jabot     }();
37ca619613SCorentin Jabot     // UnusedInitWithLambda is never constructed, so the initializer
38ca619613SCorentin Jabot     // of b and undefined() are never evaluated.
__anon87c0ec230702UnusedInitWithLambda39ca619613SCorentin Jabot     int b = [](int no_error = undefined()) {
40ca619613SCorentin Jabot         return no_error;
41ca619613SCorentin Jabot     }();
42ca619613SCorentin Jabot };
43ca619613SCorentin Jabot 
ub(int n)448698262aSCorentin Jabot consteval int ub(int n) {
4546768852SCorentin Jabot     return 0/n;
46ca619613SCorentin Jabot }
47ca619613SCorentin Jabot 
488698262aSCorentin Jabot struct InitWithLambda { // expected-note {{'InitWithLambda' is an immediate constructor because the default initializer of 'b' contains a call to a consteval function 'undefined' and that call is not a constant expression}}
__anon87c0ec230802InitWithLambda498698262aSCorentin Jabot     int b = [](int error = undefined()) {  // expected-note {{undefined function 'undefined' cannot be used in a constant expression}}
50ca619613SCorentin Jabot         return error;
5146768852SCorentin Jabot     }();
__anon87c0ec230902InitWithLambda528698262aSCorentin Jabot     int c = [](int error = sizeof(undefined()) + ub(0)) {
5346768852SCorentin Jabot 
54ca619613SCorentin Jabot         return error;
5546768852SCorentin Jabot     }();
5646768852SCorentin Jabot } i;
578698262aSCorentin Jabot // expected-error@-1 {{call to immediate function 'InitWithLambda::InitWithLambda' is not a constant expression}} \
588698262aSCorentin Jabot    expected-note@-1 {{in call to 'InitWithLambda()'}}
59ca619613SCorentin Jabot 
60ca619613SCorentin Jabot namespace ShouldNotCrash {
61ca619613SCorentin Jabot     template<typename T>
62ca619613SCorentin Jabot     struct F {
63ca619613SCorentin Jabot         template<typename U>
FShouldNotCrash::F64ca619613SCorentin Jabot         F(const U&) {}
65ca619613SCorentin Jabot     };
66ca619613SCorentin Jabot     struct A {
__anon87c0ec230a02ShouldNotCrash::A67ca619613SCorentin Jabot         static constexpr auto x = [] {};
68ca619613SCorentin Jabot         F<int> f = x;
69ca619613SCorentin Jabot     };
f(A a=A ())70ca619613SCorentin Jabot     void f(A a = A()) { }
71ca619613SCorentin Jabot }
7274fd474eSMariya Podchishchaeva 
7374fd474eSMariya Podchishchaeva namespace GH62224 {
7474fd474eSMariya Podchishchaeva   consteval int fwd();
7574fd474eSMariya Podchishchaeva   template <int i = fwd()>
7674fd474eSMariya Podchishchaeva   struct C {
CGH62224::C7774fd474eSMariya Podchishchaeva     consteval C(int = fwd()) { }
getGH62224::C7874fd474eSMariya Podchishchaeva     consteval int get() { return i; }
7974fd474eSMariya Podchishchaeva   };
8074fd474eSMariya Podchishchaeva 
fwd()8174fd474eSMariya Podchishchaeva   consteval int fwd() { return 42; }
8274fd474eSMariya Podchishchaeva   C<> Val; // No error since fwd is defined already.
8374fd474eSMariya Podchishchaeva   static_assert(Val.get() == 42);
8474fd474eSMariya Podchishchaeva }
85*d773c00eScor3ntin 
86*d773c00eScor3ntin namespace GH80630 {
87*d773c00eScor3ntin 
ce()88*d773c00eScor3ntin consteval const char* ce() { return "Hello"; }
89*d773c00eScor3ntin 
f2(const char * loc=[](char const * fn){}(ce ()))90*d773c00eScor3ntin auto f2(const char* loc = []( char const* fn )
91*d773c00eScor3ntin     { return fn; }  ( ce() ) ) {
92*d773c00eScor3ntin     return loc;
93*d773c00eScor3ntin }
94*d773c00eScor3ntin 
g()95*d773c00eScor3ntin auto g() {
96*d773c00eScor3ntin     return f2();
97*d773c00eScor3ntin }
98*d773c00eScor3ntin 
99*d773c00eScor3ntin }
100