Skip to content

Bind an NSDMI's implicit this to the constructed object in a constructor init [GH #324] - #540

Merged
daveedvdv-nvidia merged 1 commit into
edgcpp:mainfrom
Momen-G-Ar:gh-324-ctor-init-this
Nov 20, 2026
Merged

daveedvdv-nvidia merged 1 commit into
edgcpp:mainfrom
Momen-G-Ar:gh-324-ctor-init-this

Conversation

@Momen-G-Ar

@Momen-G-Ar Momen-G-Ar commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #408 for discussion #324. #408 fixed the implicit this of a default member initializer that aggregate-initializes another object of the same class, but only when the default member initializer is used by an aggregate initialization. When it is used by a constructor, this still bound to the nested temporary:

struct Y { unsigned k = 7; unsigned n = Y{2, k}.n; };  // n = this->k
struct D : Y { D() {} };
int main() {
  if (D().n != 7) abort();  // EDG: D().n == 2
}

k here is (*this).k ([class.mfct.non-static]/3, [expr.prim.id.general] in C++23). this names the object whose non-static data member's initializer is being evaluated ([expr.prim.this]/1), and Y's constructor initializes n from its default member initializer ([class.base.init]/9, [class.default.ctor]/4). So n is 7.

This affects every case where Y's constructor runs: D(), D() : Y() {}, a member Y y; of a class with a user-provided constructor, an array member, new Y[2], and Y y; when an initializer is not a constant (otherwise the front end folds Y y; to {7U, 7U}). Aggregate initialization (Y{}, D() : Y{} {}) was already correct.

Cause

lower_param_ref (lower_il.c) replaced the enk_param_ref for this with, in order:

  1. the innermost matching candidate on aggregate_this_stack (Bind an NSDMI's implicit this to the right aggregate [GH #324] #408 skips an aggregate while one of its explicit elements is lowered),
  2. any matching entry on the stack, including the skipped ones,
  3. ctor_init_this, the this parameter of the constructor init being lowered.

In Y's constructor, the temporary for Y{2, k} is skipped while its explicit element k is lowered, so step 1 finds nothing and step 2 takes that temporary. The lowered constructor read __T.n = __T.k; instead of __T.n = this->k;.

Change

lower_param_ref now tries the constructor's this (when its type matches) after the candidate search and before the search that includes the skipped aggregates. While a constructor init is lowered, every entry on the stack was pushed by it (constructor inits don't nest), and a skipped entry is never what an explicit element's this refers to, so the constructor's this is right whenever no candidate matches. The search that includes the skipped aggregates stays as the last resort outside constructor inits.

New test: regressions/gh-324-ctor-init.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed default member initializers that use implicit this, including when initializing another object of the same class. References in nested initialization now resolve to the object being constructed.
    • Prevented failures when a constructor’s this parameter type does not match a referenced parameter.
  • Tests
    • Added regression coverage across multiple C++ versions and construction scenarios, including nested aggregates and inheritance.

@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change updates how aggregate-initializer this references are resolved during constructor initialization. It adds regression cases for initializer forms and object contexts, with test configurations spanning C++14 through C++23.

Changes: Aggregate-initializer this resolution

