From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 4E1133858D32; Tue, 26 Jul 2022 03:12:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E1133858D32 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 r12-8618] c++: aggregate prvalue as for range [PR106230] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 3387ec265151e6635e4b382c6023e898eb8bb08b X-Git-Newrev: 60954a06ceb1598d86763e5504d8e1b874b6bd6a Message-Id: <20220726031213.4E1133858D32@sourceware.org> Date: Tue, 26 Jul 2022 03:12:13 +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: Tue, 26 Jul 2022 03:12:13 -0000 https://gcc.gnu.org/g:60954a06ceb1598d86763e5504d8e1b874b6bd6a commit r12-8618-g60954a06ceb1598d86763e5504d8e1b874b6bd6a Author: Jason Merrill Date: Mon Jul 25 11:13:31 2022 -0400 c++: aggregate prvalue as for range [PR106230] Since my PR94041 work on temporary lifetime in aggregate initialization, we end up calling build_vec_init to initialize the reference-extended temporary for the artificial __for_range variable. And build_vec_init uses finish_for_stmt to implement its loop. That function assumes that if __for_range is in current_binding_level, we're finishing a range-for, and we should fix up the variable as it goes out of scope. But when called from build_vec_init we aren't finishing a range-for, and do_poplevel doesn't remove the variable from scope because stmts_are_full_exprs_p is false. So let's check that here as well, and leave the DECL_NAME alone. PR c++/106230 gcc/cp/ChangeLog: * semantics.cc (finish_for_stmt): Check stmts_are_full_exprs_p. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/range-for38.C: New test. Diff: --- gcc/cp/semantics.cc | 5 +++++ gcc/testsuite/g++.dg/cpp0x/range-for38.C | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index ab48f11c9be..972a1966f69 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -1398,6 +1398,11 @@ finish_for_stmt (tree for_stmt) add_stmt (do_poplevel (scope)); + /* If we're being called from build_vec_init, don't mess with the names of + the variables for an enclosing range-for. */ + if (!stmts_are_full_exprs_p ()) + return; + for (int i = 0; i < 3; i++) if (range_for_decl[i]) DECL_NAME (range_for_decl[i]) diff --git a/gcc/testsuite/g++.dg/cpp0x/range-for38.C b/gcc/testsuite/g++.dg/cpp0x/range-for38.C new file mode 100644 index 00000000000..39845b937c1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/range-for38.C @@ -0,0 +1,16 @@ +// PR c++/106230 +// { dg-do compile { target c++11 } } + +struct A { + A(); + operator int(); +}; +template struct array { + A elts[N]; + A *begin(); + A *end(); +}; +void fn() { + for (int i : array<4>{}) + ; +}