Skip to content

Commit bc851d1

Browse files
Evaluate the arguments of a constructor call before checking the constructor in constant evaluation [GH #527, GH #494]
1 parent e88c89d commit bc851d1

7 files changed

Lines changed: 194 additions & 76 deletions

File tree

‎src/interpret.c‎

Lines changed: 82 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25854,83 +25854,23 @@ compound statement of that handler.
2585425854
goto continue_mem_inits;
2585525855
} /* if */
2585625856

25857-
/* Retrieve the routine scope, or issue an error. */
2585825857
if (callee == NULL) {
2585925858
info_with_pos(ec_constexpr_expression_cannot_be_interpreted, pos, ips);
2586025859
do_constexpr_fail(result);
2586125860
goto done;
25862-
} else if (!callee->is_constexpr) {
25863-
info_with_pos_sym(ec_constexpr_call_to_nonconstexpr_function,
25864-
pos, symbol_for(callee), ips);
25865-
do_constexpr_fail(result);
25866-
goto done;
25867-
} /* if */
25868-
if (!callee->defined) {
25869-
if (has_routine_definition_from_module(callee)) {
25870-
/* The definition was stored in a module file. */
25871-
if (!load_routine_definition_from_module(callee)) {
25872-
ips->input_error = TRUE;
25873-
do_constexpr_fail(result);
25874-
} /* if */
25875-
} else if ((callee->is_inheriting_ctor || callee->compiler_generated) &&
25876-
!callee->is_deleted) {
25877-
force_definition_of_compiler_generated_routine(callee);
25878-
} else {
25879-
set_instance_required(symbol_for(callee), TRUE, SIR_CONSTANT_CONTEXT);
25880-
} /* if */
25881-
} /* if */
25882-
if (callee->function_def_number == NULL_function_def_number) {
25883-
info_with_pos_sym(ec_constexpr_function_undefined, pos,
25884-
symbol_for(callee), ips);
25885-
do_constexpr_fail(result);
25886-
if (callee->is_deleted && ips->is_constant_evaluated) {
25887-
/* An error has presumably been issued earlier (use of a deleted
25888-
function). Treat this as an input error to avoid extraneous
25889-
diagnostics. */
25890-
expect_error();
25891-
ips->input_error = TRUE;
25892-
} /* if */
25893-
goto done;
25894-
} else if (callee->is_prototype_instantiation) {
25895-
info_with_pos_sym(ec_constexpr_call_not_interpretable, pos,
25896-
symbol_for(callee), ips);
25897-
do_constexpr_fail(result);
25898-
goto done;
25899-
} else if (cost_exceeded(ips)) {
25900-
report_excessive_cost(ips);
25901-
do_constexpr_fail(result);
25902-
goto done;
2590325861
} else {
2590425862
a_byte *result_storage = cap.address;
2590525863
a_byte *complete_object = cap.complete_object;
25906-
a_scope_ptr callee_scope = scope_for_routine(callee);
25864+
a_scope_ptr callee_scope;
2590725865
an_expr_node_ptr args = dip->variant.constructor.args, arg;
25908-
a_variable_ptr params = callee_scope->variant.routine.parameters,
25909-
param, this_var;
25866+
a_variable_ptr params, param, this_var;
2591025867
a_base_class_ptr bcp;
2591125868
a_byte_count n_args = 1, n_params = 1;
2591225869
a_byte_count *arg_size;
2591325870
a_byte *arg_ptrs, **p_arg_ptr, *this_bytes, *arg_sizes;
2591425871
an_alloc_seq_number alloc_seq_number;
2591525872
a_type_ptr class_type = parent_class_of(callee);
2591625873
unsigned long up_front_cost;
25917-
/* Don't attempt to interpret a non-constexpr function. The flag
25918-
scope->is_constexpr_routine is set at the end of a constexpr function
25919-
definition, so this also prevents the interpretation of a function that
25920-
is not fully parsed (e.g., requested due to a recursive call in a
25921-
constexpr function). */
25922-
if (!callee_scope->is_constexpr_routine) {
25923-
do_constexpr_fail(result);
25924-
info_with_pos_sym(ec_constexpr_call_not_interpretable, pos,
25925-
symbol_for(callee), ips);
25926-
goto done;
25927-
} /* if */
25928-
if (class_type->variant.class_struct_union.any_virtual_base_classes) {
25929-
do_constexpr_fail(result);
25930-
info_with_pos_type(ec_constexpr_object_with_virtual_base, pos,
25931-
class_type, ips);
25932-
goto done;
25933-
} /* if */
2593425874
/* Set up arguments, starting with "this" if applicable. */
2593525875
/* This process must happen in two phases. First, the arguments must be
2593625876
allocated and evaluated. Only then can we map parameter variables onto
@@ -25948,17 +25888,9 @@ compound statement of that handler.
2594825888
result = FALSE;
2594925889
goto done;
2595025890
} /* if */
25951-
/* Count the parameters (including "this") to make sure there are enough
25952-
arguments for the parameters. */
25953-
for (param = params; param != NULL; param = param->next) {
25954-
n_params += 1;
25955-
} /* if */
25956-
if (n_args < n_params) {
25957-
info_with_pos(ec_too_few_arguments, pos, ips);
25958-
do_constexpr_fail(result);
25959-
goto done;
25960-
} /* if */
25961-
/* Phase 1: Allocate and evaluate the arguments. */
25891+
/* Phase 1: Allocate and evaluate the arguments. That precedes the checks
25892+
of the constructor below: If the evaluation of an argument exits by
25893+
throwing an exception, the constructor is not invoked. */
2596225894
/* The "+1" below is to account for the "this" pointer (handled later). */
2596325895
p_arg_ptr = (a_byte**)arg_ptrs+1;
2596425896
arg_size = (a_byte_count*)arg_sizes;
@@ -25967,13 +25899,16 @@ compound statement of that handler.
2596725899
/* The first non-this argument is implicit (i.e., not represented in
2596825900
the IL). Since it always corresponds to the reference parameter of
2596925901
a copy/move constructor, we know it is a constexpr address. */
25902+
a_type_ptr routine_type = skip_typerefs(callee->type);
25903+
a_type_ptr param_type =
25904+
rout_type_supp(routine_type)->param_type_list->type;
2597025905
a_byte_count n_bytes = sizeof(a_constexpr_address);
2597125906
a_byte *arg_bytes;
2597225907
do_host_alignment(&n_bytes);
2597325908
*arg_size = n_bytes;
2597425909
arg_size += 1;
2597525910
n_bytes += sizeof(a_var_postfix);
25976-
if (!alloc_complete_object(ips, n_bytes, params->type, arg_bytes)) {
25911+
if (!alloc_complete_object(ips, n_bytes, param_type, arg_bytes)) {
2597725912
goto done;
2597825913
} /* if */
2597925914
*(a_constexpr_address*)arg_bytes = *implied_src;
@@ -26036,6 +25971,79 @@ compound statement of that handler.
2603625971
goto done;
2603725972
} /* if */
2603825973
} /* for */
25974+
/* Check that the constructor can be interpreted, and retrieve its routine
25975+
scope. */
25976+
if (!callee->is_constexpr) {
25977+
info_with_pos_sym(ec_constexpr_call_to_nonconstexpr_function,
25978+
pos, symbol_for(callee), ips);
25979+
do_constexpr_fail(result);
25980+
goto done;
25981+
} /* if */
25982+
if (!callee->defined) {
25983+
if (has_routine_definition_from_module(callee)) {
25984+
/* The definition was stored in a module file. */
25985+
if (!load_routine_definition_from_module(callee)) {
25986+
ips->input_error = TRUE;
25987+
do_constexpr_fail(result);
25988+
} /* if */
25989+
} else if ((callee->is_inheriting_ctor || callee->compiler_generated) &&
25990+
!callee->is_deleted) {
25991+
force_definition_of_compiler_generated_routine(callee);
25992+
} else {
25993+
set_instance_required(symbol_for(callee), TRUE, SIR_CONSTANT_CONTEXT);
25994+
} /* if */
25995+
} /* if */
25996+
if (callee->function_def_number == NULL_function_def_number) {
25997+
info_with_pos_sym(ec_constexpr_function_undefined, pos,
25998+
symbol_for(callee), ips);
25999+
do_constexpr_fail(result);
26000+
if (callee->is_deleted && ips->is_constant_evaluated) {
26001+
/* An error has presumably been issued earlier (use of a deleted
26002+
function). Treat this as an input error to avoid extraneous
26003+
diagnostics. */
26004+
expect_error();
26005+
ips->input_error = TRUE;
26006+
} /* if */
26007+
goto done;
26008+
} else if (callee->is_prototype_instantiation) {
26009+
info_with_pos_sym(ec_constexpr_call_not_interpretable, pos,
26010+
symbol_for(callee), ips);
26011+
do_constexpr_fail(result);
26012+
goto done;
26013+
} else if (cost_exceeded(ips)) {
26014+
report_excessive_cost(ips);
26015+
do_constexpr_fail(result);
26016+
goto done;
26017+
} /* if */
26018+
callee_scope = scope_for_routine(callee);
26019+
params = callee_scope->variant.routine.parameters;
26020+
/* Don't attempt to interpret a non-constexpr function. The flag
26021+
scope->is_constexpr_routine is set at the end of a constexpr function
26022+
definition, so this also prevents the interpretation of a function that
26023+
is not fully parsed (e.g., requested due to a recursive call in a
26024+
constexpr function). */
26025+
if (!callee_scope->is_constexpr_routine) {
26026+
do_constexpr_fail(result);
26027+
info_with_pos_sym(ec_constexpr_call_not_interpretable, pos,
26028+
symbol_for(callee), ips);
26029+
goto done;
26030+
} /* if */
26031+
if (class_type->variant.class_struct_union.any_virtual_base_classes) {
26032+
do_constexpr_fail(result);
26033+
info_with_pos_type(ec_constexpr_object_with_virtual_base, pos,
26034+
class_type, ips);
26035+
goto done;
26036+
} /* if */
26037+
/* Count the parameters (including "this") to make sure there are enough
26038+
arguments for the parameters. */
26039+
for (param = params; param != NULL; param = param->next) {
26040+
n_params += 1;
26041+
} /* if */
26042+
if (n_args < n_params) {
26043+
info_with_pos(ec_too_few_arguments, pos, ips);
26044+
do_constexpr_fail(result);
26045+
goto done;
26046+
} /* if */
2603926047
/* Phase 2: Map the parameters to the arguments. */
2604026048
/* Associate with the parameter variables a new allocation number. For
2604126049
ordinary calls, we just use the allocation number about to be created

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx0x-initializer-stdinitializerlist.rto/default.1.1.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
112112
"Test_name.c", line 314: error: expression must have a constant value
113113
constexpr int k = f( // expected-error {{must be initialized by a constant expression}}
114114
^
115-
"Test_name.c", line 317: note: cannot call non-constexpr function "TemporaryInitListSourceRange_PR22367::A::A(std::initializer_list<int>)" (declared at line 311)
115+
"Test_name.c", line 317: note: cannot call non-constexpr function "std::initializer_list<_E>::initializer_list(const _E *, std::size_t) [with _E=int]" (declared at line 34)
116116
std::initializer_list // expected-note {{constructor}}
117117
^
118118

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx0x-initializer-stdinitializerlist.rto/default.2.1.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
112112
"Test_name.c", line 314: error: expression must have a constant value
113113
constexpr int k = f( // expected-error {{must be initialized by a constant expression}}
114114
^
115-
"Test_name.c", line 317: note: cannot call non-constexpr function "TemporaryInitListSourceRange_PR22367::A::A(std::initializer_list<int>)" (declared at line 311)
115+
"Test_name.c", line 317: note: cannot call non-constexpr function "std::initializer_list<_E>::initializer_list(const _E *, std::size_t) [with _E=int]" (declared at line 34)
116116
std::initializer_list // expected-note {{constructor}}
117117
^
118118

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fe_only -DTEST_NUMBER=1 --c++26 Test_name.c
2+
"Test_name.c", line 17: error: expression must have a constant value
3+
static_assert(f(1) == 0); // Error: S::S(int) is not constexpr.
4+
^
5+
"Test_name.c", line 13: note: cannot call non-constexpr function "S::S(int)" (declared at line 8)
6+
try { S(h(n)); } catch (int i) { return i; }
7+
^
8+
9+
"Test_name.c", line 25: error: expression must have a constant value
10+
static_assert(g() == 0); // Error: *p is not a constant.
11+
^
12+
"Test_name.c", line 22: note: attempt to access run-time storage
13+
S(h(*p));
14+
^
15+
16+
2 errors detected in the compilation of "Test_name.c".
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++26 Test_name.c
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//type:fn
2+
//options_all:--c++26
3+
//remark:Non-constexpr constructor call with a non-throwing argument
4+
5+
constexpr int h(int n) { if (n < 0) throw n; return n; }
6+
7+
struct S {
8+
S(int) {}
9+
constexpr S(double) {}
10+
};
11+
12+
constexpr int f(int n) {
13+
try { S(h(n)); } catch (int i) { return i; }
14+
return 0;
15+
}
16+
static_assert(f(-1) == -1);
17+
static_assert(f(1) == 0); // Error: S::S(int) is not constexpr.
18+
19+
// An argument that is not a constant is diagnosed first.
20+
constexpr int g() {
21+
int* p = nullptr;
22+
S(h(*p));
23+
return 0;
24+
}
25+
static_assert(g() == 0); // Error: *p is not a constant.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//type:fp
2+
//options_all:--c++26
3+
//remark:Non-constexpr constructor call whose argument throws in constexpr
4+
5+
// The arguments of a constructor call are evaluated before the constructor is
6+
// invoked: If an argument exits by throwing an exception, the constructor is
7+
// not invoked, so it doesn't matter that it isn't constexpr.
8+
constexpr int h(int n) { throw n; }
9+
10+
struct S {
11+
S(int) {}
12+
constexpr S(double) {}
13+
};
14+
15+
constexpr int temporary() {
16+
try { S(h(1)); } catch (int i) { return i; }
17+
return 0;
18+
}
19+
static_assert(temporary() == 1);
20+
21+
constexpr int variable() {
22+
try { S s(h(2)); } catch (int i) { return i; }
23+
return 0;
24+
}
25+
static_assert(variable() == 2);
26+
27+
constexpr int new_expression() {
28+
try { delete new S(h(3)); } catch (int i) { return i; }
29+
return 0;
30+
}
31+
static_assert(new_expression() == 3);
32+
33+
struct M {
34+
S s;
35+
constexpr M() : s(h(4)) {}
36+
};
37+
constexpr int member() {
38+
try { M m; } catch (int i) { return i; }
39+
return 0;
40+
}
41+
static_assert(member() == 4);
42+
43+
struct D : S {
44+
constexpr D() : S(h(5)) {}
45+
};
46+
constexpr int base() {
47+
try { D d; } catch (int i) { return i; }
48+
return 0;
49+
}
50+
static_assert(base() == 5);
51+
52+
// The arguments evaluated before the one that throws are destroyed. (The
53+
// braces make the arguments be evaluated in order.)
54+
struct L {
55+
int* p;
56+
constexpr L(int* p) : p(p) {}
57+
constexpr ~L() { ++*p; }
58+
};
59+
struct T {
60+
T(const L&, int) {}
61+
constexpr T(double) {}
62+
};
63+
constexpr int destroyed() {
64+
int n = 0;
65+
try { T{L(&n), h(6)}; } catch (int i) { return n * 10 + i; }
66+
return 0;
67+
}
68+
static_assert(destroyed() == 16);

0 commit comments

Comments
 (0)