From 2410819b6d8cee41d044a05f0075fb338df04930 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Fri, 11 Jul 2014 21:53:59 +0000 Subject: [PATCH] re PR c++/53159 (Missing narrowing check) /cp 2014-07-11 Paolo Carlini PR c++/53159 * call.c (build_user_type_conversion_1): Copy LOOKUP_NO_NARROWING into convflags. * decl.c (check_initializer): Don't call check_narrowing here, set LOOKUP_NO_NARROWING. * typeck2.c (digest_init_r): Likewise. /testsuite 2014-07-11 Paolo Carlini PR c++/53159 * g++.dg/cpp0x/Wnarrowing1.C: New. From-SVN: r212469 --- gcc/cp/ChangeLog | 9 +++++++++ gcc/cp/call.c | 3 ++- gcc/cp/decl.c | 3 +-- gcc/cp/typeck2.c | 2 +- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/cpp0x/Wnarrowing1.C | 18 ++++++++++++++++++ 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wnarrowing1.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index ef04cba5cea..05c9615ee7e 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,12 @@ +2014-07-11 Paolo Carlini + + PR c++/53159 + * call.c (build_user_type_conversion_1): Copy LOOKUP_NO_NARROWING + into convflags. + * decl.c (check_initializer): Don't call check_narrowing here, + set LOOKUP_NO_NARROWING. + * typeck2.c (digest_init_r): Likewise. + 2014-07-10 Jason Merrill PR c++/61661 diff --git a/gcc/cp/call.c b/gcc/cp/call.c index e002d0180df..46e51864a29 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -3586,7 +3586,8 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags, /* It's OK to bind a temporary for converting constructor arguments, but not in converting the return value of a conversion operator. */ - convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION); + convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION + | (flags & LOOKUP_NO_NARROWING)); flags &= ~LOOKUP_NO_TEMP_BIND; if (ctors) diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 01d74e34f92..dae85c2d584 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -5756,8 +5756,7 @@ check_initializer (tree decl, tree init, int flags, vec **cleanups) else { init = reshape_init (type, init, tf_warning_or_error); - if (SCALAR_TYPE_P (type)) - check_narrowing (type, init); + flags |= LOOKUP_NO_NARROWING; } } else if (TREE_CODE (init) == TREE_LIST diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index f57aef14316..59a47605d6c 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -1027,7 +1027,7 @@ digest_init_r (tree type, tree init, bool nested, int flags, tree *exp; if (nested) - check_narrowing (type, init); + flags |= LOOKUP_NO_NARROWING; init = convert_for_initialization (0, type, init, flags, ICR_INIT, NULL_TREE, 0, complain); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index dc362c3bebb..3b676bea446 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2014-07-11 Paolo Carlini + + PR c++/53159 + * g++.dg/cpp0x/Wnarrowing1.C: New. + 2014-07-11 Andreas Schwab PR preprocessor/61389 diff --git a/gcc/testsuite/g++.dg/cpp0x/Wnarrowing1.C b/gcc/testsuite/g++.dg/cpp0x/Wnarrowing1.C new file mode 100644 index 00000000000..634c4c377d0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/Wnarrowing1.C @@ -0,0 +1,18 @@ +// PR c++/53159 +// { dg-do compile { target c++11 } } +// { dg-options "-Wnarrowing -Wno-overflow" } + +struct X +{ + constexpr operator int() { return __INT_MAX__; } +}; + +int f() { return __INT_MAX__; } + +signed char a { __INT_MAX__ }; // { dg-warning "narrowing conversion" } +signed char b { f() }; // { dg-warning "narrowing conversion" } +signed char c { X{} }; // { dg-warning "narrowing conversion" } + +signed char ar[] { __INT_MAX__ }; // { dg-warning "narrowing conversion" } +signed char br[] { f() }; // { dg-warning "narrowing conversion" } +signed char cr[] { X{} }; // { dg-warning "narrowing conversion" }