Skip to content

Missing returned object destructor call after exception throw #50865

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

Closed
llvmbot opened this issue Aug 18, 2021 · 3 comments
Closed

Missing returned object destructor call after exception throw #50865

llvmbot opened this issue Aug 18, 2021 · 3 comments
Labels
bugzilla Issues migrated from bugzilla c++ clang:codegen IR generation bugs: mangling, exceptions, etc. duplicate Resolved as duplicate

Comments

@llvmbot
Copy link
Member

llvmbot commented Aug 18, 2021

Bugzilla Link 51523
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor

Extended Description

14.3.2 Exception handling:Constructors and destructors:2 (https://eel.is/c++draft/except.ctor#2)
says
"If an exception is thrown during the destruction of temporaries or local variables for a return statement ([stmt.return]), the destructor for the returned object (if any) is also invoked"

And it contains example (https://eel.is/c++draft/except.ctor#example-1) with description
"...the local variable y is destroyed, causing stack unwinding, resulting in the destruction of the returned object, ..."

Trying to recreate this example with following code

#include
using namespace std;

struct S{
int i = 123;
S(int i) :i(i) {cout<<"S("<<i<<")"<<endl;}
~S() {cout<<"~S("<<i<<")"<<endl;}
};

struct T {
T() {cout<<"T()"<<endl;}
~T() noexcept(false) {
cout<<"~T()"<<endl;
throw 0;
}
};

S foo() {
try {
T t;
return {3};
} catch (...){}
return {4};
}

int main() {
foo();
}

Output doesn't have S{3}'s (returned object) destruction line:
T()
S(3)
~T()
S(4)
~S(4)

while should be
T()
S(3)
~T()
~S(3) <-----!
S(4)
~S(4)

Link to godbolt:
https://godbolt.org/z/YrasqsvMe

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@zygoloid
Copy link
Collaborator

Duplicate of #12658

@zygoloid zygoloid marked this as a duplicate of #12658 Sep 25, 2023
@zygoloid zygoloid closed this as not planned Won't fix, can't repro, duplicate, stale Sep 25, 2023
@Endilll Endilll added duplicate Resolved as duplicate c++ clang:codegen IR generation bugs: mangling, exceptions, etc. labels Sep 26, 2023
@llvmbot
Copy link
Member Author

llvmbot commented Sep 26, 2023

@llvm/issue-subscribers-c-1

| | | | --- | --- | | Bugzilla Link | [51523](https://llvm.org/bz51523) | | Version | trunk | | OS | Linux | | Reporter | LLVM Bugzilla Contributor |

Extended Description

14.3.2 Exception handling:Constructors and destructors:2 (https://eel.is/c++draft/except.ctor#2)
says
"If an exception is thrown during the destruction of temporaries or local variables for a return statement ([stmt.return]), the destructor for the returned object (if any) is also invoked"

And it contains example (https://eel.is/c++draft/except.ctor#example-1) with description
"...the local variable y is destroyed, causing stack unwinding, resulting in the destruction of the returned object, ..."

Trying to recreate this example with following code

#include <iostream>
using namespace std;

struct S{
int i = 123;
S(int i) :i(i) {cout<<"S("<<i<<")"<<endl;}
~S() {cout<<"~S("<<i<<")"<<endl;}
};

struct T {
T() {cout<<"T()"<<endl;}
~T() noexcept(false) {
cout<<"~T()"<<endl;
throw 0;
}
};

S foo() {
try {
T t;
return {3};
} catch (...){}
return {4};
}

int main() {
foo();
}

Output doesn't have S{3}'s (returned object) destruction line:
T()
S(3)
~T()
S(4)
~S(4)

while should be
T()
S(3)
~T()
~S(3) <-----!
S(4)
~S(4)

Link to godbolt:
https://godbolt.org/z/YrasqsvMe

@llvmbot
Copy link
Member Author

llvmbot commented Sep 26, 2023

@llvm/issue-subscribers-clang-codegen

| | | | --- | --- | | Bugzilla Link | [51523](https://llvm.org/bz51523) | | Version | trunk | | OS | Linux | | Reporter | LLVM Bugzilla Contributor |

Extended Description

14.3.2 Exception handling:Constructors and destructors:2 (https://eel.is/c++draft/except.ctor#2)
says
"If an exception is thrown during the destruction of temporaries or local variables for a return statement ([stmt.return]), the destructor for the returned object (if any) is also invoked"

And it contains example (https://eel.is/c++draft/except.ctor#example-1) with description
"...the local variable y is destroyed, causing stack unwinding, resulting in the destruction of the returned object, ..."

Trying to recreate this example with following code

#include <iostream>
using namespace std;

struct S{
int i = 123;
S(int i) :i(i) {cout<<"S("<<i<<")"<<endl;}
~S() {cout<<"~S("<<i<<")"<<endl;}
};

struct T {
T() {cout<<"T()"<<endl;}
~T() noexcept(false) {
cout<<"~T()"<<endl;
throw 0;
}
};

S foo() {
try {
T t;
return {3};
} catch (...){}
return {4};
}

int main() {
foo();
}

Output doesn't have S{3}'s (returned object) destruction line:
T()
S(3)
~T()
S(4)
~S(4)

while should be
T()
S(3)
~T()
~S(3) <-----!
S(4)
~S(4)

Link to godbolt:
https://godbolt.org/z/YrasqsvMe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++ clang:codegen IR generation bugs: mangling, exceptions, etc. duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

3 participants