Repository navigation
Constant evaluation: find overriders below non-overriding classes [GH #421] #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daveedvdv-nvidia
wants to merge
1
commit into
main
Choose a base branch
from
fix/421
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−158
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
16 changes: 16 additions & 0 deletions
16
src/pending_changes/gh-421-virtual-call-indirect-overrider.txt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [GH #421] | ||
| Constant evaluation of virtual calls to an overrider in a derived class | ||
|
|
||
| During constant evaluation, a virtual function call did not find the final | ||
| overrider if the class of which it is a member is derived from the class of | ||
| the called function through an intermediate class that does not itself | ||
| override the function: The called function was not replaced. For example, | ||
| with --c++20: | ||
|
|
||
| struct A { constexpr virtual int f() const { return 1; } }; | ||
| struct NA : A {}; | ||
| struct B : NA { constexpr int f() const override { return 2; } }; | ||
| constexpr int g() { B b; const A* p = &b; return p->f(); } | ||
| static_assert(g() == 2); // Previously failed. | ||
|
|
||
| This is now fixed. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++20 Test_name.c |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //type:fp | ||
| //options:--c++20 | ||
| //remark:Constant evaluation: overrider below a class that does not override | ||
|
|
||
| struct A { | ||
| constexpr virtual int f() const { return 1; } | ||
| int a = 0; | ||
| }; | ||
| struct NA : A {}; | ||
| struct B : NA { | ||
| constexpr int f() const override { return 2; } | ||
| }; | ||
| constexpr int g1() { B b; const A* p = &b; return p->f(); } | ||
| static_assert(g1() == 2); // Previously failed. | ||
| constexpr int g1n() { B b; const NA* p = &b; return p->f(); } | ||
| static_assert(g1n() == 2); // Previously failed. | ||
|
|
||
| // Two non-overriding levels. | ||
| struct NNA : NA {}; | ||
| struct C : NNA { constexpr int f() const override { return 3; } }; | ||
| constexpr int g2() { C c; const A* p = &c; return p->f(); } | ||
| static_assert(g2() == 3); // Previously failed. | ||
|
|
||
| // The overrider is in a middle class, above a non-overriding one. | ||
| struct M : NA { constexpr int f() const override { return 4 + a; } }; | ||
| struct NM : M {}; | ||
| struct E { int e = 7; }; | ||
| struct T : E, NM {}; | ||
| constexpr int g3() { T t; t.a = 10; const A* p = &t; return p->f(); } | ||
| static_assert(g3() == 14); // Previously failed. | ||
|
|
||
| // Repeated base class: only one path overrides. | ||
| struct L : A { constexpr int f() const override { return 5 + a; } }; | ||
| struct R : A {}; | ||
| struct D : L, R {}; | ||
| constexpr int g4() { | ||
| D d; | ||
| static_cast<L&>(d).a = 1; | ||
| static_cast<R&>(d).a = 2; | ||
| const A* pl = static_cast<const L*>(&d); | ||
| const A* pr = static_cast<const R*>(&d); | ||
| return pl->f() * 10 + pr->f(); | ||
| } | ||
| static_assert(g4() == 61); | ||
| struct DD : D { | ||
| constexpr int f() const override { return 9; } | ||
| }; | ||
| constexpr int g5() { | ||
| DD d; | ||
| const A* pl = static_cast<const L*>(&d); | ||
| const A* pr = static_cast<const R*>(&d); | ||
| return pl->f() * 10 + pr->f(); | ||
| } | ||
| static_assert(g5() == 99); // Previously failed. | ||
|
|
||
| // Covariant return through a non-overriding intermediate class. | ||
| struct X { int x = 'X'; }; | ||
| struct Y : X { int y = 'Y'; }; | ||
| struct Z0 { int z = 0; }; | ||
| struct Q : Z0, Y { int q = 'Q'; }; | ||
| struct CA { constexpr virtual const X* h() const { return &xx; } X xx; }; | ||
| struct CN : CA {}; | ||
| struct CB : CN { | ||
| constexpr const Q* h() const override { return &qq; } | ||
| Q qq; | ||
| }; | ||
| constexpr bool g6() { | ||
| CB b; | ||
| const CA* p = &b; | ||
| return p->h() == static_cast<const X*>(&b.qq) && p->h()->x == 'X'; | ||
| } | ||
| static_assert(g6()); // Previously failed. | ||
|
|
||
| // Virtual call during construction of an intermediate class. | ||
| struct K : NA { | ||
| int r; | ||
| constexpr K() : r(static_cast<const A*>(this)->f()) {} | ||
| constexpr int f() const override { return 6; } | ||
| }; | ||
| struct NK : K {}; | ||
| struct KK : NK { constexpr int f() const override { return 8; } }; | ||
| constexpr int g7() { KK k; const A* p = &k; return k.r * 10 + p->f(); } | ||
| static_assert(g7() == 68); // Previously failed. | ||
| struct W : NA { | ||
| int r; | ||
| constexpr W() : r(static_cast<const A*>(this)->f()) {} | ||
| }; | ||
| struct WW : W { constexpr int f() const override { return 8; } }; | ||
| constexpr int g8() { WW w; const A* p = &w; return w.r * 10 + p->f(); } | ||
| static_assert(g8() == 18); // Previously failed. | ||
|
|
||
| constexpr B gb; | ||
| static_assert(static_cast<const A&>(gb).f() == 2); |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: edgcpp/compiler
Length of output: 29013
Script executed:
Repository: edgcpp/compiler
Length of output: 13207
Distinguish the active constructor from an unconstructed enclosing class.
The loop assigns
outer_classbefore checking initialization. DuringA::A()inB : A, this can leaveouter_classasBand selectB::f, although construction-time virtual dispatch must useA's overrider.The proposed reorder is not sufficient. During
K's constructor, theKsubobject can also be uninitialized while a call throughAmust selectK::f. Use the active constructor's dispatch class, not the initialization bit alone, and add a regression for theA-constructor/B-overrider case while preserving the existingK-constructor behavior.