Skip to content

Commit ce6e8c8

Browse files
committed
Lifetime of temporaries in aggregate initialization [EDGcpfe/29057]
A temporary created in the initializer of an element of an aggregate was destroyed at the end of that element's initialization, instead of when the initialization of the whole aggregate is complete. Treat the complete braced initialization of an aggregate as one full-expression, including nested aggregate initializers and initializers resumed after parsing was suspended. Add regression tests, including members with nontrivial destructors and exceptions thrown during and after the initialization. gnu/cpp/cpp0x/initlist122, gnu/cpp/init/aggr7-eh and gnu/cpp/init/array12 now pass.
1 parent ad4cfc4 commit ce6e8c8

39 files changed

Lines changed: 146 additions & 1655 deletions

‎src/decl_inits.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4704,12 +4704,17 @@ initializer, already copied and substituted.
47044704
a_routine_ptr dtor_rp = NULL;
47054705
a_boolean need_to_free_icp_tree = FALSE;
47064706
a_boolean saved_reduce_backing_expression_use;
4707+
an_expr_stack_entry expr_stack_entry, *saved_expr_stack;
4708+
a_boolean full_expression;
47074709

47084710
check_assertion(rescan_aggr != NULL || curr_token == tok_lbrace ||
47094711
(dps != NULL &&
47104712
anything_cached(&dps->prescanned_initializer_cache)));
47114713
saved_reduce_backing_expression_use = reduce_backing_expression_use;
47124714
dtype = skip_typerefs(dtype);
4715+
full_expression = !C_mode() && is->elements_are_full_expressions &&
4716+
is_aggregate_type(dtype) &&
4717+
!(dps != NULL && dps->is_struct_binding_decl);
47134718
if (rescan_aggr != NULL) {
47144719
/* Rescan. The {...} is provided by the caller in init-component form. */
47154720
icp_tree = rescan_aggr;
@@ -4728,6 +4733,12 @@ initializer, already copied and substituted.
47284733
} /* if */
47294734
icp = icp_tree;
47304735
check_assertion(icp != NULL && is_braced_init_component(icp));
4736+
if (full_expression) {
4737+
/* All elements share the lifetime of the complete initialization. */
4738+
push_expr_stack_for_initializer(&expr_stack_entry, &saved_expr_stack,
4739+
ek_normal,
4740+
/*is_full_expr=*/TRUE, dps, is);
4741+
} /* if */
47314742
is_var_init = dps != NULL && dps->sym != NULL &&
47324743
variable_for_symbol(dps->sym) != NULL;
47334744
/* The force_dynamic_init flag only applies to the top-level result. */
@@ -4933,6 +4944,17 @@ initializer, already copied and substituted.
49334944
} /* if */
49344945
is->no_diagnostics = saved_no_diagnostics;
49354946
reduce_backing_expression_use = saved_reduce_backing_expression_use;
4947+
if (full_expression) {
4948+
if (is->init_dip != NULL) {
4949+
wrap_up_dynamic_init_full_expression(is->init_dip);
4950+
} else if (is->init_error) {
4951+
discard_curr_expr_object_lifetime();
4952+
} else if (is->init_con != NULL) {
4953+
wrap_up_constant_full_expression(is->init_con);
4954+
} /* if */
4955+
pop_expr_stack_for_initializer(saved_expr_stack,
4956+
/*is_full_expr=*/TRUE, dps, is);
4957+
} /* if */
49364958
} /* braced_initializer */
49374959

49384960

‎src/expr.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48016,11 +48016,13 @@ free the components prior to prev_icp.
4801648016
an_expr_stack_entry *saved_expr_stack;
4801748017
an_expr_stack_entry expr_stack_entry;
4801848018
an_object_lifetime *saved_object_lifetime = curr_object_lifetime;
48019+
a_boolean saved_full_expressions;
4801948020

