Repository navigation
Bind an NSDMI's implicit this to the right aggregate [GH #324] - #408
Open
Momen-G-Ar wants to merge 1 commit into
Open
Momen-G-Ar wants to merge 1 commit into
Momen-G-Ar wants to merge 1 commit into
Conversation
When lowering a non-constant aggregate initialization, lower_param_ref
replaced the enk_param_ref for "this" with the innermost aggregate on
aggregate_this_stack whose type matched. A default member initializer
that aggregate-initializes another object of the same class then bound
the implicit "this" of its explicit elements to that nested object:
struct Y { unsigned k; unsigned n = Y{2, k}.n; };
Y *p = new Y{1}; // p->n was 2, not 1.
Only an element that comes from a default member initializer (marked
implicit_aggr_element) can refer to the aggregate it initializes, which
is also the rule the constexpr interpreter uses. An aggregate is now
skipped in the search while one of its explicit elements is lowered;
if nothing else matches, the previous search is used.
Collaborator
|
AcknowlEDG review results updated for
CI other. |
Collaborator
|
AcknowlEDG review results updated for
CI success. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the bug reported in discussion #324 (no issue exists yet; the commit and Changes entry are tagged
[GH #324]after the discussion, and I'll retag them once an issue is opened): when the default member initializer of an aggregate class aggregate-initializes another object of the same class, the implicitthisin the elements of that nested initializer referred to the nested object instead of to the object being initialized.khere isthis->k, andthisis the aggregate whose element is initialized from the default member initializer ([dcl.init.aggr]/5.1). g++ and Clang given == 1.Y y{1};,Y a = Y{1};(folded to a constant by the front end)new Y{1},return Y{1};, inner object passed by value or byconst Y &(lowered)thisbound to the nestedY{2, k}Y y{v};, a global,return {v};, a mem-initializer, a member of another aggregate, a class templatethisbound to the nestedY{2, k}Same result in strict,
--g++and--clangmodes.Root cause
lower_param_ref(lower_il.c) replaces theenk_param_refforthisin a non-constant aggregate initialization with the innermost entry onaggregate_this_stackwhose type matches. A comment there assumed that "there can only be one". In the example, the innerY{2, k}pushes a secondYentry (inlower_dynamic_init_aggregate_constant,lower_init.c) on top of the outer one before its explicit elementkis lowered, sothisbound to the inner temporary.Change
Only an element that comes from a default member initializer, which the front end marks with
implicit_aggr_element, can refer to the aggregate it initializes. An element written in the braces refers to thethisof the enclosing context. The constexpr interpreter already uses this rule (interpret.c,set_up_param_ref_for_this_ptris called only forimplicit_aggr_elementelements).lower_il.h: newan_init_pos_descr::not_this_candidateflag, cleared inclear_init_pos_descr.lower_init.c:lower_dynamic_init_aggregate_constantsets the flag on its stack entry while an explicit element is lowered. Anonymous unions are not affected, since their members' default member initializers refer to the enclosing class.lower_il.c: the stack search moves toreplace_param_ref_with_aggregate_this. It skips flagged entries first; if nothing matches, it repeats the previous search, so any case not covered gives the same result as before.Alternatives considered
Wcase in the test).enk_param_refwhich aggregate it belongs to, in the front end. Larger and more intrusive, for a problem that exists only in the lowering; the front end and the constexpr interpreter already have the information they need (implicit_aggr_element).The fallback to the previous search keeps any unforeseen path (an NSDMI-derived element without
implicit_aggr_element) behaving exactly as before, instead of reachingctor_init_thisor an internal error.Changes entry:
src/pending_changes/gh-324-nsdmi-this-nested-aggregate.txt(passesedg-collect-changes --check). Based onmainat 35b7c47; merges cleanly with the currentmain. The commit message passesedg-check-commit-messages.Tests
regressions/gh-324.sft.cppnew, return, by-value and by-reference cases; foldedY y{1}; run-time values (local, global,return {v}, by-value argument, mem-initializer, member of another aggregate, class template); the inner object's own default member initializer still binds to the inner object (W); anonymous union, aggregate base class, lambda capturingthis, arraynew.--c++20strict,--g++ 15,--clangimported/gnu/cpp/cpp1y/pr79937-4.sft.cpp--c++14. It was expected to fail and now passes; itsFAILline is removed fromtests/expectations/edg_x86_64/imported.log.The
Wcheck is skipped in--clangmode: there, a member skipped by a designated initializer does not get its default member initializer (W{.k = 2, .n = 0}.jis 0). That happens without this change too and is the issue in discussion #259.mainand passes with this change (edg-docker-test,edg_x86_64andedg_x86_64_cp, all three variants).edg-docker-test -W, and thedefault.*files were reviewed.--g++,--clang; C and C++ generating back ends) gives the same output as g++ and Clang with this change, and differs in every mode without it.this) match their expectations inedg_x86_64andedg_x86_64_cp.Build & Test CI, all four configurations): passes on e7c7820 (run 40556389927). Strict Build Check, Python Typecheck and Pending Changes Entries also pass.Discussion: #324
Summary by CodeRabbit
thisreferences are resolved in default member initializers when constructing nested aggregates of the same class. References now point to the object being initialized rather than an unintended nested object, including in runtime-valued initialization and return expressions.