Repository navigation
Do not destroy subobjects of a thrown aggregate twice [GH #553] #566
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
lexx42xxel
wants to merge
4
commits into
edgcpp:main
Choose a base branch
from
lexx42xxel:fix/throw-aggregate-subobject-cleanup
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ae72c18
Do not destroy subobjects of a thrown aggregate twice [GH #553]
lexx42xxel e679749
Test temporaries in thrown aggregates; assert one context [GH #553]
lexx42xxel 6517dc3
Use dyn_init_is in the thrown-aggregate cleanup branch [GH #553]
lexx42xxel c56402e
Keep cleanups across a constant lambda in a thrown object [GH #553]
lexx42xxel 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
10 changes: 10 additions & 0 deletions
10
src/pending_changes/gh-553-throw-aggregate-subobject-cleanup.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,10 @@ | ||
| [GH #553] | ||
| Subobjects of a thrown aggregate destroyed twice | ||
|
|
||
| When an aggregate with class members that have destructors was thrown, e.g., | ||
|
|
||
| struct H { E a, b; }; | ||
| throw H{E(7)}; | ||
|
|
||
| the members were destroyed during stack unwinding and again when the | ||
| exception object was destroyed. |
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.1.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 @@ | ||
| eccp -DTEST_NUMBER=1 --c++14 --exceptions Test_name.c |
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.2.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 @@ | ||
| ./a.out |
95 changes: 95 additions & 0 deletions
95
tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp
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,95 @@ | ||
| //type:rp | ||
| //options_all:--c++17 --exceptions | ||
|
|
||
| // Throwing an aggregate whose initialization has cleanups for partially | ||
| // constructed subobjects (e.g., "throw H{E(1)}" with two class members). | ||
| // Once the exception object is completely initialized, those cleanups must | ||
| // no longer be active: before the fix, they were still active when the | ||
| // exception was thrown, so the subobjects were destroyed during unwinding | ||
| // and again when the exception object was destroyed. Temporaries with | ||
| // destructors in the initializer must still be destroyed (once). | ||
|
|
||
| int live, tlive, errors; | ||
|
|
||
| struct E { | ||
| int v; | ||
| E(int x = -1) : v(x) { ++live; } | ||
| E(const E &o) : v(o.v) { ++live; } | ||
| ~E() { if (--live < 0) ++errors; } | ||
| }; | ||
| struct Thrower { | ||
| Thrower() { throw 0; } | ||
| ~Thrower() { ++errors; } | ||
| }; | ||
| struct T { | ||
| int v; | ||
| T(int x) : v(x) { ++tlive; } | ||
| T(const T &o) : v(o.v) { ++tlive; } | ||
| ~T() { if (--tlive < 0) ++errors; } | ||
| }; | ||
| static int tv(const T &t) { return t.v; } | ||
| static E te(const T &t) { return E(t.v); } | ||
|
|
||
| struct H2 { E a, b; }; | ||
| struct H4 { E a[2]; }; | ||
| struct H5 { E a[2]; E b; }; | ||
| struct H6 { E a[2][2]; }; | ||
| struct L { E a; E b; int i; }; | ||
| struct L2 { int i; E a; E b; }; | ||
| struct L3 { E a; int i; E b; }; | ||
| struct M { L l; E c; }; | ||
| struct X { E a; Thrower t; }; | ||
| struct X2 { E a; int i; Thrower t; }; | ||
| template <class F> struct LF { E a; F f; int i; }; | ||
| template <class F> LF(E, F, int) -> LF<F>; | ||
| template <class F> struct XF { E a; F f; Thrower t; }; | ||
| template <class F> XF(E, F) -> XF<F>; | ||
|
|
||
| template <class F> static void run(F f) { | ||
| live = 0; | ||
| tlive = 0; | ||
| try { f(); } catch (...) {} | ||
| if (live != 0 || tlive != 0) ++errors; | ||
| } | ||
|
|
||
| int main() { | ||
| run([] { throw H2{E(7)}; }); | ||
| run([] { throw H2{E(7), E(8)}; }); | ||
| run([] { throw H4{{E(7)}}; }); | ||
| run([] { throw H5{{E(7)}}; }); | ||
| run([] { throw H5{{E(1), E(2)}, E(3)}; }); | ||
| run([] { throw H6{{{E(7)}}}; }); | ||
| run([] { throw L{E(1), E(2), tv(T(3))}; }); | ||
| run([] { throw H2{E(tv(T(7)))}; }); | ||
| run([] { throw L2{tv(T(3)), E(1), E(2)}; }); | ||
| run([] { throw L3{E(1), tv(T(3)), E(2)}; }); | ||
| run([] { throw H2{te(T(7)), te(T(8))}; }); | ||
| run([] { throw H2{E(1), te(T(8))}; }); | ||
| run([] { throw H5{{E(1), te(T(2))}, te(T(3))}; }); | ||
| run([] { throw M{{E(1), E(2), tv(T(3))}, te(T(4))}; }); | ||
| run([] { throw M{{E(1), te(T(2)), tv(T(3))}, E(4)}; }); | ||
| run([] { E x(1), y(2); throw [x, y] { return x.v + y.v; }; }); | ||
| run([] { E x(1); throw [x, y = te(T(2))] { return x.v + y.v; }; }); | ||
| // An enclosing local must still be destroyed (once) during unwinding. | ||
| run([] { E outer(1); throw H2{E(7)}; }); | ||
| run([] { E outer(1); throw L3{E(1), tv(T(3)), te(T(4))}; }); | ||
| // A subobject initializer that throws: the subobjects constructed so far | ||
| // must be destroyed (once), and no exception object is thrown. | ||
| run([] { throw X{E(1)}; }); | ||
| run([] { throw X2{E(1), tv(T(3))}; }); | ||
| // A lambda whose captures are all constant is initialized as a constant | ||
| // (without cleanups of its own); that must leave the cleanups of the | ||
| // enclosing initialization alone. | ||
| run([] { throw LF{E(1), [k = 1] { return k; }, 0}; }); | ||
| run([] { throw XF{E(1), [k = 1] { return k; }}; }); | ||
| run([] { L3{E(1), (throw [k = 1] { return k; }, 0), E(2)}; }); | ||
| // The cleanup state after a caught exception must be intact. | ||
| run([] { | ||
| E outer(1); | ||
| try { throw H4{{E(2)}}; } catch (const H4 &h) { | ||
| if (h.a[0].v != 2 || h.a[1].v != -1) ++errors; | ||
| } | ||
| throw H2{E(3)}; | ||
| }); | ||
| return errors != 0; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.