From 54e7d7f5e9e226024737b41ce6feabf1522d13df Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Tue, 2 Aug 2011 17:09:17 -0400 Subject: [PATCH] re PR c++/49577 ([C++0x] narrowing conversion not rejected) PR c++/49577 * typeck2.c (check_narrowing): Check unsigned mismatch. * semantics.c (finish_compound_literal): check_narrowing. From-SVN: r177215 --- gcc/cp/ChangeLog | 4 ++++ gcc/cp/semantics.c | 3 +++ gcc/cp/typeck2.c | 3 ++- gcc/testsuite/ChangeLog | 3 +++ gcc/testsuite/g++.dg/cpp0x/initlist5.C | 4 ++++ gcc/testsuite/g++.dg/opt/range-test-1.C | 2 +- 6 files changed, 17 insertions(+), 2 deletions(-) diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 28900c87ce8..4e64f1bae1d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,9 @@ 2011-08-02 Jason Merrill + PR c++/49577 + * typeck2.c (check_narrowing): Check unsigned mismatch. + * semantics.c (finish_compound_literal): check_narrowing. + PR c++/49593 * pt.c (find_parameter_packs_r): Handle CONSTRUCTOR. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index a52847733e6..ac24b779dd2 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 727a88bd363..c6b8c443757 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/ChangeLog b/gcc/testsuite/ChangeLog index 884162de854..6e91668b990 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2011-08-02 Jason Merrill + PR c++/49577 + * g++.dg/cpp0x/initlist5.C: Add additional cases. + PR c++/49593 * g++.dg/cpp0x/variadic115.C: New. diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist5.C b/gcc/testsuite/g++.dg/cpp0x/initlist5.C index dbd17ecbbe2..c5ba87d55f1 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 cc5ba669479..9015147b2c9 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 }) -- 2.30.2