Skip to content

Lifetime extension through a member of a temporary in GNU and Clang modes [GH #90] - #1

Closed
Momen-G-Ar wants to merge 2 commits into
mainfrom
gh-90-lifetime-extension
Closed

Momen-G-Ar wants to merge 2 commits into
mainfrom
gh-90-lifetime-extension

Conversation

@Momen-G-Ar

@Momen-G-Ar Momen-G-Ar commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes GH #90 ([GNATS 29059]): in GNU and Clang modes, binding a reference to a member of a class temporary did not extend the temporary's lifetime ([class.temporary]/6).

int d;
struct A { int b; ~A() { ++d; } };
int main() {
  const int &r = A().b;
  assert(d == 0);  // failed with --g++ / --clang: A() already destroyed
}
Mode Before After
strict C++14 and later lifetime extended unchanged
--g++ (GCC 9+), --clang (Clang 4+) r bound to a copy of b, A() destroyed at end of full-expression lifetime extended, as in GCC and Clang
--g++ (GCC < 9), --clang (Clang < 4) copy unchanged (those versions use the C++11 rule)

Root cause

  • Core issue 616. In GNU and Clang modes A().b was still a prvalue (the C++11 rule), so the reference bound to a copy of the member. GCC 9 and Clang 4 implement Core issue 616 (A().b is an xvalue). daveed's fix for EDGcpfe/29032 enabled the rule for those versions in cmd_line.c but was backed out because it regressed Clang's SemaCXX/constant-expression-cxx2a.
  • Why that regressed. That test runs with --clang --clang_version 999999, so with the cmd_line.c change Y(0,0).z1.n takes the xvalue path in the constexpr interpreter. For . and .* on a class prvalue, that path called mark_complete_object_initialized on the prvalue. The shortcut dates from before C++20 (P1331), when a constexpr constructor had to initialize every member; now it need not, so the read of the uninitialized z1.n folded to a garbage value (the final comparison was -48039863565308959547770532813896688677 == 0) instead of being diagnosed. The same bug was already present in strict --c++20 mode (static_assert(W().n == 0); was accepted for struct W { int n; constexpr W() {} };).
  • Removing the shortcut exposed one gap. Value initialization of a class without a user-provided default constructor zero-initializes the object, but the interpreter only cleared the storage without recording the members as initialized. Without fixing that first, removing the shortcut rejects B().y in Clang's dcl.init.general/p16-cxx20 (GH69890).

Commits

  1. Mark the zeroed members of a value-initialized class as initialized (interpret.c): prerequisite for commit 2. Value initialization of a class without a user-provided default constructor now uses init_subobject_to_zero. Classes with a user-provided default constructor are cleared as before but not marked.
  2. Apply Core issue 616 when emulating GCC 9+ and Clang 4+ (cmd_line.c, interpret.c, exprutil.c, il_walk.c): the fix for the issue.
    • cmd_line.c: daveed's change.
    • interpret.c: remove the "fully initialized" marking for . and .* on a class prvalue (eok_dot_field, eok_pm_field), which is what made the change regress.
    • exprutil.c: is_glvalue_for_auto_object and is_address_of_auto_object look through the new xvalue to the class prvalue, so return A().b; from a function returning const int & still warns about returning a reference to a local temporary.
    • il_walk.c: comments only.

Each commit has its own Changes entry in src/pending_changes/ (gh-90-value-init-zeroed-members.txt, gh-90-lifetime-extension-member-of-temporary.txt; both pass edg-collect-changes --check), builds, and passes its tests on its own. Based on main at 35b7c47.

Changes to existing expectations

All are improvements, except that constexpr-delegating3 still fails, now with a different diagnostic.

Commit Test Change
1 gnu cpp2a/constexpr-init18 (fp) FAIL → PASS (value-initialized base is zeroed)
1 clang dcl.init.general/p16-cxx20 (fn) false error on B().x removed (GH69890)
1 clang SemaCXX/constexpr-value-init (fn) false errors on B() and E() (// ok) removed
1 clang class.init/class.inhctor.init/p1 (fn) false error on constexpr Wrap2 b = {}; removed
2 gnu init/ref19 (rp, PR 49528) FAIL → PASS (lifetime extension)
2 clang drs/cwg6xx (fn) Core issue 616 cases no longer rejected
2 clang expr.const/p2-0x (fn) read of a member of a volatile prvalue now diagnosed, as Clang expects
2 clang SemaCXX/cxx2c-placeholder-vars (fn) "expression has no effect" on S{}._ no longer issued in GNU mode, as in strict mode
2 clang SemaCXX/constant-expression-cxx2a (fn) same diagnostics; a note caret moves from .z1 to .n
2 gnu cpp0x/constexpr-delegating3 (fp) still FAIL (delegation to a defaulted constructor, a separate bug); "access to uninitialized object" instead of a garbage value. The 32-bit recordings are deleted, since they are now identical to default.1.1.txt

Version gating

Checked on Compiler Explorer with -std=c++14: A().b is a prvalue in GCC 8.3 and Clang 3.9, and an xvalue in GCC 9.2 and Clang 4.0 and later.

Tests

Test Type Checks
regressions/gh-90.sft.cpp rp lifetime of A().b, A().i[0], A().*pm, A().i: C++14/C++20, strict, g++ 9/15, Clang 4/19
regressions/gh-90-old-modes.sft.cpp fp A().b is still a prvalue when emulating GCC 8.3 and Clang 3.9, just below the version thresholds (fails for GCC 9.0 and Clang 4.0)
regressions/gh-90-warn.sft.cpp fp return-to-temporary warnings kept (strict, g++ 15/8, Clang 19); no false positives
cwg/cwg616.sft.cpp fp Core issue 616 conformance test (fails in g++ 8 / Clang 3.9 emulation)
  • The new tests fail on main and pass with this change (edg-docker-test, edg_x86_64 and edg_x86_64_cp).
  • The recordings were created with edg-docker-test -W, and the default.* files were reviewed.
  • Full suite on GitHub Actions (Build & Test CI, all four configurations): passes on 4bea665 (run 40175117293), rebased on main 35b7c47. Strict Build Check, Python Typecheck and Pending Changes Entries also pass.

Not in this PR

Found while working on this; to be raised separately:

@Momen-G-Ar
Momen-G-Ar force-pushed the gh-90-lifetime-extension branch 5 times, most recently from 97f2958 to c0cc782 Compare October 19, 2026 14:44
@Momen-G-Ar Momen-G-Ar closed this Oct 19, 2026
@Momen-G-Ar Momen-G-Ar reopened this Oct 19, 2026
@Momen-G-Ar Momen-G-Ar closed this Oct 19, 2026
@Momen-G-Ar Momen-G-Ar reopened this Oct 19, 2026
@Momen-G-Ar
Momen-G-Ar force-pushed the gh-90-lifetime-extension branch 4 times, most recently from 45ac3ef to 8cd6c8b Compare October 20, 2026 17:26
@Momen-G-Ar Momen-G-Ar changed the title Fix lifetime extension through members and array elements of temporaries [GH #90] Lifetime extension through a member of a temporary in GNU and Clang modes [GH #90] Oct 20, 2026
@Momen-G-Ar
Momen-G-Ar force-pushed the gh-90-lifetime-extension branch from 8cd6c8b to c79d58b Compare October 20, 2026 17:48
edgcpp#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.
@Momen-G-Ar
Momen-G-Ar force-pushed the gh-90-lifetime-extension branch from c79d58b to a568d25 Compare October 25, 2026 07:28
@Momen-G-Ar Momen-G-Ar closed this Oct 25, 2026
@Momen-G-Ar Momen-G-Ar reopened this Oct 25, 2026
Under Core issue 616, selecting a member of a class prvalue yields an
xvalue, and binding a reference to it extends the lifetime of the whole
temporary.  GNU and Clang modes kept the C++11 rule (a prvalue), so

  const int &r = A().b;

bound to a copy of the member and destroyed A() at the end of the
full-expression.  GCC 9 and Clang 4 implement the issue, so cmd_line.c now
enables selection_from_prvalue_is_xvalue when emulating those versions or
later.  This reapplies the change backed out for EDGcpfe/29032.

That change regressed Clang's SemaCXX/constant-expression-cxx2a because,
for "." and ".*" on a class prvalue, the interpreter marked the whole
prvalue as initialized (eok_dot_field and eok_pm_field in interpret.c).
Since C++20 a constexpr constructor can leave members uninitialized, so
"Y(0,0).z1.n" folded to an indeterminate value instead of being diagnosed.
The marking is removed; the previous commit fixed the one case where the
per-subobject state was incomplete.  The same bug already affected strict
C++20 mode ("static_assert(W().n == 0);" was accepted).

is_glvalue_for_auto_object and is_address_of_auto_object (exprutil.c) now
look through such an xvalue to the class prvalue, so "return A().b;" still
warns about returning a reference to a local temporary.  The
traverse_addressing_subtree comments (il_walk.c) are updated to match.

Tests: regressions/edgcppgh-90 (lifetime, including the GCC 9 and Clang 4
modes), regressions/edgcppgh-90-old-modes ("A().b" is still a prvalue for GCC
8.3 and Clang 3.9), regressions/edgcppgh-90-warn (warnings), cwg/cwg616.  Updated
expectations: gnu init/ref19 now passes; clang drs/cwg6xx no longer
rejects the Core issue 616 cases; clang expr.const/p2-0x now diagnoses a
read of a volatile prvalue's member; clang cxx2c-placeholder-vars loses an
"expression has no effect" warning on "S{}._", as in strict mode; a note
caret moves to the member in SemaCXX/constant-expression-cxx2a; gnu
cpp0x/constexpr-delegating3 (delegation to a defaulted constructor, a
separate bug) still fails, but with "access to uninitialized object"
instead of a garbage value.
@Momen-G-Ar
Momen-G-Ar force-pushed the gh-90-lifetime-extension branch from a568d25 to 4bea665 Compare October 25, 2026 07:54
@Momen-G-Ar Momen-G-Ar closed this Oct 25, 2026
@Momen-G-Ar Momen-G-Ar reopened this Oct 25, 2026
@Momen-G-Ar Momen-G-Ar closed this Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant