Hi!
As the following testcases show, we don't track precisely the lifetime
of objects during their construction.
The standard I believe says that objects are within lifetime after their
construction ends, so if we have
constexpr struct A { int a, b, c; constexpr A () : a (1), c (2) { a = 3; } } d;
then at the start of the ctor neither d nor d.a nor d.b nor d.c are within
lifetime,
right after a (1) only d.a is within lifetime, right before c (2) also d.b is
within
lifetime, after c (2) (on { of the body) all subobjects are within lifetime
and only when the ctor returns the d object is within lifetime.
Now, with consteval or constexpr escalated to consteval ctors it is possible
to query it with std::is_within_lifetime.
The following patch uses just 2 new flags on EXPR_STMTs and occassionally a
dummy
EXPR_STMT on the IL and during constexpr evaluation another new flag on
CONSTRUCTOR and the ctx->global->values hash_map to record the current
state.
The patch basically marks all EXPR_STMTs not nested in other EXPR_STMTs emitted
by
emit_mem_initializers with one flag and the last of those with another flag
and expects that the body of those EXPR_STMTs is simple enough to be
understood.
During constant evaluation the patch marks the CONSTRUCTOR of the object
being created with a new flag - CONSTRUCTOR_BEING_CONSTRUCTED_P - or, if it
doesn't exist yet, creates the CONSTRUCTOR and marks it, and additionally
records in ctx->global->values for that particular constructor with
NULL_TREE the case that it is in a state before any subobject is within
lifetime. When evaluating EXPR_STMTs with the new flags we try to deduce
from the body which FIELD_DECL they are initializing (expect those to be
either INIT_EXPR with in most cases COMPONENT_REF = whatever (with some
exceptions), or CALL_EXPR with ADDR_EXPR COMPONENT_REF with optional casts
in between those (again, with some exceptions for empty class bases) or
MODIFY_EXPR for vptr updates, find the CONSTRUCTOR_BEING_CONSTRUCTED_P
and for it update the hash_map with the FIELD_DECL of subobject being
constructed, and after evaluating the last EXPR_STMT of mem-initializers
(explicit or implicit), i.e. right before { of the ctor body, mark the
hash_map with a value which says all subobjects are initialized.
When evaluating is_within_lifetime and the CONSTRUCTOR_BEING_CONSTRUCTED_P
flag is set, if it is the object we query, we return boolean_false_node,
if it is on a COMPONENT_REF, depending on if the COMPONENT_REF's FIELD_DECL
and the FIELD_DECL in hash_map is a base, or member, or one base and another
member we figure out if the subobject in question is within lifetime, if
both are from the same category walk what sort_mem_initializers walks and
check which of those appears first. Oh, and the code doesn't update the
hash_map when it is clearly unnecessary (if it is INIT_EXPR with rhs without
side-effects).
Admittedly this is a little bit fragile, but nothing during
bootstrap/regtest fails.
Another possibility would be to replace those EXPR_STMTs with some new tree
which would record the FIELD_DECL in it somehow, so that it doesn't need
to be rediscovered (but then a question is how to do that, normal say 2
operand expression would expect an expression operand but FIELD_DECL is
not).
I had some problems with the creation of the CONSTRUCTOR on some testcases,
clearly often we created the CONSTRUCTOR only when actually storing something
into it for the first time. The problem was with the clearing of
CONSTRUCTOR_NO_CLEARING flag in reduced_constant_expression_p.
I think clearing it is just wrong when in fact we say it is a valid
constant, but where there is actually no clearing.
Two cases, one is C++20 union CONSTRUCTOR with no elements (i.e. union
without an active member), that needs to be distinguished from zero
initialized union (which has the first member active).
And another is C++26 array with some inactive union elemental subobjects,
again we need to distinguish that from the case where all the missing
subobjects are zero initialized.
So the patch stops clearing CONSTRUCTOR_NO_CLEARING in that case, and in
cxx_eval_outermost_constant_expr special cases the former case so that we
don't regress valid cases, the latter shouldn't be toplevel.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
Or do you prefer to mark explicitly in the IL what is being initialized
and throw it away during gimplification?
2026-10-30 Jakub Jelinek <[email protected]>
PR c++/127386
* cp-tree.h (EXPR_STMT_MEM_INITIALIZER_P,
EXPR_STMT_LAST_MEM_INITIALIZER_P, CONSTRUCTOR_BEING_CONSTRUCTED_P):
Define.
* init.cc: Include tree-iterator.h.
(mark_mem_init_expr_stmts): New function.
(emit_mem_initializers): Mark EXPR_STMTS in the emitted sequence
with EXPR_STMT_MEM_INITIALIZER_P flags and the last one also with
EXPR_STMT_LAST_MEM_INITIALIZER_P flag, if there aren't any,
create a dummy EXPR_STMT with that latter flag.
* constexpr.cc (is_within_lifetime): Handle
CONSTRUCTOR_BEING_CONSTRUCTED_P.
(cxx_eval_start_lifetime): Remove dead lhs from a call.
(cxx_eval_call_expression): Create if not already created a
CONSTRUCTOR at the start of a constructor, set
CONSTRUCTOR_BEING_CONSTRUCTED_P flag on it and clear it after
evaluating whole body.
(reduced_constant_expression_p): Don't clear CONSTRUCTOR_NO_CLEARING
flag for C++20 unions with no active members or for C++26 arrays
with some inactive union elemental subobjects.
(cxx_eval_constant_expression) <case EXPR_STMT>: For
EXPR_STMT_MEM_INITIALIZER_P and EXPR_STMT_LAST_MEM_INITIALIZER_P
flag note in hash map the first FIELD_DECL which is not yet
within lifetime or after constructing the last one that all subobjects
are within lifetime.
(cxx_eval_outermost_constant_expr): Don't error on C++20 unions with
no active members.
* g++.dg/cpp26/within-lifetime3.C: Remove two xfailed dg-bogus
directives.
* g++.dg/cpp26/within-lifetime15.C: New test.
* g++.dg/cpp26/within-lifetime16.C: New test.
--- a/gcc/cp/cp-tree.h 2026-10-29 10:04:31.988882762 +0100
+++ b/gcc/cp/cp-tree.h 2026-10-29 10:20:51.299924716 +0100
@@ -509,6 +509,7 @@ extern GTY(()) tree cp_global_trees[CPTI
MUST_NOT_THROW_CATCH_P (in MUST_NOT_THROW_EXPR)
MULTIPLE_NAMES_PARM_P (in PARM_DECL)
SPLICE_EXPR_ADDRESS_P (in SPLICE_EXPR)
+ EXPR_STMT_MEM_INITIALIZER_P (in EXPR_STMT)
3: IMPLICIT_RVALUE_P (in NON_LVALUE_EXPR or STATIC_CAST_EXPR)
ICS_BAD_FLAG (in _CONV)
FN_TRY_BLOCK_P (in TRY_BLOCK)
@@ -524,6 +525,7 @@ extern GTY(()) tree cp_global_trees[CPTI
IF_STMT_VACUOUS_INIT_P (IF_STMT)
TYPENAME_IS_RESOLVING_P (in TYPENAME_TYPE)
SPLICE_EXPR_TEMPLATE_P (in SPLICE_EXPR)
+ EXPR_STMT_LAST_MEM_INITIALIZER_P (in EXPR_STMT)
4: IDENTIFIER_MARKED (IDENTIFIER_NODEs)
TREE_HAS_CONSTRUCTOR (in INDIRECT_REF, SAVE_EXPR, CONSTRUCTOR,
CALL_EXPR, or FIELD_DECL).
@@ -799,6 +801,14 @@ typedef struct ptrmem_cst * ptrmem_cst_t
#define EXPR_STMT_STMT_EXPR_RESULT(NODE) \
TREE_LANG_FLAG_0 (EXPR_STMT_CHECK (NODE))
+/* Marks EXPR_STMT emitted by emit_mem_initializers in ctors. */
+#define EXPR_STMT_MEM_INITIALIZER_P(NODE) \
+ TREE_LANG_FLAG_2 (EXPR_STMT_CHECK (NODE))
+
+/* Marks the last EXPR_STMT emitted by emit_mem_initializers in ctors. */
+#define EXPR_STMT_LAST_MEM_INITIALIZER_P(NODE) \
+ TREE_LANG_FLAG_3 (EXPR_STMT_CHECK (NODE))
+
/* Nonzero if this statement-expression does not have an associated scope. */
#define STMT_EXPR_NO_SCOPE(NODE) \
TREE_LANG_FLAG_0 (STMT_EXPR_CHECK (NODE))
@@ -5119,6 +5129,11 @@ get_vec_init_expr (tree t)
#define CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P(NODE) \
(CONSTRUCTOR_CHECK (NODE)->base.deprecated_flag)
+/* True for a CONSTRUCTOR of an object which is being constructed during
+ constant evaluation. */
+#define CONSTRUCTOR_BEING_CONSTRUCTED_P(NODE) \
+ (CONSTRUCTOR_CHECK (NODE)->base.used_flag)
+
/* True if NODE represents a conversion for direct-initialization in a
template. Set by perform_implicit_conversion_flags. */
#define IMPLICIT_CONV_EXPR_DIRECT_INIT(NODE) \
--- a/gcc/cp/init.cc 2026-10-28 11:57:59.007232484 +0100
+++ b/gcc/cp/init.cc 2026-10-29 10:20:51.301496289 +0100
@@ -34,6 +34,7 @@ along with GCC; see the file COPYING3.
#include "asan.h"
#include "stor-layout.h"
#include "pointer-query.h"
+#include "tree-iterator.h"
static bool begin_init_stmts (tree *, tree *);
static tree finish_init_stmts (bool, tree, tree);
@@ -1521,6 +1522,33 @@ mark_exp_read_r (tree *tp, int *, void *
return NULL_TREE;
}
+/* Mark EXPR_STMTs in statements emitted by emit_mem_initializers with
+ the EXPR_STMT_MEM_INITIALIZER_P flag. Return the last such EXPR_STMT
+ if any, otherwise NULL_TREE. */
+
+static tree
+mark_mem_init_expr_stmts (tree t, tree last)
+{
+ if (TREE_CODE (t) == CLEANUP_STMT)
+ t = CLEANUP_BODY (t);
+ if (TREE_CODE (t) == IF_STMT)
+ t = THEN_CLAUSE (t);
+ if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
+ t = TREE_OPERAND (t, 0);
+ if (TREE_CODE (t) == STATEMENT_LIST)
+ {
+ for (tree stmt : tsi_range (t))
+ last = mark_mem_init_expr_stmts (stmt, last);
+ return last;
+ }
+ if (TREE_CODE (t) == DECL_EXPR)
+ /* This can happen when extending lifetime of temporaries. */
+ return last;
+ gcc_assert (TREE_CODE (t) == EXPR_STMT);
+ EXPR_STMT_MEM_INITIALIZER_P (t) = 1;
+ return t;
+}
+
/* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
is a TREE_LIST giving the explicit mem-initializer-list for the
constructor. The TREE_PURPOSE of each entry is a subobject (a
@@ -1571,6 +1599,10 @@ emit_mem_initializers (tree mem_inits)
in_base_initializer = 1;
+ tree stmt_list = cur_stmt_list;
+ gcc_assert (TREE_CODE (stmt_list) == STATEMENT_LIST);
+ tree_stmt_iterator iter = tsi_last (stmt_list);
+
/* Initialize base classes. */
for (; (mem_inits
&& TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
@@ -1653,6 +1685,37 @@ emit_mem_initializers (tree mem_inits)
mem_inits = TREE_CHAIN (mem_inits);
}
+
+ /* For constant evaluation purposes, set the EXPR_STMT_MEM_INITIALIZER_P
+ and EXPR_STMT_LAST_MEM_INITIALIZER_P flags so that lifetime of subobjects
+ can be accurately tracked. */
+ if (!is_empty_class (current_class_type))
+ {
+ tree last;
+ if (tsi_end_p (iter))
+ last = mark_mem_init_expr_stmts (stmt_list, NULL_TREE);
+ else
+ {
+ last = NULL_TREE;
+ ++iter;
+ while (!tsi_end_p (iter))
+ {
+ last = mark_mem_init_expr_stmts (tsi_stmt (iter), last);
+ ++iter;
+ }
+ }
+ if (last)
+ EXPR_STMT_LAST_MEM_INITIALIZER_P (last) = 1;
+ else
+ {
+ /* No code is needed for construction before entering the
+ body of the ctor. Add artificial EXPR_STMT to say so. */
+ last = build_stmt (UNKNOWN_LOCATION, EXPR_STMT, void_node);
+ EXPR_STMT_MEM_INITIALIZER_P (last) = 1;
+ EXPR_STMT_LAST_MEM_INITIALIZER_P (last) = 1;
+ append_to_statement_list_force (last, &cur_stmt_list);
+ }
+ }
}
/* Returns the address of the vtable (i.e., the value that should be
--- a/gcc/cp/constexpr.cc 2026-10-28 11:58:06.716131759 +0100
+++ b/gcc/cp/constexpr.cc 2026-10-29 20:29:35.342743972 +0100
@@ -2688,6 +2688,12 @@ is_within_lifetime (location_t loc, cons
else
{
gcc_assert (TREE_CODE (val) == CONSTRUCTOR);
+ if (CONSTRUCTOR_BEING_CONSTRUCTED_P (val))
+ {
+ tree *vp = ctx->global->get_raw_value_ptr (val);
+ if (vp && *vp != void_node)
+ return boolean_false_node;
+ }
if (CONSTRUCTOR_NELTS (val) == 0)
{
if (CONSTRUCTOR_NO_CLEARING (val))
@@ -2715,7 +2721,75 @@ is_within_lifetime (location_t loc, cons
continue;
if (val == error_mark_node)
return val;
+ tree *vp;
gcc_assert (TREE_CODE (val) == CONSTRUCTOR);
+ vp = NULL;
+ if (CONSTRUCTOR_BEING_CONSTRUCTED_P (val))
+ {
+ vp = ctx->global->get_raw_value_ptr (val);
+ if (vp)
+ {
+ /* If nothing is within lifetime yet, result false. */
+ if (*vp == NULL_TREE)
+ return boolean_false_node;
+ tree field = TREE_OPERAND (ref, 1);
+ if (TREE_CODE (*vp) == FIELD_DECL)
+ {
+ tree type = TREE_TYPE (TREE_OPERAND (ref, 0));
+ if (DECL_FIELD_IS_BASE (*vp))
+ {
+ if (DECL_FIELD_IS_BASE (field))
+ {
+ tree base, binfo, base_binfo;
+ vec<tree, va_gc> *vbases;
+ int i;
+ /* Search the virtual bases. */
+ for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
+ vec_safe_iterate (vbases, i, &base); i++)
+ if (TREE_TYPE (*vp) == TREE_TYPE (base))
+ return boolean_false_node;
+ else if (TREE_TYPE (field) == TREE_TYPE (base))
+ {
+ vp = NULL;
+ break;
+ }
+ if (vp)
+ {
+ /* Search the direct bases. */
+ for (binfo = TYPE_BINFO (type), i = 0;
+ BINFO_BASE_ITERATE (binfo, i, base_binfo);
+ ++i)
+ if (BINFO_VIRTUAL_P (base_binfo))
+ continue;
+ else if (TREE_TYPE (*vp)
+ == TREE_TYPE (base_binfo))
+ return boolean_false_node;
+ else if (TREE_TYPE (field)
+ == TREE_TYPE (base_binfo))
+ break;
+ }
+ }
+ else
+ /* During construction of some base members are
+ certainly not within lifetime. */
+ return boolean_false_node;
+ }
+ else if (!DECL_FIELD_IS_BASE (field))
+ {
+ for (tree fields = TYPE_FIELDS (type); fields;
+ fields = DECL_CHAIN (fields))
+ if (fields == *vp)
+ return boolean_false_node;
+ else if (fields == field)
+ break;
+ }
+ }
+ /* void_node means all subobjects are within lifetime
+ already. */
+ else
+ gcc_assert (*vp == void_node);
+ }
+ }
unsigned int j;
tree field, value;
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), j, field, value)
@@ -2774,6 +2848,9 @@ is_within_lifetime (location_t loc, cons
default:
gcc_unreachable ();
}
+ if (TREE_CODE (val) == CONSTRUCTOR
+ && CONSTRUCTOR_BEING_CONSTRUCTED_P (val))
+ return boolean_false_node;
return boolean_true_node;
}
@@ -2896,8 +2973,8 @@ cxx_eval_start_lifetime (const constexpr
CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (ctor) = 1;
tree modopexpr
= build3_loc (loc, MODOP_EXPR, TREE_TYPE (arg), arg, ctor, NULL_TREE);
- arg = cxx_eval_store_expression (ctx, modopexpr, vc_discard, non_constant_p,
- overflow_p, jump_target);
+ cxx_eval_store_expression (ctx, modopexpr, vc_discard, non_constant_p,
+ overflow_p, jump_target);
ggc_free (modopexpr);
if (*jump_target)
return NULL_TREE;
@@ -5064,6 +5141,47 @@ cxx_eval_call_expression (const constexp
return NULL_TREE;
}
+ tree ctor = NULL_TREE;
+ if (DECL_CONSTRUCTOR_P (fun))
+ {
+ tree this_parm = call_ctx.call->fundef->parms;
+ if (!is_empty_class (TREE_TYPE (TREE_TYPE (this_parm))))
+ {
+ tree obj = cp_build_fold_indirect_ref (this_parm);
+ /* Force creation of a CONSTRUCTOR for *this if it
+ doesn't exist yet (in that case with
+ CONSTRUCTOR_NO_CLEARING flag set), but if it exists
+ already, e.g. if there is a zero initialization
+ where the object has been cleared already and
+ non-user provided constructor is called afterwards,
+ don't do anything. */
+ ctor = build_constructor (TREE_TYPE (obj), NULL);
+ CONSTRUCTOR_NO_CLEARING (ctor) = true;
+ tree activate = build3 (MODOP_EXPR, TREE_TYPE (obj),
+ obj, ctor, NULL_TREE);
+ cxx_eval_store_expression (&call_ctx, activate, vc_discard,
+ non_constant_p, overflow_p,
+ jump_target);
+ ggc_free (activate);
+ if (*jump_target)
+ return NULL_TREE;
+ ctor
+ = cxx_eval_constant_expression (&call_ctx, obj, vc_prvalue,
+ non_constant_p, overflow_p,
+ jump_target);
+ if (*jump_target)
+ return NULL_TREE;
+ if (TREE_CODE (ctor) != CONSTRUCTOR)
+ ctor = NULL_TREE;
+ else
+ {
+ CONSTRUCTOR_BEING_CONSTRUCTED_P (ctor) = 1;
+ /* Indicate no subobject has been constructed yet. */
+ ctx->global->put_value (ctor, NULL_TREE);
+ }
+ }
+ }
+
ctx->global->state_dependent = false;
tree jmp_target = NULL_TREE;
@@ -5071,6 +5189,14 @@ cxx_eval_call_expression (const constexp
vc_discard, non_constant_p, overflow_p,
&jmp_target);
+ if (DECL_CONSTRUCTOR_P (fun)
+ && ctor
+ && CONSTRUCTOR_BEING_CONSTRUCTED_P (ctor))
+ {
+ CONSTRUCTOR_BEING_CONSTRUCTED_P (ctor) = 0;
+ ctx->global->clear_value (ctor);
+ }
+
if (!*non_constant_p && throws (&jmp_target))
{
result = NULL_TREE;
@@ -5289,25 +5415,33 @@ reduced_constant_expression_p (tree t, t
return false;
if (CONSTRUCTOR_NO_CLEARING (t))
{
- if (union_elemental_subobject
- && cxx_dialect >= cxx26
- && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
- && CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (t))
- field = NULL_TREE;
- else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
+ if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
{
+ bool allow_holes
+ = (union_elemental_subobject
+ && cxx_dialect >= cxx26
+ && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
+ && CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (t));
/* There must be a valid constant initializer at every array
- index. */
+ index unless allow_holes. Still, even with allow_holes
+ we need to track if there are any holes, so that we don't
+ incorrectly clear CONSTRUCTOR_NO_CLEARING flag. */
tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
tree cursor = min;
+ bool has_holes = false;
for (auto &e: CONSTRUCTOR_ELTS (t))
{
if (!reduced_constant_expression_p
(e.value, NULL_TREE, union_elemental_subobject))
return false;
if (array_index_cmp (cursor, e.index) != 0)
- return false;
+ {
+ if (allow_holes)
+ has_holes = true;
+ else
+ return false;
+ }
if (TREE_CODE (e.index) == RANGE_EXPR)
cursor = TREE_OPERAND (e.index, 1);
if (TREE_CODE (e.value) == RAW_DATA_CST)
@@ -5319,7 +5453,14 @@ reduced_constant_expression_p (tree t, t
size_one_node);
}
if (find_array_ctor_elt (t, max) == -1)
- return false;
+ {
+ if (allow_holes)
+ has_holes = true;
+ else
+ return false;
+ }
+ if (has_holes)
+ return true;
goto ok;
}
else if (cxx_dialect >= cxx20
@@ -5327,7 +5468,10 @@ reduced_constant_expression_p (tree t, t
{
/* A union can have at most one active member. A union with no
active member has no constituent values, so all constituent
- values are constant. */
+ values are constant. If a union has no active members,
+ make sure not to clear CONSTRUCTOR_NO_CLEARING. */
+ if (CONSTRUCTOR_NELTS (t) == 0)
+ return true;
field = NULL_TREE;
union_elemental_subobject = true;
}
@@ -10250,8 +10394,131 @@ cxx_eval_constant_expression (const cons
}
break;
- case NON_LVALUE_EXPR:
case EXPR_STMT:
+ if (EXPR_STMT_MEM_INITIALIZER_P (t))
+ {
+ tree b = TREE_OPERAND (t, 0);
+ bool last = EXPR_STMT_LAST_MEM_INITIALIZER_P (t);
+ tree field = NULL_TREE;
+ tree this_parm = ctx->call->fundef->parms;
+ tree type
+ = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (this_parm)));
+ if (TREE_CODE (b) == CONVERT_EXPR && VOID_TYPE_P (TREE_TYPE (b)))
+ b = TREE_OPERAND (b, 0);
+ if (TREE_CODE (b) == INIT_EXPR)
+ {
+ if (TREE_SIDE_EFFECTS (TREE_OPERAND (b, 1)))
+ {
+ field = b;
+ b = TREE_OPERAND (b, 0);
+ if (TREE_CODE (b) == INDIRECT_REF)
+ {
+ b = TREE_OPERAND (b, 0);
+ STRIP_NOPS (b);
+ if (b == this_parm)
+ field = NULL_TREE;
+ else
+ {
+ gcc_assert (TREE_CODE (b) == ADDR_EXPR);
+ b = TREE_OPERAND (b, 0);
+ }
+ }
+ if (field)
+ {
+ gcc_assert (TREE_CODE (b) == COMPONENT_REF);
+ while (TREE_CODE (TREE_OPERAND (b, 0)) == COMPONENT_REF)
+ b = TREE_OPERAND (b, 0);
+ field = TREE_OPERAND (b, 1);
+ }
+ }
+ /* Do nothing for simple cases like b (42), those can't
+ query what subobjects are within lifetime. */
+ }
+ else if (TREE_CODE (b) == CALL_EXPR)
+ {
+ field = CALL_EXPR_ARG (b, 0);
+ STRIP_NOPS (field);
+ if (field == this_parm)
+ {
+ tree ctor_type
+ = TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (b, 0)));
+ for (field = TYPE_FIELDS (type);
+ field; field = DECL_CHAIN (field))
+ if (TREE_CODE (field) == FIELD_DECL)
+ break;
+ if (!field
+ || !DECL_FIELD_IS_BASE (field)
+ || TREE_TYPE (field) != ctor_type)
+ {
+ gcc_assert (is_empty_class (ctor_type));
+ field = NULL_TREE;
+ }
+ }
+ else if (TREE_CODE (field) == POINTER_PLUS_EXPR
+ && (tree_strip_nop_conversions (TREE_OPERAND (field, 0))
+ == this_parm)
+ && TREE_CODE (TREE_OPERAND (field, 1)) == INTEGER_CST)
+ {
+ tree ctor_type
+ = TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (b, 0)));
+ gcc_assert (is_empty_class (ctor_type));
+ field = NULL_TREE;
+ }
+ else
+ {
+ gcc_assert (TREE_CODE (field) == ADDR_EXPR);
+ field = TREE_OPERAND (field, 0);
+ gcc_assert (TREE_CODE (field) == COMPONENT_REF);
+ while (TREE_CODE (TREE_OPERAND (field, 0)) == COMPONENT_REF)
+ field = TREE_OPERAND (field, 0);
+ field = TREE_OPERAND (field, 1);
+ }
+ }
+ else if (TREE_CODE (b) == MODIFY_EXPR)
+ {
+ /* This must be a _vptr.X modification. */
+ gcc_assert (TREE_CODE (TREE_OPERAND (b, 0))
+ == COMPONENT_REF);
+ gcc_assert (DECL_ARTIFICIAL (TREE_OPERAND (TREE_OPERAND (b, 0),
+ 1))
+ && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (b, 0), 1))
+ == vtbl_ptr_type_node)
+ && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND (b, 0),
+ 1)));
+ }
+ else
+ gcc_assert (b == void_node);
+ if (field && TREE_CODE (type) == UNION_TYPE)
+ field = NULL_TREE;
+ if (field || last)
+ {
+ if (field)
+ gcc_assert (DECL_CONTEXT (field) == type);
+ tree obj = cp_build_fold_indirect_ref (this_parm);
+ tree ctor = cxx_eval_indirect_ref (ctx, obj, vc_prvalue,
+ non_constant_p, overflow_p,
+ jump_target);
+ if (*non_constant_p || *jump_target)
+ break;
+ gcc_assert (TREE_CODE (ctor) == CONSTRUCTOR
+ && CONSTRUCTOR_BEING_CONSTRUCTED_P (ctor));
+ ggc_free (obj);
+ if (field)
+ /* Indicate subobjects before field are constructed. */
+ ctx->global->put_value (ctor, field);
+ r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
+ lval,
+ non_constant_p, overflow_p,
+ jump_target);
+ if (last)
+ /* Indicate all subobjects are constructed, but the whole
+ object isn't constructed yet. */
+ ctx->global->put_value (ctor, void_node);
+ break;
+ }
+ }
+ /* FALLTHRU */
+ case NON_LVALUE_EXPR:
case EH_SPEC_BLOCK:
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
lval,
@@ -11812,7 +12079,12 @@ cxx_eval_outermost_constant_expr (tree t
/* After verify_constant because reduced_constant_expression_p can unset
CONSTRUCTOR_NO_CLEARING. */
- if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
+ if (TREE_CODE (r) == CONSTRUCTOR
+ && CONSTRUCTOR_NO_CLEARING (r)
+ /* Allow C++20 unions with no active members. */
+ && !(TREE_CODE (TREE_TYPE (r)) == UNION_TYPE
+ && cxx_dialect >= cxx20
+ && CONSTRUCTOR_NELTS (r) == 0))
{
if (!allow_non_constant)
error ("%qE is not a constant expression because it refers to "
--- a/gcc/testsuite/g++.dg/cpp26/within-lifetime3.C 2026-09-14
18:13:08.227679593 +0200
+++ b/gcc/testsuite/g++.dg/cpp26/within-lifetime3.C 2026-10-29
12:24:10.358082552 +0100
@@ -211,11 +211,7 @@ qux ()
return a[0].a + a[0].b + a[0].c;
}
-// FIXME: In mem-initializer of b, a should be already constructed,
-// so within lifetime, but b is in the middle of construction and c
-// construction has not started yet.
-static_assert (qux () == 4 + 5 + 6 + 32); // { dg-bogus "note: the
comparison reduces to '\\\(239 == 47\\\)'" "" { xfail *-*-* } }
- // { dg-bogus "static assertion
failed" "" { xfail *-*-* } .-1 }
+static_assert (qux () == 4 + 5 + 6 + 32);
consteval bool
corge (bool x)
--- a/gcc/testsuite/g++.dg/cpp26/within-lifetime15.C 2026-10-29
13:07:03.234494337 +0100
+++ b/gcc/testsuite/g++.dg/cpp26/within-lifetime15.C 2026-10-29
15:14:42.625193365 +0100
@@ -0,0 +1,109 @@
+// P2641R4 - Checking if a union alternative is active
+// { dg-do compile { target c++20 } }
+
+#include "../cpp2a/construct_at.h"
+
+namespace std {
+ template <class T>
+ consteval bool
+ is_within_lifetime (const T *p) noexcept
+ {
+ return __builtin_is_within_lifetime (p);
+ }
+}
+
+struct A {
+ consteval A () : a (0) {}
+ consteval A (int *p) : a (std::is_within_lifetime (p)) {}
+ int a;
+};
+
+struct B {
+ consteval B () : b (0) {}
+ consteval B (int *p) : b (std::is_within_lifetime (p)) {}
+ int b;
+};
+
+struct C : A, B {
+ consteval C () : c (0) {}
+ consteval C (int) : A (&b), B (&a), c (0)
+ {
+ if (!std::is_within_lifetime (&a)
+ || !std::is_within_lifetime (&b)
+ || !std::is_within_lifetime (&c)
+ || std::is_within_lifetime (this))
+ throw 1;
+ }
+ consteval C (long) : A (&c), B (&b), c (std::is_within_lifetime (&b)) {}
+ int c;
+};
+
+struct D {
+ consteval D () : d (0) {}
+ consteval D (int *p) : d (std::is_within_lifetime (p)) {}
+ constexpr ~D () {}
+ int d;
+};
+
+struct E {
+ consteval E () : e (0) {}
+ consteval E (int *p) : e (std::is_within_lifetime (p)) {}
+ constexpr ~E () {}
+ int e;
+};
+
+struct F : D, E {
+ consteval F () : f (0) {}
+ consteval F (int) : D (&e), E (&d), f (0) {}
+ consteval F (long) : D (&f), E (&e), f (std::is_within_lifetime (&e))
+ {
+ if (!std::is_within_lifetime (&d)
+ || !std::is_within_lifetime (&e)
+ || !std::is_within_lifetime (&f)
+ || std::is_within_lifetime (this))
+ throw 1;
+ }
+ constexpr ~F () {}
+ int f;
+};
+
+consteval int
+foo ()
+{
+ C a[2] = {};
+ std::destroy_at (&a[0]);
+ std::construct_at (&a[0], 1);
+ return a[0].a * 1 + a[0].b * 2 + a[0].c * 4;
+}
+
+consteval int
+bar ()
+{
+ C a[2] = {};
+ std::destroy_at (&a[1]);
+ std::construct_at (&a[1], 1L);
+ return a[1].a * 1 + a[1].b * 2 + a[1].c * 4;
+}
+
+consteval int
+baz ()
+{
+ F a[2] = {};
+ std::destroy_at (&a[0]);
+ std::construct_at (&a[0], 1);
+ return a[0].d * 1 + a[0].e * 2 + a[0].f * 4;
+}
+
+consteval int
+qux ()
+{
+ F a[2] = {};
+ std::destroy_at (&a[1]);
+ std::construct_at (&a[1], 1L);
+ return a[1].d * 1 + a[1].e * 2 + a[1].f * 4;
+}
+
+static_assert (foo () == 2);
+static_assert (bar () == 4);
+static_assert (baz () == 2);
+static_assert (qux () == 4);
--- a/gcc/testsuite/g++.dg/cpp26/within-lifetime16.C 2026-10-29
14:13:41.975213418 +0100
+++ b/gcc/testsuite/g++.dg/cpp26/within-lifetime16.C 2026-10-29
14:18:20.942564152 +0100
@@ -0,0 +1,141 @@
+// P2641R4 - Checking if a union alternative is active
+// { dg-do compile { target c++26 } }
+
+#include "../cpp2a/construct_at.h"
+
+namespace std {
+ template <class T>
+ consteval bool
+ is_within_lifetime (const T *p) noexcept
+ {
+ return __builtin_is_within_lifetime (p);
+ }
+}
+
+struct A {
+ consteval A () : a (0) {}
+ consteval A (int *p) : a (1 + std::is_within_lifetime (p)) {}
+ int a;
+};
+
+struct B {
+ consteval B () : b (0) {}
+ consteval B (int *p) : b (1 + std::is_within_lifetime (p)) {}
+ int b;
+};
+
+struct C : virtual A, virtual B {
+ consteval C () : c (0) {}
+ consteval C (int *p, int *q) : A (p), B (q), c (1 + std::is_within_lifetime
(&b)) {}
+ int c;
+};
+
+struct D : virtual A, virtual C, virtual B {
+ consteval D () : d (0) {}
+ consteval D (int) : A (&b), B (&a), C (&c, &a), d (0) {}
+ consteval D (long) : A (&d), B (&a), C (&d, &d), d (0) {}
+ int d;
+};
+
+struct E : virtual A, virtual C {
+ consteval E () : e (0) {}
+ consteval E (int) : A (&b), C (&c, &a), e (0) {}
+ int e;
+};
+
+struct F {
+ consteval F () : a (0) {}
+ consteval F (int *p) : a (1 + std::is_within_lifetime (p)) {}
+ constexpr ~F () {}
+ int a;
+};
+
+struct G {
+ consteval G () : b (0) {}
+ consteval G (int *p) : b (1 + std::is_within_lifetime (p)) {}
+ constexpr ~G () {}
+ int b;
+};
+
+struct H : virtual F, virtual G {
+ consteval H () : c (0) {}
+ consteval H (int *p, int *q) : F (p), G (q), c (1 + std::is_within_lifetime
(&b)) {}
+ constexpr ~H () {}
+ int c;
+};
+
+struct I : virtual F, virtual H, virtual G {
+ consteval I () : d (0) {}
+ consteval I (int) : F (&b), G (&a), H (&c, &a), d (0) {}
+ consteval I (long) : F (&d), G (&a), H (&d, &d), d (0) {}
+ constexpr ~I () {}
+ int d;
+};
+
+struct J : virtual F, virtual H {
+ consteval J () : e (0) {}
+ consteval J (int) : F (&b), H (&c, &a), e (0) {}
+ constexpr ~J () {}
+ int e;
+};
+
+consteval int
+foo ()
+{
+ D a[2] = {};
+ std::destroy_at (&a[0]);
+ std::construct_at (&a[0], 1);
+ return a[0].a * 1 + a[0].b * 4 + a[0].c * 16 + a[0].d * 64;
+}
+
+consteval int
+bar ()
+{
+ D a[2] = {};
+ std::destroy_at (&a[1]);
+ std::construct_at (&a[1], 1L);
+ return a[1].a * 1 + a[1].b * 4 + a[1].c * 16 + a[1].d * 64;
+}
+
+consteval int
+baz ()
+{
+ E a[2] = {};
+ std::destroy_at (&a[0]);
+ std::construct_at (&a[0], 1);
+ return a[0].a * 1 + a[0].b * 4 + a[0].c * 16 + a[0].e * 64;
+}
+
+consteval int
+qux ()
+{
+ I a[2] = {};
+ std::destroy_at (&a[0]);
+ std::construct_at (&a[0], 1);
+ return a[0].a * 1 + a[0].b * 4 + a[0].c * 16 + a[0].d * 64;
+}
+
+consteval int
+corge ()
+{
+ I a[2] = {};
+ std::destroy_at (&a[1]);
+ std::construct_at (&a[1], 1L);
+ return a[1].a * 1 + a[1].b * 4 + a[1].c * 16 + a[1].d * 64;
+}
+
+consteval int
+garply ()
+{
+ J a[2] = {};
+ std::destroy_at (&a[0]);
+ std::construct_at (&a[0], 1);
+ return a[0].a * 1 + a[0].b * 4 + a[0].c * 16 + a[0].e * 64;
+}
+
+static_assert (foo () == 1 + 2 * 4 + 2 * 16);
+static_assert (bar () == 1 + 2 * 4 + 2 * 16);
+static_assert (baz () == 1 + 0 * 4 + 2 * 16);
+static_assert (qux () == 1 + 2 * 4 + 2 * 16);
+static_assert (corge () == 1 + 2 * 4 + 2 * 16);
+static_assert (garply () == 1 + 0 * 4 + 2 * 16);
Jakub