public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-7010] c++: dependent array bounds completion [PR104302]
@ 2022-02-03  4:21 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2022-02-03  4:21 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:501c4ee9fad68716497db4d96b63b801cc57b4a1

commit r12-7010-g501c4ee9fad68716497db4d96b63b801cc57b4a1
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Feb 2 17:49:02 2022 -0500

    c++: dependent array bounds completion [PR104302]
    
    The patch for PR55227 changed the minimal init-list handling in
    cp_complete_array_type to a call to reshape_init, which broke on the
    dependent initializer.  It occurred to me that trying to deduce the array
    size from a dependent init-list is wrong in general, so let's just not.  I
    also limited the reshape_init call to the case of a char array, as before
    the patch for 55227; that's the only case where we want to strip a level of
    braces from an array.
    
            PR c++/104302
    
    gcc/cp/ChangeLog:
    
            * decl.cc (maybe_deduce_size_from_array_init): Give up
            on type-dependent init.
            (cp_complete_array_type): Only call reshape_init for character
            array.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/template/array35.C: New test.
            * g++.dg/template/array36.C: New test.

Diff:
---
 gcc/cp/decl.cc                          | 10 ++++++++--
 gcc/testsuite/g++.dg/template/array35.C | 11 +++++++++++
 gcc/testsuite/g++.dg/template/array36.C | 15 +++++++++++++++
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 09eed9ceba6..7b48b56231b 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -6017,6 +6017,11 @@ maybe_deduce_size_from_array_init (tree decl, tree init)
 		return;
 	      if (!check_array_designated_initializer (ce, i))
 		failure = 1;
+	      /* If an un-designated initializer is type-dependent, we can't
+		 check brace elision yet.  */
+	      if (ce->index == NULL_TREE
+		  && type_dependent_expression_p (ce->value))
+		return;
 	    }
 	}
 
@@ -8113,7 +8118,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       else
 	{
 	  gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
-	  /* Deduce array size even if the initializer is dependent.  */
+	  /* Try to deduce array size.  */
 	  maybe_deduce_size_from_array_init (decl, init);
 	  /* And complain about multiple initializers.  */
 	  if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
@@ -9570,7 +9575,8 @@ cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
       /* An array of character type can be initialized from a
 	 brace-enclosed string constant so call reshape_init to
 	 remove the optional braces from a braced string literal.  */
-      if (BRACE_ENCLOSED_INITIALIZER_P (initial_value))
+      if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
+	  && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
 	initial_value = reshape_init (*ptype, initial_value,
 				      tf_warning_or_error);
 
diff --git a/gcc/testsuite/g++.dg/template/array35.C b/gcc/testsuite/g++.dg/template/array35.C
new file mode 100644
index 00000000000..9fd02633523
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/array35.C
@@ -0,0 +1,11 @@
+// PR c++/104302
+
+struct ss {};
+static ss ff(void* const v);
+template <unsigned mem_size>
+void f1(void) {
+    int mem[mem_size];
+    ss StateRegs[] = {
+        ff(mem)
+    };
+}
diff --git a/gcc/testsuite/g++.dg/template/array36.C b/gcc/testsuite/g++.dg/template/array36.C
new file mode 100644
index 00000000000..1511da7020d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/array36.C
@@ -0,0 +1,15 @@
+// Don't try to deduce array bounds from a dependent initializer.
+// { dg-do compile { target c++11 } }
+
+struct A { int i,j; };
+
+template <class T> void f(T t)
+{
+  A ar[] = { t, t };
+  static_assert (sizeof(ar)/sizeof(A) == 1, "");
+}
+
+int main()
+{
+  f(42);
+}


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

only message in thread, other threads:[~2022-02-03  4:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-03  4:21 [gcc r12-7010] c++: dependent array bounds completion [PR104302] Jason Merrill

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