https://gcc.gnu.org/g:2619413a5e032e2f2c3d4a2202ba8bf324cf08ab

commit r15-6924-g2619413a5e032e2f2c3d4a2202ba8bf324cf08ab
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Wed Jan 15 18:27:07 2025 +0100

    c++: Handle RAW_DATA_CST in unify [PR118390]
    
    This patch uses the count_ctor_elements function to fix up
    unify deduction of array sizes.
    
    2025-01-15  Jakub Jelinek  <ja...@redhat.com>
    
            PR c++/118390
            * cp-tree.h (count_ctor_elements): Declare.
            * call.cc (count_ctor_elements): No longer static.
            * pt.cc (unify): Use count_ctor_elements instead of
            CONSTRUCTOR_NELTS.
    
            * g++.dg/cpp/embed-20.C: New test.
            * g++.dg/cpp0x/pr118390.C: New test.

Diff:
---
 gcc/cp/call.cc                        |  2 +-
 gcc/cp/cp-tree.h                      |  1 +
 gcc/cp/pt.cc                          |  2 +-
 gcc/testsuite/g++.dg/cpp/embed-20.C   | 14 ++++++++++++++
 gcc/testsuite/g++.dg/cpp0x/pr118390.C | 23 +++++++++++++++++++++++
 5 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 63be8b58906b..9e57261cf17d 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -4333,7 +4333,7 @@ has_non_trivial_temporaries (tree expr)
 
 /* Return number of initialized elements in CTOR.  */
 
-static unsigned HOST_WIDE_INT
+unsigned HOST_WIDE_INT
 count_ctor_elements (tree ctor)
 {
   unsigned HOST_WIDE_INT len = 0;
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 2d17c4c77b56..bb10c7ca053d 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6815,6 +6815,7 @@ extern tree type_decays_to                        (tree);
 extern tree extract_call_expr                  (tree);
 extern tree build_trivial_dtor_call            (tree, bool = false);
 extern tristate ref_conv_binds_to_temporary    (tree, tree, bool = false);
+extern unsigned HOST_WIDE_INT count_ctor_elements (tree);
 extern tree build_user_type_conversion         (tree, tree, int,
                                                 tsubst_flags_t);
 extern tree build_new_function_call            (tree, vec<tree, va_gc> **,
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index ff0a3a4f7d81..3b6743f2e3af 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -25072,7 +25072,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, 
int strict,
          && deducible_array_bound (TYPE_DOMAIN (parm)))
        {
          /* Also deduce from the length of the initializer list.  */
-         tree max = size_int (CONSTRUCTOR_NELTS (arg));
+         tree max = size_int (count_ctor_elements (arg));
          tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
          if (idx == error_mark_node)
            return unify_invalid (explain_p);
diff --git a/gcc/testsuite/g++.dg/cpp/embed-20.C 
b/gcc/testsuite/g++.dg/cpp/embed-20.C
new file mode 100644
index 000000000000..ad708406f3d7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp/embed-20.C
@@ -0,0 +1,14 @@
+// PR c++/118390
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+template<typename T, int N>
+constexpr int
+foo (const T (&x)[N])
+{
+  return N;
+}
+
+static_assert (foo ({
+  #embed __FILE__ limit (64)
+}) == 64, "");
diff --git a/gcc/testsuite/g++.dg/cpp0x/pr118390.C 
b/gcc/testsuite/g++.dg/cpp0x/pr118390.C
new file mode 100644
index 000000000000..e5017b017d7f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/pr118390.C
@@ -0,0 +1,23 @@
+// PR c++/118390
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+template<typename T, int N>
+constexpr int
+foo (const T (&x)[N])
+{
+  return N;
+}
+
+static_assert (foo ({
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+}) == 160, "");

Reply via email to