Skip to content

Commit e5bc297

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

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

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

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
inline int runtime_only_function() {
64+
// BEFORE-note@-1 {{declared here}}
65+
return 11;
66+
}
67+
68+
constexpr int constexpr_function() {
69+
return runtime_only_function();
70+
// BEFORE-note@-1 {{non-constexpr function 'runtime_only_function' cannot be used in a constant expression}}
71+
}
72+
73+
consteval int consteval_function() {
74+
return constexpr_function();
75+
// BEFORE-note@-1 {{in call to 'constexpr_function()'}}
76+
}
77+
78+
static int noncalled_runtime_function() {
79+
// we enter consteval context here to replace `consteval_function()` with a constant.
80+
// this is happen during parsing!!
81+
return consteval_function();
82+
// BEFORE-error@-1 {{call to consteval function 'consteval_function' is not a constant expression}}
83+
// BEFORE-note@-2 {{in call to 'consteval_function()'}}
84+
}
85+

0 commit comments

Comments
 (0)