From patchwork Mon Sep 17 16:03:54 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: C++ PATCH for c++/53661 (wrong narrowing error with enum) Date: Mon, 17 Sep 2012 06:03:54 -0000 From: Jason Merrill X-Patchwork-Id: 184484 Message-Id: <505749EA.7080807@redhat.com> To: gcc-patches List 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 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) else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) && 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};