aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2020-04-17 15:47:15 -0400
committerMarek Polacek <polacek@redhat.com>2020-04-20 19:14:13 -0400
commitd419e176d74162af6513be0b3bc1031726543993 (patch)
treef62b0b8f150b75990e81d8030fb99d730ec6ec75
parenta3a4f6be0c7ac1536c4d1def14217840b04dd488 (diff)
downloadgcc-d419e176d74162af6513be0b3bc1031726543993.zip
gcc-d419e176d74162af6513be0b3bc1031726543993.tar.gz
gcc-d419e176d74162af6513be0b3bc1031726543993.tar.bz2
c++: Fix ICE with { } as template argument [PR94592]
As an extension (there should be a CWG about this though), we support braced-init-list as a template argument, but convert_nontype_argument had trouble digesting them. We ICEd because of the double coercion we perform for template arguments: convert_nontype_argument called from finish_template_type got a { }, and since a class type was involved and we were in a template, convert_like created an IMPLICIT_CONV_EXPR. Then the second conversion of the same argument crashed in constexpr.c because the IMPLICIT_CONV_EXPR had gotten wrapped in a TARGET_EXPR. Another issue was that an IMPLICIT_CONV_EXPR leaked to constexpr.c when building an aggregate init. We should have instantiated the IMPLICIT_CONV_EXPR in the first call to convert_nontype_argument, but we didn't, because the call to is_nondependent_constant_expression returned false because it checks !BRACE_ENCLOSED_INITIALIZER_P. Then non_dep was false even though the expression didn't contain anything dependent and we didn't instantiate it in convert_nontype_argument. To fix this, check BRACE_ENCLOSED_INITIALIZER_P in cxx_eval_outermost_constant_expr rather than in is_nondependent_*. PR c++/94592 * constexpr.c (cxx_eval_outermost_constant_expr): Return when T is a BRACE_ENCLOSED_INITIALIZER_P. (is_nondependent_constant_expression): Don't check BRACE_ENCLOSED_INITIALIZER_P. (is_nondependent_static_init_expression): Likewise. * g++.dg/cpp2a/nontype-class34.C: New test. * g++.dg/cpp2a/nontype-class35.C: New test.
-rw-r--r--gcc/cp/ChangeLog9
-rw-r--r--gcc/cp/constexpr.c8
-rw-r--r--gcc/testsuite/ChangeLog6
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/nontype-class34.C16
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/nontype-class35.C17
5 files changed, 54 insertions, 2 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 797984b..53daab1 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,12 @@
+2020-04-17 Marek Polacek <polacek@redhat.com>
+
+ PR c++/94592
+ * constexpr.c (cxx_eval_outermost_constant_expr): Return when T is
+ a BRACE_ENCLOSED_INITIALIZER_P.
+ (is_nondependent_constant_expression): Don't check
+ BRACE_ENCLOSED_INITIALIZER_P.
+ (is_nondependent_static_init_expression): Likewise.
+
2020-04-20 Patrick Palka <ppalka@redhat.com>
PR c++/94628
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index c8e7d08..6b3e514 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -6534,6 +6534,12 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
bool non_constant_p = false;
bool overflow_p = false;
+ if (BRACE_ENCLOSED_INITIALIZER_P (t))
+ {
+ gcc_checking_assert (allow_non_constant);
+ return t;
+ }
+
constexpr_global_ctx global_ctx;
constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
allow_non_constant, strict,
@@ -8295,7 +8301,6 @@ bool
is_nondependent_constant_expression (tree t)
{
return (!type_unknown_p (t)
- && !BRACE_ENCLOSED_INITIALIZER_P (t)
&& is_constant_expression (t)
&& !instantiation_dependent_expression_p (t));
}
@@ -8307,7 +8312,6 @@ bool
is_nondependent_static_init_expression (tree t)
{
return (!type_unknown_p (t)
- && !BRACE_ENCLOSED_INITIALIZER_P (t)
&& is_static_init_expression (t)
&& !instantiation_dependent_expression_p (t));
}
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 813f94c..e733cae 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2020-04-17 Marek Polacek <polacek@redhat.com>
+
+ PR c++/94592
+ * g++.dg/cpp2a/nontype-class34.C: New test.
+ * g++.dg/cpp2a/nontype-class35.C: New test.
+
2020-04-20 Patrick Palka <ppalka@redhat.com>
PR c++/94628
diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class34.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class34.C
new file mode 100644
index 0000000..2d3ba01
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class34.C
@@ -0,0 +1,16 @@
+// PR c++/94592 - ICE with { } as template argument.
+// { dg-do compile { target c++2a } }
+
+struct A {
+ constexpr A() {}
+};
+
+template <A> struct B { };
+
+template<typename> void bar () {
+ B<{}> var;
+}
+
+void fu() {
+ bar<int>();
+}
diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class35.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class35.C
new file mode 100644
index 0000000..78cf0a3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class35.C
@@ -0,0 +1,17 @@
+// PR c++/94592 - ICE with { } as template argument.
+// { dg-do compile { target c++2a } }
+
+struct A {
+ int i;
+ constexpr A(int n) : i(n) {}
+};
+
+template <A a> struct B { int i; constexpr B() : i(a.i) { } };
+
+template<typename> void bar () {
+ B<{1}> var;
+}
+
+void fu() {
+ bar<int>();
+}