Skip to content

Commit e5cb7da

Browse files
Keep C++11 constexpr templates usable after non-return statement warning [GH #356]
1 parent 4d11f84 commit e5cb7da

5 files changed

Lines changed: 79 additions & 6 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[GH #356]
2+
GNU/Clang compatibility: C++11 constexpr templates with non-return statements
3+
4+
When a statement that is not permitted in a C++11 constexpr function or
5+
constructor was accepted with a warning (for example, in a function or
6+
constructor template in some GNU or Clang C++11 modes), the function or
7+
constructor was nevertheless marked as unusable in constant expressions.
8+
That is now fixed: The function or constructor remains eligible for constant
9+
evaluation unless the diagnostic is issued as an error. For example, with
10+
--c++11 --gnu=150000:
11+
12+
template<typename T> struct S {
13+
T x;
14+
constexpr S(T i) : x(i) {
15+
if (sizeof(T) == 0) {} // Warning in C++11 mode.
16+
}
17+
};
18+
constexpr S<int> s(42); // Previously an error, now okay.

‎src/statements.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8147,6 +8147,10 @@ this statement was preceded by the GNU keyword __extension__.
81478147
if (current_rp->is_declared_constexpr || current_rp->is_consteval) {
81488148
/* Report an error if the statement is not one that is allowed in a
81498149
constexpr function or constexpr constructor. */
8150+
an_error_code err_code =
8151+
special_kind_is(current_rp, sfk_constructor) ?
8152+
ec_invalid_statement_in_constexpr_constructor :
8153+
ec_invalid_statement_in_constexpr_function;
81508154
if (current_rp->is_prototype_instantiation &&
81518155
in_constexpr_body_sev == es_error && !strict_ansi_mode) {
81528156
/* In uninstantiated templates common practice is to either not
@@ -8157,13 +8161,13 @@ this statement was preceded by the GNU keyword __extension__.
81578161
current_rp->is_template_function) {
81588162
in_constexpr_body_sev = es_warning;
81598163
} /* if */
8160-
pos_diagnostic(in_constexpr_body_sev,
8161-
special_kind_is(current_rp, sfk_constructor) ?
8162-
ec_invalid_statement_in_constexpr_constructor :
8163-
ec_invalid_statement_in_constexpr_function,
8164-
&start_pos);
8164+
pos_diagnostic(in_constexpr_body_sev, err_code, &start_pos);
8165+
if (is_effective_error(err_code, in_constexpr_body_sev, &start_pos)) {
8166+
scope_stack[depth_innermost_function_scope].constexpr_ruled_out = TRUE;
8167+
} /* if */
8168+
} else {
8169+
scope_stack[depth_innermost_function_scope].constexpr_ruled_out = TRUE;
81658170
} /* if */
8166-
scope_stack[depth_innermost_function_scope].constexpr_ruled_out = TRUE;
81678171
} /* if */
81688172
/* Loop if we just got a label and not an actual statement. */
81698173
if (get_another_statement) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fe_only -DTEST_NUMBER=1 --c++11 --g++ --gnu_version=150000 Test_name.c
2+
"Test_name.c", line 9: warning: statement may not appear in a constexpr constructor
3+
if (sizeof(Tp) == 0) {}
4+
^
5+
6+
"Test_name.c", line 29: warning: statement may not appear in a constexpr function
7+
if (sizeof(T) == 0) {}
8+
^
9+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fe_only -DTEST_NUMBER=2 --c++11 --clang --clang_version=190000 Test_name.c
2+
"Test_name.c", line 9: warning: statement may not appear in a constexpr constructor
3+
if (sizeof(Tp) == 0) {}
4+
^
5+
6+
"Test_name.c", line 29: warning: statement may not appear in a constexpr function
7+
if (sizeof(T) == 0) {}
8+
^
9+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//type:fp
2+
//options:--c++11 --g++ --gnu_version=150000:--c++11 --clang --clang_version=190000
3+
//remark:[GH #356] C++11 constexpr template accepted with a warning remains usable in constant expressions.
4+
5+
template <class Tp>
6+
struct A {
7+
Tp x;
8+
constexpr A(Tp i) : x(i) {
9+
if (sizeof(Tp) == 0) {}
10+
}
11+
};
12+
13+
template <int X>
14+
struct V {
15+
static const int x = X;
16+
};
17+
18+
template <class T, class U>
19+
constexpr int test_it(U u) {
20+
return ((void)T(u), 1);
21+
}
22+
23+
constexpr A<int> a(5);
24+
static_assert(a.x == 5, "");
25+
static_assert(V<test_it<A<int> >(5)>::x == 1, "");
26+
27+
template <class T>
28+
constexpr int f(T x) {
29+
if (sizeof(T) == 0) {}
30+
return x + 1;
31+
}
32+
33+
static_assert(f(41) == 42, "");

0 commit comments

Comments
 (0)