Skip to content

Commit be1a2e3

Browse files
Momen-G-Ardaveedvdv-nvidia
authored andcommitted
Mark the zeroed members of a value-initialized class as initialized [GH #90]
Value initialization of a class without a user-provided default constructor zero-initializes the object before calling the constructor. The interpreter cleared the storage but did not record the subobjects as initialized, so reading a member that the constructor does not set was rejected in a constant expression: struct A { constexpr A() {} int x; }; struct B : A { int y; }; static_assert(B().x == 0); // Previously rejected. The constructor-call setup in interpret.c now uses init_subobject_to_zero for such classes. Classes with a user-provided default constructor are cleared as before, but not marked. This is needed by the next commit, which stops treating every member of a class prvalue as initialized; without it, "B().y" above would be rejected as well (Clang's dcl.init.general/p16-cxx20, GH69890). Updated expectations: gnu cpp2a/constexpr-init18 now passes; clang dcl.init.general/p16-cxx20, SemaCXX/constexpr-value-init and class.inhctor.init/p1 no longer report false errors.
1 parent 182106d commit be1a2e3

8 files changed

Lines changed: 31 additions & 84 deletions

File tree

‎src/interpret.c‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23985,10 +23985,21 @@ after the constructor body has been interpreted.
2398523985
p_arg_ptr += 1;
2398623986
arg_size += 1;
2398723987
} /* for */
23988-
if (dip->variant.constructor.value_initialization) {
23989-
/* If this is for value initialization, clear the storage first.
23990-
Do not, however, overwrite the first word (which may record the
23991-
inheritance hierarchy). */
23988+
if (!dip->variant.constructor.value_initialization) {
23989+
/* Nothing to do. */
23990+
} else if (!type_has_user_provided_default_constructor(class_type)) {
23991+
/* Value initialization of a class without a user-provided default
23992+
constructor zero-initializes the object before calling the
23993+
constructor, which need not initialize every subobject (e.g., a
23994+
member of a class whose implicit default constructor calls a
23995+
user-provided base class constructor). Mark the subobjects as
23996+
initialized, so that reading them is valid. */
23997+
init_subobject_to_zero(ips, result_storage, class_type,
23998+
complete_object);
23999+
} else {
24000+
/* Otherwise, the object is not zero-initialized, but clear the
24001+
storage anyway. Do not, however, overwrite the first word (which
24002+
may record the inheritance hierarchy). */
2399224003
a_byte_count n_class_bytes;
2399324004
a_boolean dummy = TRUE;
2399424005
n_class_bytes = f_value_bytes_for_type(ips, class_type, &dummy);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[GH #90]
2+
Constexpr reads of zero-initialized members after value initialization
3+
4+
Value initialization of a class without a user-provided default constructor
5+
zero-initializes the object before calling the constructor. During
6+
constant evaluation, the zeroed members were not treated as initialized, so
7+
reading one that the constructor does not set was rejected. For example,
8+
with --c++20:
9+
10+
struct A { constexpr A() {} int x; };
11+
struct B : A { int y; };
12+
static_assert(B().x == 0); // Previously rejected.

‎tests/expectations/edg_x86_64/imported.log‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7065,7 +7065,6 @@ gnu/cpp/cpp2a/consteval13.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
70657065
gnu/cpp/cpp2a/consteval25.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
70667066
gnu/cpp/cpp2a/consteval32.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:FAIL:
70677067
gnu/cpp/cpp2a/constexpr-99287.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
7068-
gnu/cpp/cpp2a/constexpr-init18.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
70697068
gnu/cpp/cpp2a/constexpr-inst1.sft.cpp:fn:-DTEST_NUMBER=1 --c++14:FAIL:
70707069
gnu/cpp/cpp2a/constexpr-inst2.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
70717070
gnu/cpp/cpp2a/constexpr-new11/test.mft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:

‎tests/tests/imported/clang/cpp/CXX/dcl.decl/dcl.init/dcl.init.general/.p16-cxx20.rto/default.1.1.txt‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_version 999999 --c++20 Test_name.c
2-
"Test_name.c", line 16: error: expression must have a constant value
3-
static_assert(B().x == 0);
4-
^
5-
"Test_name.c", line 16: note: access to uninitialized object
6-
static_assert(B().x == 0);
7-
^
8-
92
"Test_name.c", line 27: error: class "P0960R3::A" has no suitable copy constructor
103
A a1(va);
114
^
@@ -144,4 +137,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
144137
D d8 = {vdd}; // expected-error {{no matching constructor for initialization of 'D'}}
145138
^
146139

147-
31 errors detected in the compilation of "Test_name.c".
140+
30 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/CXX/special/class.init/class.inhctor.init/.p1.rto/default.1.1.txt‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
3838
^
3939
detected during implicit generation of "vbase::D::B(int, int)" at line 89
4040

41-
"Test_name.c", line 125: error: expression must have a constant value
42-
constexpr Wrap2 b = {};
43-
^
44-
"Test_name.c", line 117: note: access to uninitialized object
45-
constexpr Param(int c) : n(4 * b.a + b.b + c) {}
46-
^
47-
"Test_name.c", line 123: note: called from:
48-
constexpr Wrap1::Wrap1() : B(1) {}
49-
^
50-
"Test_name.c", line 112: note: called from:
51-
struct Wrap2 : Wrap1 {};
52-
^
53-
54-
10 errors detected in the compilation of "Test_name.c".
41+
9 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/SemaCXX/.constexpr-value-init.rto/default.1.1.txt‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
3030
struct B { // expected-note {{in call to 'A()'}}
3131
^
3232

33-
"Test_name.c", line 22: error: expression must have a constant value
34-
constexpr B b2 = B(); // ok
35-
^
36-
"Test_name.c", line 7: note: access to uninitialized object
37-
constexpr A() : a(b + 1), b(a + 1) {} // expected-note 5{{outside its lifetime}}
38-
^
39-
"Test_name.c", line 11: note: called from:
40-
struct B { // expected-note {{in call to 'A()'}}
41-
^
42-
4333
"Test_name.c", line 30: error: const variable "c1" requires an initializer -- class "C" has no user-provided default constructor
4434
constexpr C c1; // expected-error {{without a user-provided default constructor}}
4535
^
@@ -67,16 +57,6 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
6757
constexpr E e1; // expected-error {{constant expression}} expected-note {{in call to 'E()'}}
6858
^
6959

70-
"Test_name.c", line 47: error: expression must have a constant value
71-
constexpr E e2 = E();
72-
^
73-
"Test_name.c", line 7: note: access to uninitialized object
74-
constexpr A() : a(b + 1), b(a + 1) {} // expected-note 5{{outside its lifetime}}
75-
^
76-
"Test_name.c", line 46: note: called from:
77-
constexpr E e1; // expected-error {{constant expression}} expected-note {{in call to 'E()'}}
78-
^
79-
8060
"Test_name.c", line 59: error: incomplete type "InvalidDeclInsideConstExpr::i<1>" is not allowed
8161
i<x> g; // expected-error {{implicit instantiation of undefined template 'InvalidDeclInsideConstExpr::i<1>'}}
8262
^
@@ -118,4 +98,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
11898
constexpr E e1; // expected-error {{constant expression}} expected-note {{in call to 'E()'}}
11999
^
120100

121-
12 errors detected in the compilation of "Test_name.c".
101+
10 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/SemaCXX/.constexpr-value-init.rto/default.2.1.txt‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
3030
struct B { // expected-note {{in call to 'A()'}}
3131
^
3232

33-
"Test_name.c", line 22: error: expression must have a constant value
34-
constexpr B b2 = B(); // ok
35-
^
36-
"Test_name.c", line 7: note: access to uninitialized object
37-
constexpr A() : a(b + 1), b(a + 1) {} // expected-note 5{{outside its lifetime}}
38-
^
39-
"Test_name.c", line 11: note: called from:
40-
struct B { // expected-note {{in call to 'A()'}}
41-
^
42-
4333
"Test_name.c", line 30: error: const variable "c1" requires an initializer -- class "C" has no user-provided default constructor
4434
constexpr C c1; // expected-error {{without a user-provided default constructor}}
4535
^
@@ -67,16 +57,6 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
6757
constexpr E e1; // expected-error {{constant expression}} expected-note {{in call to 'E()'}}
6858
^
6959

70-
"Test_name.c", line 47: error: expression must have a constant value
71-
constexpr E e2 = E();
72-
^
73-
"Test_name.c", line 7: note: access to uninitialized object
74-
constexpr A() : a(b + 1), b(a + 1) {} // expected-note 5{{outside its lifetime}}
75-
^
76-
"Test_name.c", line 46: note: called from:
77-
constexpr E e1; // expected-error {{constant expression}} expected-note {{in call to 'E()'}}
78-
^
79-
8060
"Test_name.c", line 59: error: incomplete type "InvalidDeclInsideConstExpr::i<1>" is not allowed
8161
i<x> g; // expected-error {{implicit instantiation of undefined template 'InvalidDeclInsideConstExpr::i<1>'}}
8262
^
@@ -118,4 +98,4 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
11898
constexpr E e1; // expected-error {{constant expression}} expected-note {{in call to 'E()'}}
11999
^
120100

121-
12 errors detected in the compilation of "Test_name.c".
101+
10 errors detected in the compilation of "Test_name.c".
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1 @@
11
fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++20 Test_name.c
2-
"Test_name.c", line 14: error: initialization is not constant
3-
constexpr t<1> h1;
4-
^
5-
"Test_name.c", line 14: note: access to uninitialized subobject (member "e::w")
6-
constexpr t<1> h1;
7-
^
8-
9-
"Test_name.c", line 17: error: initialization is not constant
10-
constexpr t<42> h2;
11-
^
12-
"Test_name.c", line 17: note: access to uninitialized subobject (member "e::w")
13-
constexpr t<42> h2;
14-
^
15-
16-
2 errors detected in the compilation of "Test_name.c".

0 commit comments

Comments
 (0)