Skip to content

Keep union active members when zero-initializing in constant evaluation [GH #618] - #615

Open
jonatanlinden wants to merge 1 commit into
edgcpp:mainfrom
jonatanlinden:fix/constexpr-union-zero-init-assign
Open

jonatanlinden wants to merge 1 commit into
edgcpp:mainfrom
jonatanlinden:fix/constexpr-union-zero-init-assign

Conversation

@jonatanlinden

@jonatanlinden jonatanlinden commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

This addresses the issue described in discussion #537, I'll update the PR once it has gotten an issue.

Problem

In constant evaluation, assigning a value-initialized temporary to an object of class type, x = R(), loses the active member of every union subobject. Reading the first union member afterwards is diagnosed as "invalid access to inactive member ... (no field is active)". Zero-initialization should make the first named member of each union active (n4868 [dcl.init.general]/6.3).

Fix

In interpret.c, call mark_complete_class_object_if_needed, before init_subobject_to_zero zero-initializes each member. Now, init_subobject_to_zero is called first, even though the comment of mark_complete_class_object_if_needed says "must therefore be invoked before placing a result in the indicated storage, because that result could set the active field".

Testing

  • tests/tests/regressions/constexpr-union-zero-init-assign.sft.cpp (fp, --c++20)
    • x = R{}, R x = R();, nested unions, and a derived class whose base has a union member are accepted as well.
  • constexpr-union-zero-init-assign-negative.sft.cpp (fn, --c++20)
    • Reading the inactive member is still rejected, and the diagnostic names the active member.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed C++20 constant evaluation for assignments involving value-initialized objects with unions. The first union member is correctly treated as active, allowing valid constant expressions while continuing to reject reads of inactive members.
  • Tests
    • Added regression coverage for named and anonymous unions, arrays, and nested objects, including checks for both accepted constant expressions and invalid reads of inactive members.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The zero-initialization path now marks complete class objects before initializing array elements, class fields, and the first union field. C++20 regression tests cover union assignment from value-initialized temporaries and access to an inactive union member.

Changes: Constexpr union assignment

Layer / File(s) Summary
Mark objects before subobject initialization
src/interpret.c
The interpreter now marks each array element, class field, and first union field before zero-initializing it.
Test constexpr union assignment
tests/tests/regressions/constexpr-union-zero-init-assign.sft.cpp, tests/tests/regressions/constexpr-union-zero-init-assign-negative.sft.cpp, tests/tests/regressions/.constexpr-union-zero-init-assign.rto/default.1.1.txt, tests/tests/regressions/.constexpr-union-zero-init-assign-negative.rto/default.1.1.txt, src/pending_changes/gh-618-constexpr-union-zero-init-assign.txt
The positive C++20 tests check zero-valued union members after assignment from value-initialized temporaries across several union forms. The negative test checks that reading an inactive member is not a constant expression. Test configuration, expected output, and an issue note document these cases.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

Suggested labels: bug

Suggested reviewers: daveedvdv-nvidia, daveedvdv

Merge Risk: Minimal · up to f5090

This fixes constant evaluation losing the active union member after assigning a value-initialized temporary, and adds C++20 regression tests. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: zero-initialization during constant evaluation now keeps union active members. The raw summary confirms the reordering in init_subobject_to_zero so complete-class meta…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: zero-initialization during constant evaluation now keeps union active members. The raw summary confirms the reordering in init_subobject_to_zero so complete-class metadata is set before zero-initialization. The objectives describe the same fix for GH #618 and PR #615, and the added tests cover unions in arrays, nested structs, and base classes. The title is specific and matches the changeset, and it needs no search.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c66e8f6 and f50906a.

Files selected for processing: 6

  • src/interpret.c
  • src/pending_changes/gh-618-constexpr-union-zero-init-assign.txt
  • tests/tests/regressions/.constexpr-union-zero-init-assign-negative.rto/default.1.1.txt
  • tests/tests/regressions/.constexpr-union-zero-init-assign.rto/default.1.1.txt
  • tests/tests/regressions/constexpr-union-zero-init-assign-negative.sft.cpp
  • tests/tests/regressions/constexpr-union-zero-init-assign.sft.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 0eabdab348a1:

CI success.

@jonatanlinden jonatanlinden changed the title Keep union active members when zero-initializing in constant evaluation Keep union active members when zero-initializing in constant evaluation [GH #618] Nov 30, 2026
@jonatanlinden
jonatanlinden force-pushed the fix/constexpr-union-zero-init-assign branch from 0eabdab to c66e8f6 Compare November 30, 2026 08:19
@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for c66e8f661474:

CI success.

@jonatanlinden
jonatanlinden marked this pull request as ready for review November 30, 2026 13:25
@jonatanlinden
jonatanlinden requested a review from a team November 30, 2026 13:25
…on. [GH edgcpp#618]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonatanlinden
jonatanlinden force-pushed the fix/constexpr-union-zero-init-assign branch from c66e8f6 to f50906a Compare November 30, 2026 13:39
@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for f50906ac758b:

CI success.

This branch has not been deployed

No deployments
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.

2 participants