C++ PATCH: PR 31038

Mark Mitchell mark@codesourcery.com
Mon Mar 12 01:04:00 GMT 2007


This patch fixes PR c++/31038, an ICE-on-valid regression in 4.2
involving a bad interaction between templates and compound literals.
(We continue to pay for our language extensions.)  In particular, the
C++ parser was mistakenly allowing compound literals in integral
constant-expressions, with ensuing confusion.  This patch makes us
reject all compound literals in integral constant-expressions.  

We could decide to allow compound literals of integral type, with a
constant initializer.  However, as the standard doesn't say anything
about that case, and as that's presumably a very rare case, and one
with an easy workaround (write "0", or "(int) 0", rather than the
non-standard "(int) { 0 }"), it doesn't seem very important, so I
decided to leave that for later.  (Note that we're not talking about
whether to accept such constructs at all, but, merely, whether such
things count as valid template arguments, array bounds, etc.)

Tested on x86_64-unknown-linux-gnu, applied to mainline.  I will apply
this to 4.2 after testing completes.

--
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

2007-03-11  Mark Mitchell  <mark@codesourcery.com>

	PR c++/31038
	* parser.c (cp_parser_postfix_expression): Disallow compound
	literals in constant expressions.

2007-03-11  Mark Mitchell  <mark@codesourcery.com>

	PR c++/31038
	* g++.dg/template/complit2.C: New test.

Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c	(revision 122822)
+++ gcc/cp/parser.c	(working copy)
@@ -4343,6 +4343,21 @@ cp_parser_postfix_expression (cp_parser 
 		   allowed in standard C++.  */
 		if (pedantic)
 		  pedwarn ("ISO C++ forbids compound-literals");
+		/* For simplicitly, we disallow compound literals in
+		   constant-expressions for simpliicitly.  We could
+		   allow compound literals of integer type, whose
+		   initializer was a constant, in constant
+		   expressions.  Permitting that usage, as a further
+		   extension, would not change the meaning of any
+		   currently accepted programs.  (Of course, as
+		   compound literals are not part of ISO C++, the
+		   standard has nothing to say.)  */
+		if (cp_parser_non_integral_constant_expression 
+		    (parser, "non-constant compound literals"))
+		  {
+		    postfix_expression = error_mark_node;
+		    break;
+		  }
 		/* Form the representation of the compound-literal.  */
 		postfix_expression
 		  = finish_compound_literal (type, initializer_list);
Index: gcc/testsuite/g++.dg/template/complit2.C
===================================================================
--- gcc/testsuite/g++.dg/template/complit2.C	(revision 0)
+++ gcc/testsuite/g++.dg/template/complit2.C	(revision 0)
@@ -0,0 +1,17 @@
+// PR c++/31038
+// { dg-options "" }
+
+template<int> void foo()
+{
+  int i = (int) { 0 };
+}
+
+template void foo<0>();
+int f();
+
+template<int> void bar()
+{
+  int i = (int) { f() };
+}
+
+template void bar<0>();



More information about the Gcc-patches mailing list