Skip to content

Commit 78f95ec

Browse files
Momen-G-Ardaveedvdv-nvidia
authored andcommitted
Apply Core issue 616 when emulating GCC 9+ and Clang 4+ [GH #90]
Under Core issue 616, selecting a member of a class prvalue yields an xvalue, and binding a reference to it extends the lifetime of the whole temporary. GNU and Clang modes kept the C++11 rule (a prvalue), so const int &r = A().b; bound to a copy of the member and destroyed A() at the end of the full-expression. GCC 9 and Clang 4 implement the issue, so cmd_line.c now enables selection_from_prvalue_is_xvalue when emulating those versions or later. This reapplies the change backed out for EDGcpfe/29032. That change regressed Clang's SemaCXX/constant-expression-cxx2a because, for "." and ".*" on a class prvalue, the interpreter marked the whole prvalue as initialized (eok_dot_field and eok_pm_field in interpret.c). Since C++20 a constexpr constructor can leave members uninitialized, so "Y(0,0).z1.n" folded to an indeterminate value instead of being diagnosed. The marking is removed; the previous commit fixed the one case where the per-subobject state was incomplete. The same bug already affected strict C++20 mode ("static_assert(W().n == 0);" was accepted). is_address_of_auto_object (exprutil.c) now looks through such an xvalue to the class prvalue, so returning a pointer into the temporary (e.g., "return &static_cast<const int &>(A().b);") still warns; "return A().b;" is already diagnosed through is_glvalue_for_auto_object_or_temp_subobject. The traverse_addressing_subtree comments (il_walk.c) are updated to match. Tests: regressions/gh-90 (lifetime, including the GCC 9 and Clang 4 modes), regressions/gh-90-old-modes ("A().b" is still a prvalue for GCC 8.3 and Clang 3.9), regressions/gh-90-warn (warnings), regressions/gh-90-uninit-member ("W().n" and "W().*pn" read an uninitialized member), cwg/cwg616. Updated expectations: gnu init/ref19 now passes; clang drs/cwg6xx no longer rejects the Core issue 616 cases; clang expr.const/p2-0x now diagnoses a read of a volatile prvalue's member; clang cxx2c-placeholder-vars loses an "expression has no effect" warning on "S{}._", as in strict mode; a note caret moves to the member in SemaCXX/constant-expression-cxx2a; gnu cpp0x/constexpr-delegating3 (delegation to a defaulted constructor, a separate bug) still fails, but with "access to uninitialized object" instead of a garbage value.
1 parent be1a2e3 commit 78f95ec

67 files changed

Lines changed: 337 additions & 312 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/cmd_line.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4317,7 +4317,9 @@ default mode (e.g., exception handling).
43174317
if (!option_kind_used[(int)optk_digit_separators]) {
43184318
digit_separators_enabled = TRUE;
43194319
} /* if */
4320-
if (rvalue_references_enabled && !gpp_mode && !clang_mode) {
4320+
if (rvalue_references_enabled &&
4321+
!gpp_version_is(<90000) && !clangcpp_version_is(<40000)) {
4322+
/* A field selection on a class prvalue is an xvalue (CWG 616). */
43214323
selection_from_prvalue_is_xvalue = TRUE;
43224324
} /* if */
43234325
relaxed_constexpr_enabled = TRUE;

‎src/exprutil.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25481,6 +25481,10 @@ underlying entity is a temporary, return *is_temp set to TRUE.
2548125481
clear_expr_or_stmt_traversal_block(&tblock);
2548225482
tblock.process_expr = examine_expr_for_auto_object;
2548325483
tblock.follow_addressing_path = TRUE;
25484+
/* A pointer into a prvalue.field xvalue (CWG 616) addresses a
25485+
subobject of the class prvalue. */
25486+
tblock.follow_class_rvalue_addressing_path =
25487+
selection_from_prvalue_is_xvalue;
2548425488
traverse_expr(expr, &tblock);
2548525489
is_addr_of_auto = tblock.result;
2548625490
*is_temp = tblock.is_temp;

‎src/il_walk.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,10 +3267,10 @@ Tips for proper use of the follow_addressing_path mode:
32673267
switch (op) {
32683268
case eok_dot_field:
32693269
/* x.y: Follow x. */
3270-
/* We don't have to test for the prvalue.field case here because its
3271-
result is a prvalue and therefore wouldn't get here, unless
3272-
follow_class_rvalue_addressing_path is TRUE, in which case
3273-
we want to keep going anyway. */
3270+
/* We don't have to test for the prvalue.field case here: it gets
3271+
here only if follow_class_rvalue_addressing_path is TRUE or if
3272+
its result is an xvalue (selection_from_prvalue_is_xvalue,
3273+
CWG 616), and in both cases we want to keep going. */
32743274
traverse_expr(operand1, tblock);
32753275
break;
32763276
case eok_points_to_field:
@@ -3279,10 +3279,10 @@ Tips for proper use of the follow_addressing_path mode:
32793279
break;
32803280
case eok_pm_field:
32813281
/* x.*pm: Follow x. */
3282-
/* We don't have to test for the prvalue.*pm case here because its
3283-
result is a prvalue and therefore wouldn't get here, unless
3284-
follow_class_rvalue_addressing_path is TRUE, in which case
3285-
we want to keep going anyway. */
3282+
/* We don't have to test for the prvalue.*pm case here: it gets
3283+
here only if follow_class_rvalue_addressing_path is TRUE or if
3284+
its result is an xvalue (selection_from_prvalue_is_xvalue,
3285+
CWG 616), and in both cases we want to keep going. */
32863286
traverse_expr(operand1, tblock);
32873287
break;
32883288
case eok_pm_points_to_field:

‎src/interpret.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32261,8 +32261,9 @@ the value representation of the integer value.
3226132261
result_addr = *(a_constexpr_address*)opnd1_value;
3226232262
} else {
3226332263
/* The first operand is a class rvalue: Create an address for
32264-
it. */
32265-
mark_complete_object_initialized(opnd1_value);
32264+
it. Do not mark the object as fully initialized: since
32265+
C++20, a constexpr constructor can leave members
32266+
uninitialized, and reading those must be diagnosed. */
3226632267
clear_address(&result_addr, opnd1_value);
3226732268
result_addr.alloc_seq_number = ips->storage_stack.alloc_seq();
3226832269
} /* if */
@@ -32386,8 +32387,9 @@ the value representation of the integer value.
3238632387
result_addr = *(a_constexpr_address*)opnd1_value;
3238732388
} else {
3238832389
/* The first operand is a class rvalue: Create an address for
32389-
it. */
32390-
mark_complete_object_initialized(opnd1_value);
32390+
it. Do not mark the object as fully initialized: since
32391+
C++20, a constexpr constructor can leave members
32392+
uninitialized, and reading those must be diagnosed. */
3239132393
clear_address(&result_addr, opnd1_value);
3239232394
result_addr.alloc_seq_number = ips->storage_stack.alloc_seq();
3239332395
} /* if */
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[GH #90]
2+
GNU and Clang C++ modes: lifetime extension through a member of a temporary
3+
4+
When emulating GCC 9 or later or Clang 4 or later, selecting a member of a
5+
class prvalue now produces an xvalue (Core issue 616), as it already did in
6+
standard C++14 and later modes. Binding a reference to the member now
7+
extends the lifetime of the whole temporary. For example, with
8+
--g++ --gnu_version=150000:
9+
10+
struct A { int b; ~A(); };
11+
const int &r = A().b; // A() was previously destroyed here.
12+
13+
Constant evaluation also no longer treats every member of a class prvalue
14+
as initialized when one is selected with "." or ".*". Since C++20, a
15+
constexpr constructor can leave members uninitialized, and reading such a
16+
member is now diagnosed instead of producing an indeterminate value. For
17+
example, with --c++20:
18+
19+
struct W { int n; constexpr W() {} };
20+
static_assert(W().n == 0); // Error: n is uninitialized.

‎tests/expectations/edg_x86_64/imported.log‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7740,7 +7740,6 @@ gnu/cpp/init/new22.sft.cpp:fn:-DTEST_NUMBER=1:FAIL:
77407740
gnu/cpp/init/new39.sft.cpp:rp:-DTEST_NUMBER=1:FAIL:
77417741
gnu/cpp/init/new40.sft.cpp:rp:-DTEST_NUMBER=1:FAIL:
77427742
gnu/cpp/init/null1.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
7743-
gnu/cpp/init/ref19.sft.cpp:rp:-DTEST_NUMBER=1:FAIL:
77447743
gnu/cpp/init/ref23.sft.cpp:fp:-DTEST_NUMBER=1:FAIL:
77457744
gnu/cpp/init/reference2.sft.cpp:fn:-DTEST_NUMBER=1:FAIL:
77467745
gnu/cpp/init/self1.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:FAIL:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++14 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=2 --c++14 --g++ --gnu_version=150000 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=3 --c++14 --clang --clang_version=190100 Test_name.c

‎tests/tests/cwg/cwg616.sft.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//type:fp
2+
//options_all:--c++14
3+
//options::--g++ --gnu_version=150000:--clang --clang_version=190100
4+
template <class T> struct is_xvalue { static const bool value = false; };
5+
template <class T> struct is_xvalue<T &&> { static const bool value = true; };
6+
struct A { int b; };
7+
// Selecting a member of a class prvalue produces an xvalue.
8+
static_assert(is_xvalue<decltype((A().b))>::value, "");
9+
static_assert(is_xvalue<decltype((A().*&A::b))>::value, "");
10+
11+
//cwg: 616
12+
//title: Definition of "indeterminate value"
13+
//meeting: Bristol 4/13
14+
//edg_status: Passes

0 commit comments

Comments
 (0)