From: Marek Polacek <polacek@redhat.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>, Jason Merrill <jason@redhat.com>
Subject: [PATCH] c++: ICE with template code in constexpr [PR104284]
Date: Thu, 10 Mar 2022 17:04:59 -0500 [thread overview]
Message-ID: <20220310220459.91301-1-polacek@redhat.com> (raw)
Since r9-6073 cxx_eval_store_expression preevaluates the value to
be stored, and that revealed a crash where a template code (here,
code=IMPLICIT_CONV_EXPR) leaks into cxx_eval*.
It happens because we're performing build_vec_init while processing
a template, which calls get_temp_regvar which creates an INIT_EXPR.
This INIT_EXPR's RHS contains an rvalue conversion so we create an
IMPLICIT_CONV_EXPR. Its operand is not type-dependent and the whole
INIT_EXPR is not type-dependent. So we call build_non_dependent_expr
which, with -fchecking=2, calls fold_non_dependent_expr. At this
point the expression still has an IMPLICIT_CONV_EXPR, which ought to
be handled in instantiate_non_dependent_expr_internal. However,
tsubst_copy_and_build doesn't handle INIT_EXPR; it will just call
tsubst_copy which does nothing when args is null. So we fail to
replace the IMPLICIT_CONV_EXPR and ICE.
Eliding the IMPLICIT_CONV_EXPR in this particular case would be too
risky, so we could do
if (TREE_CODE (t) == INIT_EXPR)
t = TREE_OPERAND (t, 1);
in fold_non_dependent_expr, but that feels too ad hoc. So it might
make sense to actually take care of INIT_EXPR in tsubst_c_and_b.
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/11?
PR c++/104284
gcc/cp/ChangeLog:
* pt.cc (tsubst_copy_and_build): Handle INIT_EXPR.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1y/constexpr-104284.C: New test.
---
gcc/cp/pt.cc | 8 ++++++++
gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C | 17 +++++++++++++++++
2 files changed, 25 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index f7ee33a6dfd..e8920f98e4d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -21289,6 +21289,14 @@ tsubst_copy_and_build (tree t,
with constant operands. */
RETURN (t);
+ case INIT_EXPR:
+ {
+ tree op0 = RECUR (TREE_OPERAND (t, 0));
+ tree op1 = RECUR (TREE_OPERAND (t, 1));
+ RETURN (build2_loc (input_location, INIT_EXPR, TREE_TYPE (op0),
+ op0, op1));
+ }
+
case NON_LVALUE_EXPR:
case VIEW_CONVERT_EXPR:
if (location_wrapper_p (t))
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C
new file mode 100644
index 00000000000..f60033069e4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284.C
@@ -0,0 +1,17 @@
+// PR c++/104284
+// { dg-do compile { target c++14 } }
+// { dg-additional-options "-fchecking=2" }
+
+struct S {
+ char c{};
+};
+
+auto x = [](auto) { constexpr S s[]{{}}; };
+
+template<class>
+constexpr void gn ()
+{
+ constexpr S s[]{{}};
+}
+
+static_assert ((gn<int>(), true), "");
base-commit: b5417a0ba7e26bec2abf05cad6c6ef840a9be41c
--
2.35.1
next reply other threads:[~2022-03-10 22:05 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-10 22:04 Marek Polacek [this message]
2022-03-10 22:27 ` Marek Polacek
2022-03-11 23:46 ` Jason Merrill
2022-03-18 21:55 ` [PATCH v2] " Marek Polacek
2022-03-24 15:40 ` Jason Merrill
2022-03-24 21:53 ` [PATCH v3] " Marek Polacek
2022-03-24 22:14 ` Jason Merrill
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=20220310220459.91301-1-polacek@redhat.com \
--to=polacek@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
/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).