4802048021
check_assertion(is_continuation_elem(prev_icp->next));
4802148022
continuation = prev_icp->next->variant.continuation.state;
4802248023
dps = continuation->decl_parse_state;
4802348024
check_assertion(dps != NULL && dps->init_state.pending_elements);
48025+
saved_full_expressions = dps->init_state.elements_are_full_expressions;
4802448026
if (continuation->resumable) {
4802548027
/* Free most init components preceding prev_icp, but keep the first one in
4802648028
case it is useful for diagnostic purposes. */
@@ -48055,6 +48057,7 @@ free the components prior to prev_icp.
4805548057
/*bundle=*/TRUE, &continuation);
4805648058
pop_expr_stack_for_initializer(saved_expr_stack, /*is_full_expr=*/TRUE,
4805748059
dps, (an_init_state *)NULL);
48060+
dps->init_state.elements_are_full_expressions = saved_full_expressions;
4805848061
if (continuation != NULL) {
4805948062
/* Parsing was suspended again. Record additional state information to
4806048063
enable a subsequent call to this function. */

‎src/lower_init.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11048,6 +11048,8 @@ do_assignment:;
1104811048
/* coverity[uninit_use] */
1104911049
if ((dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate ||
1105011050
dip->kind == (a_dynamic_init_kind)dik_lambda) &&
11051+
/* Inner partial cleanups are handled by add_dyn_init_cleanup. */
11052+
!dip->overlaps_temps_in_inner_lifetime &&
1105111053
latest_initialization_on_entry !=
1105211054
eff_context->latest_initialization) {
1105311055
/* This is an aggregate for which some partial-aggregate
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[GH #458]
2+
Lifetime of temporaries in aggregate initialization
3+
4+
In C++, a temporary created in the initializer of an element of an aggregate
5+
was destroyed at the end of that element's initialization, instead of at the
6+
end of the full-expression containing the complete initialization. It is now
7+
destroyed when the initialization of the whole aggregate is complete,
8+
including for nested aggregate initializers. For example:
9+
10+
struct Temp { Temp(); ~Temp(); };
11+
struct A { A(const Temp&); };
12+
struct Pair { A a; int b; };
13+
int f();
14+
Pair p{A(Temp{}), f()}; // The Temp is now still alive when f is called.

‎tests/expectations/edg_x86_64/imported.log‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6647,7 +6647,6 @@ gnu/cpp/cpp0x/initlist-opt2.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
66476647
gnu/cpp/cpp0x/initlist-ref1.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
66486648
gnu/cpp/cpp0x/initlist-ref3.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
66496649
gnu/cpp/cpp0x/initlist-value4.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:FAIL:
6650-
gnu/cpp/cpp0x/initlist122.sft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL:
66516650
gnu/cpp/cpp0x/initlist13.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
66526651
gnu/cpp/cpp0x/initlist36.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:FAIL:
66536652
gnu/cpp/cpp0x/initlist55.sft.cpp:s:-DTEST_NUMBER=1 --c++11 -W:SKIPPED:
@@ -7725,10 +7724,8 @@ gnu/cpp/inherit/pr97268.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:FAIL:
77257724
gnu/cpp/inherit/using4.sft.cpp:fn:-DTEST_NUMBER=1:FAIL:
77267725
gnu/cpp/init/addr-const1.sft.cpp:rp:-DTEST_NUMBER=1:FAIL:
77277726
gnu/cpp/init/aggr4.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:FAIL:
7728-
gnu/cpp/init/aggr7-eh.sft.cpp:rp:-DTEST_NUMBER=1:FAIL:
77297727
gnu/cpp/init/aggr7-eh2.sft.cpp:rp:-DTEST_NUMBER=1:FAIL:
77307728
gnu/cpp/init/aggr7-eh3.sft.cpp:rp:-DTEST_NUMBER=1:FAIL:
7731-
gnu/cpp/init/array12.sft.cpp:rp:-DTEST_NUMBER=1:FAIL:
77327729
gnu/cpp/init/array44.sft.cpp:fn:-DTEST_NUMBER=1:FAIL:
77337730
gnu/cpp/init/brace4.sft.cpp:fn:-DTEST_NUMBER=1:FAIL:
77347731
gnu/cpp/init/copy3.sft.cpp:rp:-DTEST_NUMBER=1 --c++14:FAIL:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
./a.out
2+
~DTemp
23
~A
34
~A
45
~BTemp
5-
~DTemp
66
~BTemp

‎tests/tests/imported/clang/cpp/SemaCXX/.source_location.rto/default.1.1.txt‎

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -135,80 +135,6 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
135135
check(t.x, " T", 0);
136136
^
137137

138-
"test_passed.cpp", line 4301: error: static assertion failed with ""
139-
static_assert(b.a.n == 4300, "");
140-
^
141-
"test_passed.cpp", line 4301: note: the final comparison was 4201 == 4300
142-
static_assert(b.a.n == 4300, "");
143-
^
144-
145-
"test_passed.cpp", line 4302: error: static assertion failed with ""
146-
static_assert(b.a.n2 == 4300, "");
147-
^
148-
"test_passed.cpp", line 4302: note: the final comparison was 4201 == 4300
149-
static_assert(b.a.n2 == 4300, "");
150-
^
151-
152-
"test_passed.cpp", line 4303: error: static assertion failed with ""
153-
static_assert(b.a.info.line() == 4300, "");
154-
^
155-
"test_passed.cpp", line 4303: note: the final comparison was 4201 == 4300
156-
static_assert(b.a.info.line() == 4300, "");
157-
^
158-
159-
"test_passed.cpp", line 4304: error: static assertion failed
160-
static_assert(is_equal(b.a.f, "test_passed.cpp"));
161-
^
162-
163-
"test_passed.cpp", line 4305: error: static assertion failed
164-
static_assert(is_equal(b.a.f2, "test_passed.cpp"));
165-
^
166-
167-
"test_passed.cpp", line 4306: error: static assertion failed
168-
static_assert(is_equal(b.a.info.file(), "test_passed.cpp"));
169-
^
170-
171-
"test_func_passed.cpp", line 4401: error: static assertion failed with ""
172-
static_assert(b.a.n == 4400, "");
173-
^
174-
"test_func_passed.cpp", line 4401: note: the final comparison was 4201 == 4400
175-
static_assert(b.a.n == 4400, "");
176-
^
177-
178-
"test_func_passed.cpp", line 4402: error: static assertion failed with ""
179-
static_assert(b.a.n2 == 4400, "");
180-
^
181-
"test_func_passed.cpp", line 4402: note: the final comparison was 4201 == 4400
182-
static_assert(b.a.n2 == 4400, "");
183-
^
184-
185-
"test_func_passed.cpp", line 4403: error: static assertion failed with ""
186-
static_assert(b.a.info.line() == 4400, "");
187-
^
188-
"test_func_passed.cpp", line 4403: note: the final comparison was 4201 == 4400
189-
static_assert(b.a.info.line() == 4400, "");
190-
^
191-
192-
"test_func_passed.cpp", line 4404: error: static assertion failed
193-
static_assert(is_equal(b.a.f, "test_func_passed.cpp"));
194-
^
195-
196-
"test_func_passed.cpp", line 4405: error: static assertion failed
197-
static_assert(is_equal(b.a.f2, "test_func_passed.cpp"));
198-
^
199-
200-
"test_func_passed.cpp", line 4406: error: static assertion failed
201-
static_assert(is_equal(b.a.info.file(), "test_func_passed.cpp"));
202-
^
203-
204-
"test_func_passed.cpp", line 4407: error: static assertion failed
205-
static_assert(is_equal(b.a.func, "test_in_func"));
206-
^
207-
208-
"test_func_passed.cpp", line 4408: error: static assertion failed
209-
static_assert(is_equal(b.a.func2, "test_in_func"));
210-
^
211-
212138
"test_func_passed.cpp", line 4409: error: static assertion failed
213139
static_assert(is_equal(b.a.info.function(), "bool test_out_of_line_init::test_in_func()"));
214140
^
@@ -239,4 +165,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
239165
^
240166
detected during instantiation of "Pair<U, U> test_func::test_func_template(T, U) [with T=int, U=SL]" at line 361
241167

242-
39 errors detected in the compilation of "Test_name.c".
168+
25 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/SemaCXX/.source_location.rto/default.10.1.txt‎

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -145,80 +145,6 @@ fe_only -DTEST_NUMBER=10 --clang -tused -e 200 --no_wrap_diagnostics --clang_ver
145145
check(t.x, " T", 0);
146146
^
147147

148-
"test_passed.cpp", line 4301: error: static assertion failed with ""
149-
static_assert(b.a.n == 4300, "");
150-
^
151-
"test_passed.cpp", line 4301: note: the final comparison was 4201 == 4300
152-
static_assert(b.a.n == 4300, "");
153-
^
154-
155-
"test_passed.cpp", line 4302: error: static assertion failed with ""
156-
static_assert(b.a.n2 == 4300, "");
157-
^
158-
"test_passed.cpp", line 4302: note: the final comparison was 4201 == 4300
159-
static_assert(b.a.n2 == 4300, "");
160-
^
161-
162-
"test_passed.cpp", line 4303: error: static assertion failed with ""
163-
static_assert(b.a.info.line() == 4300, "");
164-
^
165-
"test_passed.cpp", line 4303: note: the final comparison was 4201 == 4300
166-
static_assert(b.a.info.line() == 4300, "");
167-
^
168-
169-
"test_passed.cpp", line 4304: error: static assertion failed
170-
static_assert(is_equal(b.a.f, "test_passed.cpp"));
171-
^
172-
173-
"test_passed.cpp", line 4305: error: static assertion failed
174-
static_assert(is_equal(b.a.f2, "test_passed.cpp"));
175-
^
176-
177-
"test_passed.cpp", line 4306: error: static assertion failed
178-
static_assert(is_equal(b.a.info.file(), "test_passed.cpp"));
179-
^
180-
181-
"test_func_passed.cpp", line 4401: error: static assertion failed with ""
182-
static_assert(b.a.n == 4400, "");
183-
^
184-
"test_func_passed.cpp", line 4401: note: the final comparison was 4201 == 4400
185-
static_assert(b.a.n == 4400, "");
186-
^
187-
188-
"test_func_passed.cpp", line 4402: error: static assertion failed with ""
189-
static_assert(b.a.n2 == 4400, "");
190-
^
191-
"test_func_passed.cpp", line 4402: note: the final comparison was 4201 == 4400
192-
static_assert(b.a.n2 == 4400, "");
193-
^
194-
195-
"test_func_passed.cpp", line 4403: error: static assertion failed with ""
196-
static_assert(b.a.info.line() == 4400, "");
197-
^
198-
"test_func_passed.cpp", line 4403: note: the final comparison was 4201 == 4400
199-
static_assert(b.a.info.line() == 4400, "");
200-
^
201-
202-
"test_func_passed.cpp", line 4404: error: static assertion failed
203-
static_assert(is_equal(b.a.f, "test_func_passed.cpp"));
204-
^
205-
206-
"test_func_passed.cpp", line 4405: error: static assertion failed
207-
static_assert(is_equal(b.a.f2, "test_func_passed.cpp"));
208-
^
209-
210-
"test_func_passed.cpp", line 4406: error: static assertion failed
211-
static_assert(is_equal(b.a.info.file(), "test_func_passed.cpp"));
212-
^
213-
214-
"test_func_passed.cpp", line 4407: error: static assertion failed
215-
static_assert(is_equal(b.a.func, "test_in_func"));
216-
^
217-
218-
"test_func_passed.cpp", line 4408: error: static assertion failed
219-
static_assert(is_equal(b.a.func2, "test_in_func"));
220-
^
221-
222148
"test_func_passed.cpp", line 4409: error: static assertion failed
223149
static_assert(is_equal(b.a.info.function(), "bool test_out_of_line_init::test_in_func()"));
224150
^
@@ -286,4 +212,4 @@ fe_only -DTEST_NUMBER=10 --clang -tused -e 200 --no_wrap_diagnostics --clang_ver
286212
^
287213
detected during instantiation of "void test_func::inner::C<Ty>::df<T>(double) [with Ty=test_func::A, T=void]" at line 496
288214

289-
47 errors detected in the compilation of "Test_name.c".
215+
33 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/SemaCXX/.source_location.rto/default.2.1.txt‎

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -135,80 +135,6 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
135135
check(t.x, " T", 0);
136136
^
137137

138-
"test_passed.cpp", line 4301: error: static assertion failed with ""
139-
static_assert(b.a.n == 4300, "");
140-
^
141-
"test_passed.cpp", line 4301: note: the final comparison was 4201 == 4300
142-
static_assert(b.a.n == 4300, "");
143-
^
144-
145-
"test_passed.cpp", line 4302: error: static assertion failed with ""
146-
static_assert(b.a.n2 == 4300, "");
147-
^
148-
"test_passed.cpp", line 4302: note: the final comparison was 4201 == 4300
149-
static_assert(b.a.n2 == 4300, "");
150-
^
151-
152-
"test_passed.cpp", line 4303: error: static assertion failed with ""
153-
static_assert(b.a.info.line() == 4300, "");
154-
^
155-
"test_passed.cpp", line 4303: note: the final comparison was 4201 == 4300
156-
static_assert(b.a.info.line() == 4300, "");
157-
^
158-
159-
"test_passed.cpp", line 4304: error: static assertion failed
160-
static_assert(is_equal(b.a.f, "test_passed.cpp"));
161-
^
162-
163-
"test_passed.cpp", line 4305: error: static assertion failed
164-
static_assert(is_equal(b.a.f2, "test_passed.cpp"));
165-
^
166-
167-
"test_passed.cpp", line 4306: error: static assertion failed
168-
static_assert(is_equal(b.a.info.file(), "test_passed.cpp"));
169-
^
170-
171-
"test_func_passed.cpp", line 4401: error: static assertion failed with ""
172-
static_assert(b.a.n == 4400, "");
173-
^
174-
"test_func_passed.cpp", line 4401: note: the final comparison was 4201 == 4400
175-
static_assert(b.a.n == 4400, "");
176-
^
177-
178-
"test_func_passed.cpp", line 4402: error: static assertion failed with ""
179-
static_assert(b.a.n2 == 4400, "");
180-
^
181-
"test_func_passed.cpp", line 4402: note: the final comparison was 4201 == 4400
182-
static_assert(b.a.n2 == 4400, "");
183-
^
184-
185-
"test_func_passed.cpp", line 4403: error: static assertion failed with ""
186-
static_assert(b.a.info.line() == 4400, "");
187-
^
188-
"test_func_passed.cpp", line 4403: note: the final comparison was 4201 == 4400
189-
static_assert(b.a.info.line() == 4400, "");
190-
^
191-
192-
"test_func_passed.cpp", line 4404: error: static assertion failed
193-
static_assert(is_equal(b.a.f, "test_func_passed.cpp"));
194-
^
195-
196-
"test_func_passed.cpp", line 4405: error: static assertion failed
197-
static_assert(is_equal(b.a.f2, "test_func_passed.cpp"));
198-
^
199-
200-
"test_func_passed.cpp", line 4406: error: static assertion failed
201-
static_assert(is_equal(b.a.info.file(), "test_func_passed.cpp"));
202-
^
203-
204-
"test_func_passed.cpp", line 4407: error: static assertion failed
205-
static_assert(is_equal(b.a.func, "test_in_func"));
206-
^
207-
208-
"test_func_passed.cpp", line 4408: error: static assertion failed
209-
static_assert(is_equal(b.a.func2, "test_in_func"));
210-
^
211-
212138
"test_func_passed.cpp", line 4409: error: static assertion failed
213139
static_assert(is_equal(b.a.info.function(), "bool test_out_of_line_init::test_in_func()"));
214140
^
@@ -239,4 +165,4 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
239165
^
240166
detected during instantiation of "Pair<U, U> test_func::test_func_template(T, U) [with T=int, U=SL]" at line 361
241167

242-
39 errors detected in the compilation of "Test_name.c".
168+
25 errors detected in the compilation of "Test_name.c".

0 commit comments

Comments
 (0)