C++ PATCH for c++/53661 (wrong narrowing error with enum)

Message ID 505749EA.7080807@redhat.com
State New
Headers show

Commit Message

Jason Merrill Sept. 17, 2012, 4:03 p.m.
When determining whether a conversion from an unscoped enum is a 
narrowing conversion, we check whether the values of the enum fit into 
the target type.  [dcl.enum] defines the values of an enum with 
unspecified underlying type to be the values of the smallest bit-field 
that could hold it, not all the values representable in the size of the 
type.  This patch fixes that check.

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


commit 373e79aaea9c5b73cebeb1e8fd4fedbdfd553a35
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Sep 14 09:48:48 2012 -0400

    	PR c++/53661
    	* typeck2.c (check_narrowing): Avoid false positives on conversion
    	from enumeral type.

diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index 6faebb5..58b2db6 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -787,6 +787,9 @@  check_narrowing (tree type, tree init)
 	   && CP_INTEGRAL_TYPE_P (type))
+      if (TREE_CODE (ftype) == ENUMERAL_TYPE)
+	/* Check for narrowing based on the values of the enumeration. */
+	ftype = ENUM_UNDERLYING_TYPE (ftype);
       if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
 			    TYPE_MAX_VALUE (ftype))
 	   || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
diff --git a/gcc/testsuite/g++.dg/init/aggr9.C b/gcc/testsuite/g++.dg/init/aggr9.C
new file mode 100644
index 0000000..67d8299
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/aggr9.C
@@ -0,0 +1,9 @@ 
+// PR c++/53661
+enum Code {
+  SUCCESS = 0
+Code a;
+int r[] = {a};