This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] Don't reject narrowing of ?: with throw expression on one side (PR c++/40566)


Hi!

convert_to_integer converts COND_EXPR arguments individually, which
results in errors if one of the argument has void type (is THROW_EXPR).
Given that there are already a bunch of places e.g. in fold-const.c
that check for this, I think convert_to_integer should do the same, rather
than adding some very ugly hack in ocp_convert.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/4.4?

2009-06-30  Jakub Jelinek  <jakub@redhat.com>

	PR c++/40566
	* convert.c (convert_to_integer) <case COND_EXPR>: Don't convert
	to type arguments that have void type.

	* g++.dg/parse/cond5.C: New test.

--- gcc/convert.c.jj	2009-06-30 13:10:30.000000000 +0200
+++ gcc/convert.c	2009-06-30 20:05:48.000000000 +0200
@@ -772,10 +772,16 @@ convert_to_integer (tree type, tree expr
 
 	case COND_EXPR:
 	  /* It is sometimes worthwhile to push the narrowing down through
-	     the conditional and never loses.  */
+	     the conditional and never loses.  A COND_EXPR may have a throw
+	     as one operand, which then has void type.  Just leave void
+	     operands as they are.  */
 	  return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
-			      convert (type, TREE_OPERAND (expr, 1)),
-			      convert (type, TREE_OPERAND (expr, 2)));
+			      VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
+			      ? TREE_OPERAND (expr, 1)
+			      : convert (type, TREE_OPERAND (expr, 1)),
+			      VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
+			      ? TREE_OPERAND (expr, 2)
+			      : convert (type, TREE_OPERAND (expr, 2)));
 
 	default:
 	  break;
--- gcc/testsuite/g++.dg/parse/cond5.C.jj	2009-06-30 20:17:51.000000000 +0200
+++ gcc/testsuite/g++.dg/parse/cond5.C	2009-06-30 20:10:35.000000000 +0200
@@ -0,0 +1,10 @@
+// PR c++/40566
+
+void
+f (int x, int y)
+{
+  int c = x ? 23 : throw "bla";
+  short d = y ? throw "bla" : 23;
+  char e = x ? 23 : throw "bla";
+  long f = x ? 23 : throw "bla";
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]