Patchwork C++ PATCH for c++/56794 (range for and variadic template)

login
register
mail settings
Submitter Jason Merrill
Date April 1, 2013, 9:12 p.m.
Message ID <5159F855.8050403@redhat.com>
Download mbox | patch
Permalink /patch/232808/
State New
Headers show

Comments

Jason Merrill - April 1, 2013, 9:12 p.m.
We try to resolve types at template definition time if expressions don't 
depend on template parameters, but this testcase shows a case of 
dependency we weren't considering: the bounds of an array can depend on 
the length of the initializer.

Tested x86_64-pc-linux-gnu, applying to 4.7, 4.8, trunk.

Patch

commit ab873d428ec43d33d1e34567749104cc5a9544ad
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Apr 1 15:45:54 2013 -0400

    	PR c++/56794
    	* parser.c (cp_parser_range_for): Don't try to do auto deduction
    	in a template if the type of the range is incomplete.

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index f29e80d..05c03f4 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -9615,7 +9615,10 @@  cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
 	range_expr = error_mark_node;
       stmt = begin_range_for_stmt (scope, init);
       finish_range_for_decl (stmt, range_decl, range_expr);
-      if (!type_dependent_expression_p (range_expr)
+      if (range_expr != error_mark_node
+	  && !type_dependent_expression_p (range_expr)
+	  /* The length of an array might be dependent.  */
+	  && COMPLETE_TYPE_P (TREE_TYPE (range_expr))
 	  /* do_auto_deduction doesn't mess with template init-lists.  */
 	  && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
 	do_range_for_auto_deduction (range_decl, range_expr);
diff --git a/gcc/testsuite/g++.dg/cpp0x/range-for24.C b/gcc/testsuite/g++.dg/cpp0x/range-for24.C
new file mode 100644
index 0000000..b4a5b18
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/range-for24.C
@@ -0,0 +1,15 @@ 
+// PR c++/56794
+// { dg-require-effective-target c++11 }
+
+template<int... values>
+static void Colors()
+{
+    static const int colors[] = { values... };
+
+    for(auto c: colors) { }
+}
+
+int main()
+{
+    Colors<0,1,2> ();
+}