public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-7805] c++: ICE with template code in constexpr [PR104284]
@ 2022-03-24 22:44 Marek Polacek
  0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2022-03-24 22:44 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:9fdac7e16c940fb6264e6ddaf99c761f1a64a054

commit r12-7805-g9fdac7e16c940fb6264e6ddaf99c761f1a64a054
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Mar 17 14:39:37 2022 -0400

    c++: ICE with template code in constexpr [PR104284]
    
    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.
    
    The problem is that we call build_vec_init in a template in the
    first place.  We can avoid doing so by checking p_t_d before
    calling build_aggr_init in check_initializer.
    
            PR c++/104284
    
    gcc/cp/ChangeLog:
    
            * decl.cc (check_initializer): Don't call build_aggr_init in
            a template.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1y/constexpr-104284-1.C: New test.
            * g++.dg/cpp1y/constexpr-104284-2.C: New test.
            * g++.dg/cpp1y/constexpr-104284-3.C: New test.
            * g++.dg/cpp1y/constexpr-104284-4.C: New test.

Diff:
---
 gcc/cp/decl.cc                                  |  4 +++
 gcc/testsuite/g++.dg/cpp1y/constexpr-104284-1.C | 34 ++++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp1y/constexpr-104284-2.C | 33 +++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp1y/constexpr-104284-3.C | 33 +++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp1y/constexpr-104284-4.C | 35 +++++++++++++++++++++++++
 5 files changed, 139 insertions(+)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 68741bbf5d2..69f60a6dc0f 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -7332,6 +7332,10 @@ check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
 	   && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
 		&& CP_AGGREGATE_TYPE_P (type)
 		&& (CLASS_TYPE_P (type)
+		    /* The call to build_aggr_init below could end up
+		       calling build_vec_init, which may break when we
+		       are processing a template.  */
+		    || processing_template_decl
 		    || !TYPE_NEEDS_CONSTRUCTING (type)
 		    || type_has_extended_temps (type))))
 	  || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-1.C
new file mode 100644
index 00000000000..809c26a6161
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-1.C
@@ -0,0 +1,34 @@
+// PR c++/104284
+// { dg-do compile { target c++14 } }
+// { dg-additional-options "-fchecking=2" }
+
+struct S {
+  char c{};
+};
+
+auto x1 = [](auto) { constexpr S s[]{{}}; };
+auto x2 = [](auto) { constexpr S s[]{{'a'}}; };
+#if __cpp_designated_initializers >= 201707L
+auto x3 = [](auto) { constexpr S s[]{{.c = 'a'}}; };
+#endif
+auto x4 = [](auto) { constexpr S s[]{'a'}; };
+auto x5 = [](auto) { constexpr S s[]{{{}}}; };
+
+template<class>
+constexpr void g ()
+{
+  constexpr S s1[]{{}};
+  static_assert(s1[0].c == '\0', "");
+  constexpr S s2[]{{'a'}};
+  static_assert(s2[0].c == 'a', "");
+#if __cpp_designated_initializers >= 201707L
+  constexpr S s3[]{{.c = 'a'}};
+  static_assert(s3[0].c == 'a', "");
+#endif
+  constexpr S s4[]{'a'};
+  static_assert(s4[0].c == 'a', "");
+  constexpr S s5[]{{{}}};
+  static_assert(s5[0].c == '\0', "");
+}
+
+static_assert ((g<int>(), true), "");
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-2.C
new file mode 100644
index 00000000000..704d37de129
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-2.C
@@ -0,0 +1,33 @@
+// PR c++/104284
+// { dg-do compile { target c++14 } }
+// { dg-additional-options "-fchecking=2" }
+
+struct S {
+  char a;
+  constexpr S() : a{'a'} { }
+  constexpr S(char a_) : a{a_} { }
+};
+
+auto x1 = [](auto) { constexpr S s[]{{}}; };
+auto x2 = [](auto) { constexpr S s[]{{'a'}}; };
+auto x3 = [](auto) { constexpr S s[]{'a'}; };
+auto x4 = [](auto) { constexpr S s[]{{{}}}; };
+
+template<typename>
+constexpr void g()
+{
+  constexpr S s1[]{{}};
+  static_assert(s1[0].a == 'a', "");
+  constexpr S s2[]{{'a'}};
+  static_assert(s2[0].a == 'a', "");
+  constexpr S s3[]{'a'};
+  static_assert(s3[0].a == 'a', "");
+  constexpr S s4[]{{{}}};
+  static_assert(s4[0].a == '\0', "");
+}
+
+void
+f ()
+{
+  g<int>();
+}
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-3.C
new file mode 100644
index 00000000000..6f23b255f9c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-3.C
@@ -0,0 +1,33 @@
+// PR c++/104284
+// { dg-do compile { target c++14 } }
+// { dg-additional-options "-fchecking=2" }
+// Like constexpr-104284.C, but the function template is not
+// constexpr.  In that case, we were still calling build_vec_init
+// in a template, just not crashing.
+
+struct S {
+  char c{};
+};
+
+template<class>
+void g ()
+{
+  constexpr S s1[]{{}};
+  static_assert(s1[0].c == '\0', "");
+  constexpr S s2[]{{'a'}};
+  static_assert(s2[0].c == 'a', "");
+#if __cpp_designated_initializers >= 201707L
+  constexpr S s3[]{{.c = 'a'}};
+  static_assert(s3[0].c == 'a', "");
+#endif
+  constexpr S s4[]{'a'};
+  static_assert(s4[0].c == 'a', "");
+  constexpr S s5[]{{{}}};
+  static_assert(s5[0].c == '\0', "");
+}
+
+void
+f ()
+{
+  g<int>();
+}
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-4.C
new file mode 100644
index 00000000000..a99d3255a47
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-4.C
@@ -0,0 +1,35 @@
+// PR c++/104284
+// { dg-do run { target c++14 } }
+// { dg-additional-options "-fchecking=2" }
+
+struct S {
+  char c{};
+};
+
+template<class>
+constexpr void g ()
+{
+  S s1[]{{}};
+  if (s1[0].c != '\0')
+    __builtin_abort ();
+  S s2[]{{'a'}};
+  if (s2[0].c != 'a')
+    __builtin_abort ();
+#if __cpp_designated_initializers >= 201707L
+  S s3[]{{.c = 'a'}};
+  if (s3[0].c != 'a')
+    __builtin_abort ();
+#endif
+  S s4[]{'a'};
+  if (s4[0].c != 'a')
+    __builtin_abort ();
+  S s5[]{{{}}};
+  if (s5[0].c != '\0')
+    __builtin_abort ();
+}
+
+int
+main ()
+{
+  g<int>();
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-24 22:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24 22:44 [gcc r12-7805] c++: ICE with template code in constexpr [PR104284] Marek Polacek

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).