Patchwork C++ PATCH to fix Enum{}

login
register
mail settings
Submitter Jason Merrill
Date Sept. 6, 2010, 4:47 a.m.
Message ID <4C84725C.9070703@redhat.com>
Download mbox | patch
Permalink /patch/63874/
State New
Headers show

Comments

Jason Merrill - Sept. 6, 2010, 4:47 a.m.
The code for handling conversion of {} to scalar type was assuming that 
we could do an implicit conversion from integer 0 to any scalar type. 
But that's not true of enums.

Tested x86_64-pc-linux-gnu, applied to trunk.

Patch

commit 31436fc0666220ad0e4a05fdef4ad8335ce74d74
Author: Jason Merrill <jason@redhat.com>
Date:   Sun Sep 5 09:05:45 2010 -0400

    	* call.c (implicit_conversion): Fix value-init of enums.
    	(convert_like_real): Likewise.

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 36f5a55..54a711a 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -1457,7 +1457,7 @@  implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
 	  tree elt;
 
 	  if (nelts == 0)
-	    elt = integer_zero_node;
+	    elt = build_value_init (to, tf_none);
 	  else if (nelts == 1)
 	    elt = CONSTRUCTOR_ELT (expr, 0)->value;
 	  else
@@ -5050,7 +5050,7 @@  convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
 	{
 	  int nelts = CONSTRUCTOR_NELTS (expr);
 	  if (nelts == 0)
-	    expr = integer_zero_node;
+	    expr = build_value_init (totype, tf_warning_or_error);
 	  else if (nelts == 1)
 	    expr = CONSTRUCTOR_ELT (expr, 0)->value;
 	  else
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist42.C b/gcc/testsuite/g++.dg/cpp0x/initlist42.C
new file mode 100644
index 0000000..e63959d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist42.C
@@ -0,0 +1,13 @@ 
+// { dg-options -std=c++0x }
+
+enum Unscoped { };
+enum class Scoped { };
+
+Unscoped bar(Unscoped x) { return x; }
+Scoped bar(Scoped x) { return x; }
+
+auto var1u = bar(Unscoped()); // OK
+auto var1s = bar(Scoped()); // OK
+
+auto var2u = bar(Unscoped{}); // #1 Error, but should work
+auto var2s = bar(Scoped{}); // #2 Error, but should work