Skip to content

Commit f2d5bfa

Browse files
committed
[clang] add -fimplicit-constexpr flag: add more tests
1 parent 77403d7 commit f2d5bfa

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

Diff for: clang/test/Sema/implicit-constexpr-chain.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// RUN: %clang_cc1 -verify=BEFORE -std=c++23 %s
2+
// RUN: %clang_cc1 -verify=AFTER -std=c++23 %s -fimplicit-constexpr
3+
4+
// AFTER-no-diagnostics
5+
6+
// FOLLOWING TWO EXAMPLES ESTABLISH THE `-fimplicit-constexpr` allows enter to constant evaluation for functions
7+
// which would be disabled based on:
8+
// [expr.const]#10.3 "an invocation of a non-constexpr function" (is not allowed)
9+
10+
// -------------------
11+
12+
inline int normal_function() {
13+
// BEFORE-note@-1 {{declared here}}
14+
return 42;
15+
}
16+
17+
constinit auto cinit = normal_function();
18+
// BEFORE-error@-1 {{variable does not have a constant initializer}}
19+
// BEFORE-note@-2 {{required by 'constinit' specifier here}}
20+
// BEFORE-note@-3 {{non-constexpr function 'normal_function' cannot be used in a constant expression}}
21+
22+
// -------------------
23+
24+
inline int still_normal_function() {
25+
// BEFORE-note@-1 {{declared here}}
26+
return 42;
27+
}
28+
29+
constexpr auto cxpr = still_normal_function();
30+
// BEFORE-error@-1 {{constexpr variable 'cxpr' must be initialized by a constant expression}}
31+
// BEFORE-note@-2 {{non-constexpr function 'still_normal_function' cannot be used in a constant expression}}
32+
33+
// -------------------
34+
35+
// Following example shows calling non-constexpr marked function in
36+
// constant evaluated context is no longer an error.
37+
38+
struct type_with_nonconstexpr_static_function {
39+
static /* non-constexpr */ int square(int v) {
40+
// BEFORE-note@-1 {{declared here}}
41+
return v*v;
42+
}
43+
44+
int value;
45+
constexpr type_with_nonconstexpr_static_function(int x): value{square(x)} { }
46+
// BEFORE-note@-1 {{non-constexpr function 'square' cannot be used in a constant expression}}
47+
// ^ (Hana's note: during evaluation)
48+
};
49+
50+
constexpr auto force_ce = type_with_nonconstexpr_static_function{4};
51+
// BEFORE-error@-1 {{constexpr variable 'force_ce' must be initialized by a constant expression}}
52+
// BEFORE-note@-2 {{in call to 'type_with_nonconstexpr_static_function(4)'}}
53+
54+
55+
// this is fine: as it's in runtime, where the constructor
56+
// is called in runtime, like a normal function, so it doesn't matter `square` is not constexpr
57+
static auto static_var = type_with_nonconstexpr_static_function{4};
58+
59+
60+
61+
// -------------------
62+
63+
// Following example shows now you can call non-constexpr marked even
64+
// from consteval function initiated constant evaluation.
65+
66+
inline int runtime_only_function() {
67+
// BEFORE-note@-1 {{declared here}}
68+
return 11;
69+
}
70+
71+
constexpr int constexpr_function() {
72+
return runtime_only_function();
73+
// BEFORE-note@-1 {{non-constexpr function 'runtime_only_function' cannot be used in a constant expression}}
74+
}
75+
76+
consteval int consteval_function() {
77+
return constexpr_function();
78+
// BEFORE-note@-1 {{in call to 'constexpr_function()'}}
79+
}
80+
81+
static int noncalled_runtime_function() {
82+
// we enter consteval context here to replace `consteval_function()` with a constant.
83+
// this is happen during parsing!!
84+
return consteval_function();
85+
// BEFORE-error@-1 {{call to consteval function 'consteval_function' is not a constant expression}}
86+
// BEFORE-note@-2 {{in call to 'consteval_function()'}}
87+
}
88+

0 commit comments

Comments
 (0)