On Fri, Oct 30, 2026 at 12:58:29PM -0400, Jason Merrill wrote:
> 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.
It is a different thing.
CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P says that the whole object is
within lifetime, any holes (i.e. omitted CONSTRUCTOR elts) are not within
lifetime and the rest normally is.
While the state during construction is different.
The whole object is not within lifetime, until the constructor returns.
The constructor can be called either on a pre-zeroed CONSTRUCTOR, i.e. one
without CONSTRUCTOR_NO_CLEARING and perhaps some initializers already
present (e.g. for meta::info objects, or pointer to members?) in case it
is non-user provided ctor and one uses it e.g. with base (), or normally
with CONSTRUCTOR_NO_CLEARING, but even in that case, if there is some
CONSTRUCTOR element for it, it doesn't necessarily mean it is within
lifetime (could be that it is being constructed), and on the other sides,
missing CONSTRUCTOR element doesn't imply it is not within lifetime, e.g.
members omitted from mem-initializer are vacuously initialized in between
the previous and next member. So, I don't see some easy rule on what is
already within lifetime and what is not depending on which parts of the
CONSTRUCTOR_ELTS are present or absent.
> Can we use normal initialization semantics and add CLOBBER_OBJECT_BEGIN for
> subobjects with no other initialization?
Wouldn't that add significantly more overhead? Larger IL, both for constant
expression evaluation and what goes into the gimplifier and at constant
evaluation time we'd need to basically always fill in all the members (some
of them with void_node, sure). Plus how does that play with the zero
initialization before evaluating the constructor? That is e.g.
struct A { int a = 42, b };
struct B { constexpr B () : A (), c (16) {} int c; };
void foo ()
{
constexpr B b;
static_assert (b.b == 0, "");
}
> Do those cases have CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P set?
For the union cases usually no, for inactive union elemental subobjects
obviously it has to be set.
Jakub