Patchwork C++ PATCH for c++/50011 (wrong narrowing error)

login
register
mail settings
Submitter Jason Merrill
Date Aug. 8, 2011, 2:37 p.m.
Message ID <4E3FF4A3.4010704@redhat.com>
Download mbox | patch
Permalink /patch/108955/
State New
Headers show

Comments

Jason Merrill - Aug. 8, 2011, 2:37 p.m.
Oops, my last fix was still wrong.  This patch just compares min/max 
values, which should be safe.  :)

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

Patch

commit 5efa9cf21a329bba89748ddbc8e3bf3c8ea4329e
Author: Jason Merrill <jason@redhat.com>
Date:   Sun Aug 7 17:10:26 2011 -0400

    	PR c++/50011
    	* typeck2.c (check_narrowing): Fix integer logic.

diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index c6b8c44..0788138 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -740,8 +740,10 @@  check_narrowing (tree type, tree init)
   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
 	   && CP_INTEGRAL_TYPE_P (type))
     {
-      if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
-	   || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (ftype))
+      if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
+			    TYPE_MAX_VALUE (ftype))
+	   || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
+			       TYPE_MIN_VALUE (type)))
 	  && (TREE_CODE (init) != INTEGER_CST
 	      || !int_fits_type_p (init, type)))
 	ok = false;
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist5.C b/gcc/testsuite/g++.dg/cpp0x/initlist5.C
index c5ba87d..51345c7 100644
--- a/gcc/testsuite/g++.dg/cpp0x/initlist5.C
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist5.C
@@ -29,3 +29,7 @@  float fa2[] = { d2, 1.1 };
 // PR c++/49577
 unsigned u{ -1 };		// { dg-error "narrowing" }
 char c = char{ u };		// { dg-error "narrowing" }
+
+// PR c++/50011
+short unsigned su;
+int i { su };