From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: aggregate prvalue as for range [PR106230]
Date: Mon, 25 Jul 2022 20:06:23 -0400 [thread overview]
Message-ID: <20220726000623.1183241-1-jason@redhat.com> (raw)
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.
Tested x86_64-pc-linux-gnu, applying to trunk.
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.
---
gcc/cp/semantics.cc | 5 +++++
gcc/testsuite/g++.dg/cpp0x/range-for38.C | 16 ++++++++++++++++
2 files changed, 21 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/range-for38.C
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 96037c21b85..16dea0593b0 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -1411,6 +1411,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 <int N> struct array {
+ A elts[N];
+ A *begin();
+ A *end();
+};
+void fn() {
+ for (int i : array<4>{})
+ ;
+}
base-commit: 4c6567b4090d9363f90b5c68e238e7d0964b6e89
--
2.31.1
reply other threads:[~2022-07-26 0:06 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220726000623.1183241-1-jason@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).