Layer / File(s) Summary
Update this-reference resolution
src/lower_il.c, src/lower_init.c, src/pending_changes/gh-324-nsdmi-this-ctor-init.txt
lower_param_ref checks aggregate candidates first, then a type-matching constructor-init this, then all aggregate entries as a fallback. The comment and issue note describe the constructor-initialization case.
Regression coverage
tests/tests/regressions/gh-324-ctor-init.sft.cpp, tests/tests/regressions/.gh-324-ctor-init.rto/*
The regression test covers initializer forms and object contexts. Fixtures cover C++14 through C++23 and multiple compiler configurations.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • edgcpp/compiler#408: This PR changes aggregate lookup logic in the same lowering path and addresses the same reported implicit-this issue.

Suggested labels: bug

Suggested reviewers: darkarc, daveedvdv

Merge Risk: Minimal · up to 7438f

The change fixes implicit this binding in default member initializers that aggregate-initialize another object of the same class, so D().n now yields 7 instead of 2. A regression test covers this. 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: the NSDMI implicit this is now bound to the object being constructed in a constructor init, which matches the lower_il.c lookup reordering, the new GH #324 note, and…
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: the NSDMI implicit this is now bound to the object being constructed in a constructor init, which matches the lower_il.c lookup reordering, the new GH #324 note, and the regression test. It also names the issue reference, so a reviewer scanning history can see the primary change.

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 69298c9 and 7438f6b.

Files selected for processing: 18

  • src/lower_il.c
  • src/lower_init.c
  • src/pending_changes/gh-324-nsdmi-this-ctor-init.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.1.1.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.1.2.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.2.1.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.2.2.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.3.1.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.3.2.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.4.1.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.4.2.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.5.1.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.5.2.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.6.1.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.6.2.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.7.1.txt
  • tests/tests/regressions/.gh-324-ctor-init.rto/default.7.2.txt
  • tests/tests/regressions/gh-324-ctor-init.sft.cpp
Files skipped from review as they are similar to previous changes: 2

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

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 6e2811db851c:

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 6e2811db851c:

CI success.

@Momen-G-Ar
Momen-G-Ar marked this pull request as ready for review November 15, 2026 08:30
@Momen-G-Ar
Momen-G-Ar requested a review from a team November 15, 2026 08:30
Comment thread src/lower_il.c Resolved
// are used by a constructor (and not folded into a constant).
template <class T> struct Der : T { Der() {} };

// Nested aggregates of different classes (PR c++/100252).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's that c++/100252 referring to? A GCC bug number? Why is that relevant?

@Momen-G-Ar Momen-G-Ar Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a GCC bug; its fix added the nsdmi-aggr14 to nsdmi-aggr19 tests, and this case has the same shape

https://gcc.gnu.org/pipermail/gcc-patches/2022-May/594855.html

@nv-cmeerw

Copy link
Copy Markdown
Contributor

I don't think the "Tests" section in the PR description is particularly useful. If the test needs to be clarified, it should be in the test itself, not in the PR description. And I don't really want to know what tests your AI agent ran before submitting the PR (all tests need to pass anyway). Same for things like "The commit message passes edg-check-commit-messages."

@Momen-G-Ar
Momen-G-Ar force-pushed the gh-324-ctor-init-this branch from 6e2811d to 69298c9 Compare November 17, 2026 20:05
@Momen-G-Ar

Copy link
Copy Markdown
Contributor Author

Thanks, I removed the Tests section and the notes about which checks were run. The description now covers the problem, the standard references, the cause and the change.

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 69298c96407d:

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 69298c96407d:

CI success.

@daveedvdv-nvidia daveedvdv-nvidia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall.

Only wording changes needed as far as I'm concerned.

Comment thread src/lower_il.c Outdated
initialized by "Y{2, k}". The aggregates for which an explicitly specified
element is being lowered are therefore skipped.
element is being lowered are therefore skipped. When no other aggregate
matches, the "this" of a constructor init comes next, as in:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/the "this" of/the "this" parameter of/

Comment thread src/lower_il.c Resolved
…tor init [GH edgcpp#324]

When a default member initializer that aggregate-initializes another
object of the same class was used by a constructor, lower_param_ref
searched every aggregate on aggregate_this_stack, including the ones
skipped because one of their explicit elements was being lowered, before
it tried the "this" parameter of the constructor init:

  struct Y { unsigned k = 7; unsigned n = Y{2, k}.n; };
  struct D : Y { D() {} };  // D().n was 2, not 7.

The "this" parameter of the constructor init is now used before the
skipped aggregates are searched.
@Momen-G-Ar
Momen-G-Ar force-pushed the gh-324-ctor-init-this branch from 69298c9 to 7438f6b Compare November 20, 2026 09:16
@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 7438f6bd9990:

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 7438f6bd9990:

CI success.

@daveedvdv-nvidia daveedvdv-nvidia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@daveedvdv-nvidia
daveedvdv-nvidia added this pull request to the merge queue Nov 20, 2026
Merged via the queue into edgcpp:main with commit 69d1f4f Nov 20, 2026
22 checks passed
@Momen-G-Ar
Momen-G-Ar deleted the gh-324-ctor-init-this branch November 20, 2026 15:39
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.

4 participants