From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 2BBC83899018; Fri, 28 Jan 2022 04:38:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2BBC83899018 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-9521] c++: nested catch in ctor fn-try-block [PR61611] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 2aa4400cb9830da0a335a8cbb75183706714b28d X-Git-Newrev: cfcf6459810b010e8af2798581967872f68f1178 Message-Id: <20220128043854.2BBC83899018@sourceware.org> Date: Fri, 28 Jan 2022 04:38:54 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Jan 2022 04:38:54 -0000 https://gcc.gnu.org/g:cfcf6459810b010e8af2798581967872f68f1178 commit r11-9521-gcfcf6459810b010e8af2798581967872f68f1178 Author: Jason Merrill Date: Thu Jan 6 09:45:26 2022 -0500 c++: nested catch in ctor fn-try-block [PR61611] Being in_function_try_handler isn't enough to satisfy the condition of reaching the end of such a handler; in this case, we're reaching the end of a handler within that handler, so we don't want the special semantics. PR c++/61611 gcc/cp/ChangeLog: * except.c (in_nested_catch): New. (expand_end_catch_block): Check it. gcc/testsuite/ChangeLog: * g++.dg/eh/ctor-fntry1.C: New test. Diff: --- gcc/cp/except.c | 20 +++++++++++++++++++- gcc/testsuite/g++.dg/eh/ctor-fntry1.C | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/gcc/cp/except.c b/gcc/cp/except.c index bc260d145d1..f53718c55f4 100644 --- a/gcc/cp/except.c +++ b/gcc/cp/except.c @@ -448,6 +448,23 @@ expand_start_catch_block (tree decl) return type; } +/* True if we are in a catch block within a catch block. Assumes that we are + in function scope. */ + +static bool +in_nested_catch (void) +{ + int catches = 0; + + /* Scan through the template parameter scopes. */ + for (cp_binding_level *b = current_binding_level; + b->kind != sk_function_parms; + b = b->level_chain) + if (b->kind == sk_catch + && ++catches == 2) + return true; + return false; +} /* Call this to end a catch block. Its responsible for emitting the code to handle jumping back to the correct place, and for emitting @@ -463,7 +480,8 @@ expand_end_catch_block (void) a handler of the function-try-block of a constructor or destructor. */ if (in_function_try_handler && (DECL_CONSTRUCTOR_P (current_function_decl) - || DECL_DESTRUCTOR_P (current_function_decl))) + || DECL_DESTRUCTOR_P (current_function_decl)) + && !in_nested_catch ()) { tree rethrow = build_throw (input_location, NULL_TREE); TREE_NO_WARNING (rethrow) = true; diff --git a/gcc/testsuite/g++.dg/eh/ctor-fntry1.C b/gcc/testsuite/g++.dg/eh/ctor-fntry1.C new file mode 100644 index 00000000000..0c783bb45ee --- /dev/null +++ b/gcc/testsuite/g++.dg/eh/ctor-fntry1.C @@ -0,0 +1,23 @@ +// PR c++/61611 +// { dg-do run } + +struct A { }; +struct B { }; + +struct Test +{ + Test() + try { throw A(); } + catch(const A&) + { + try { throw B(); } + catch(const B&) { } + } +}; + +int +main() +{ + try { Test x; } + catch(const A&) { } +}