On 10/30/26 5:03 AM, Jakub Jelinek wrote:
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.

Hmm, I'm not sure why we need any additions to the IL:

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

I expected CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P to be sufficient for this case.

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.

Can we use normal initialization semantics and add CLOBBER_OBJECT_BEGIN for subobjects with no other initialization?

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).

And here we should be able to look up the member in the CONSTRUCTOR normally.

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.

Do those cases have CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P set?

Jason

Reply via email to