A couple of narrowing errors we were missing: we need to deal with
reshape_init discarding the {} and still complain, and we need to handle
unsignedness mismatch.
Tested x86_64-pc-linux-gnu, applied to trunk.
commit 2d486097a6240eb2a4d0caaf2cdf1a5f12faa249
Author: Jason Merrill <ja...@redhat.com>
Date: Tue Aug 2 15:56:03 2011 -0400
PR c++/49577
* typeck2.c (check_narrowing): Check unsigned mismatch.
* semantics.c (finish_compound_literal): check_narrowing.
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index a528477..ac24b77 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -2379,6 +2379,9 @@ finish_compound_literal (tree type, tree compound_literal,
&& check_array_initializer (NULL_TREE, type, compound_literal))
return error_mark_node;
compound_literal = reshape_init (type, compound_literal, complain);
+ if (cxx_dialect >= cxx0x && SCALAR_TYPE_P (type)
+ && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal))
+ check_narrowing (type, compound_literal);
if (TREE_CODE (type) == ARRAY_TYPE
&& TYPE_DOMAIN (type) == NULL_TREE)
{
diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index 727a88b..c6b8c44 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -740,7 +740,8 @@ 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)
+ if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
+ || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (ftype))
&& (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 dbd17ec..c5ba87d 100644
--- a/gcc/testsuite/g++.dg/cpp0x/initlist5.C
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist5.C
@@ -25,3 +25,7 @@ double d = 1.1;
float fa[] = { d, 1.1 }; // { dg-error "narrowing conversion of 'd'" }
constexpr double d2 = 1.1;
float fa2[] = { d2, 1.1 };
+
+// PR c++/49577
+unsigned u{ -1 }; // { dg-error "narrowing" }
+char c = char{ u }; // { dg-error "narrowing" }
diff --git a/gcc/testsuite/g++.dg/opt/range-test-1.C b/gcc/testsuite/g++.dg/opt/range-test-1.C
index cc5ba66..9015147 100644
--- a/gcc/testsuite/g++.dg/opt/range-test-1.C
+++ b/gcc/testsuite/g++.dg/opt/range-test-1.C
@@ -156,7 +156,7 @@ T(16, int, x == -3 || x == -4 || x == -5,
{ -3 C -4 C -5 }, { -6 C -7 C -8 C -2 C -1 C 1 C 2 C INT_MAX C INT_MIN })
T(17, unsigned int, (x >= -8U && x <= -3U) || x == -2U || x == -1U || x == 0 || x == 1 || x == 2,
{ -8U C -7U C -6U C -5U C -4U C -3U C -2U C -1U C 0 C 1 C 2 },
- { -9U C -10U C 3 C 4 C 12 C -54U C INT_MAX C INT_MIN })
+ { -9U C -10U C 3 C 4 C 12 C -54U C INT_MAX C (unsigned int)INT_MIN })
T(18, int, (x >= -8 && x <= -3) || x == -2 || x == -1 || x == 0 || x == 1 || x == 2,
{ -8 C -7 C -6 C -5 C -4 C -3 C -2 C -1 C 0 C 1 C 2 },
{ -9 C -10 C 3 C 4 C 12 C -54 C INT_MAX C INT_MIN })