Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/interpret.c
Original file line number Diff line number Diff line change
Expand Up @@ -5124,8 +5124,8 @@ within the given complete object).
elem_size = value_bytes_for_type(ips, etp, &result);
if (!result) goto done;
for (k = 0; k<n_elems; k += 1) {
init_subobject_to_zero(ips, subobj, etp, complete_obj);
mark_complete_class_object_if_needed(etp, subobj);
init_subobject_to_zero(ips, subobj, etp, complete_obj);
subobj += elem_size;
} /* for */
}
Expand Down Expand Up @@ -5158,8 +5158,8 @@ within the given complete object).
a_type_ptr ftp = skip_typerefs(fp->type);
a_byte_count offset;
get_mapped_byte_count(&persistent_map, fp, offset);
init_subobject_to_zero(ips, subobj+offset, ftp, complete_obj);
mark_complete_class_object_if_needed(ftp, subobj+offset);
init_subobject_to_zero(ips, subobj+offset, ftp, complete_obj);
} /* for */
for (; bcp != NULL; bcp = bcp->next) {
if (bcp->direct || bcp->is_virtual) {
Expand All @@ -5180,8 +5180,8 @@ within the given complete object).
a_byte_count offset;
a_type_ptr ftp = skip_typerefs(fp->type);
get_mapped_byte_count(&persistent_map, fp, offset);
init_subobject_to_zero(ips, subobj+offset, ftp, complete_obj);
mark_complete_class_object_if_needed(ftp, subobj+offset);
init_subobject_to_zero(ips, subobj+offset, ftp, complete_obj);
/* Record the active field. */
*(a_field_ptr*)subobj = fp;
} else {
Expand Down
20 changes: 20 additions & 0 deletions src/pending_changes/gh-618-constexpr-union-zero-init-assign.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[GH #618]
Constexpr assignment from a value-initialized temporary and unions

Zero-initialization makes the first named member of each union subobject
active. When the constexpr interpreter zero-initialized a class object, it
recorded the active member of each union subobject and then cleared that
record again, so a union inside a value-initialized temporary had no active
member. Assigning such a temporary to an object in constant evaluation
therefore left the object's unions with no active member. For example, with
--c++20:

struct R { union { int a; long b; }; };
constexpr int f() {
R x;
x = R();
return x.a;
}
static_assert(f() == 0); // Previously not a constant.

This is now fixed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fe_only -DTEST_NUMBER=1 --c++20 Test_name.c
"Test_name.c", line 16: error: expression must have a constant value
static_assert(f() == 0);
^
"Test_name.c", line 14: note: invalid access to inactive member "R::b" of union (member "R::a" is active)
return x.b;
^

1 error detected in the compilation of "Test_name.c".
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --c++20 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//type:fn
//options_all:--c++20

// Companion to constexpr-union-zero-init-assign.sft.cpp: after x = R(), the
// first union member is active, so reading another member is still not a
// constant. The note names the active member.

struct R {
union { int a; long b; };
};
constexpr int f() {
R x;
x = R();
return x.b;
}
static_assert(f() == 0);
52 changes: 52 additions & 0 deletions tests/tests/regressions/constexpr-union-zero-init-assign.sft.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//type:fp
//options_all:--c++20

// Zero-initialization makes the first named member of each union subobject
// active ([dcl.init.general]). Previously, when a value-initialized temporary
// was assigned to an object during constant evaluation (x = R()), the union
// subobjects of the temporary were left with no active member, so reading
// the first member afterwards was not a constant.

struct R {
union { int a; long b; };
};
constexpr int f() {
R x;
x = R();
return x.a;
}
static_assert(f() == 0); // Previously not a constant.

union U { int a; long b; };
struct N { U u; };
constexpr int g() {
N x;
x = N();
return x.u.a;
}
static_assert(g() == 0); // Previously not a constant.

struct A { U arr[2]; };
constexpr int h() {
A x;
x = A();
return x.arr[1].a;
}
static_assert(h() == 0); // Previously not a constant.

// A union nested in a union, in a base class subobject.
union V { union { int p; } in; long q; };
struct B { V v; };
struct D : B { int z; };
constexpr int k() {
D x;
x = D();
return x.v.in.p + x.z;
}
static_assert(k() == 0); // Previously not a constant.

// Forms that were already accepted.
constexpr int l1() { R x; x = R{}; return x.a; }
static_assert(l1() == 0);
constexpr int l2() { R x = R(); return x.a; }
static_assert(l2() == 0);
Loading