From 992931ba3d02d107b3abf51781b4b4625d2cce7c Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 21 Aug 2018 15:25:17 +0000 Subject: [PATCH] re PR c++/65043 (Expected narrowing conversion during list initialization of bool from double) PR c++/65043 * call.c (standard_conversion): Set check_narrowing. * typeck2.c (check_narrowing): Use CP_INTEGRAL_TYPE_P rather than comparing with INTEGER_TYPE. * g++.dg/concepts/pr67595.C: Add dg-warning. * g++.dg/cpp0x/Wnarrowing11.C: New test. * g++.dg/cpp0x/Wnarrowing12.C: New test. * g++.dg/cpp0x/rv-cast5.C: Add static_cast. From-SVN: r263739 --- gcc/cp/ChangeLog | 5 +++++ gcc/cp/call.c | 2 ++ gcc/cp/typeck2.c | 2 +- gcc/testsuite/ChangeLog | 8 ++++++++ gcc/testsuite/g++.dg/concepts/pr67595.C | 2 +- gcc/testsuite/g++.dg/cpp0x/Wnarrowing11.C | 30 +++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/Wnarrowing12.C | 32 +++++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/rv-cast5.C | 2 +- 8 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wnarrowing11.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wnarrowing12.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index fc1b255..44be980 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,10 @@ 2018-08-21 Marek Polacek + PR c++/65043 + * call.c (standard_conversion): Set check_narrowing. + * typeck2.c (check_narrowing): Use CP_INTEGRAL_TYPE_P rather + than comparing with INTEGER_TYPE. + * cp-tree.h: Fix typo. 2018-08-20 David Malcolm diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 1f72ac8..626830c 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -1388,6 +1388,8 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p, conv->rank = cr_pbool; if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING)) conv->bad_p = true; + if (flags & LOOKUP_NO_NARROWING) + conv->check_narrowing = true; return conv; } diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index f42f0c2..1e899ab 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -913,7 +913,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain, bool const_only) if (const_only && !TREE_CONSTANT (init)) return ok; - if (TREE_CODE (type) == INTEGER_TYPE + if (CP_INTEGRAL_TYPE_P (type) && TREE_CODE (ftype) == REAL_TYPE) ok = false; else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6a6e226..7b54bc0 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2018-08-21 Marek Polacek + + PR c++/65043 + * g++.dg/concepts/pr67595.C: Add dg-warning. + * g++.dg/cpp0x/Wnarrowing11.C: New test. + * g++.dg/cpp0x/Wnarrowing12.C: New test. + * g++.dg/cpp0x/rv-cast5.C: Add static_cast. + 2018-08-21 Ed Schonberg * gnat.dg/expr_func7.adb, gnat.dg/expr_func7.ads: New testcase. diff --git a/gcc/testsuite/g++.dg/concepts/pr67595.C b/gcc/testsuite/g++.dg/concepts/pr67595.C index 63162fb..76d1fe6 100644 --- a/gcc/testsuite/g++.dg/concepts/pr67595.C +++ b/gcc/testsuite/g++.dg/concepts/pr67595.C @@ -4,7 +4,7 @@ template concept bool allocatable = requires{{new X}->X * }; template concept bool semiregular = allocatable; template concept bool readable = requires{requires semiregular}; template int weak_input_iterator = requires{{0}->readable}; -template bool input_iterator{weak_input_iterator}; +template bool input_iterator{weak_input_iterator}; // { dg-warning "narrowing conversion" } template bool forward_iterator{input_iterator}; template bool bidirectional_iterator{forward_iterator}; template diff --git a/gcc/testsuite/g++.dg/cpp0x/Wnarrowing11.C b/gcc/testsuite/g++.dg/cpp0x/Wnarrowing11.C new file mode 100644 index 0000000..5b73236 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/Wnarrowing11.C @@ -0,0 +1,30 @@ +// PR c++/65043 +// { dg-do compile { target c++11 } } + +struct X +{ + X(bool) { } +}; + +struct Y +{ + Y(char) { } +}; + +struct Z +{ + Z(char16_t) { } +}; + +struct W +{ + W(char32_t) { } +}; + +int main() +{ + X x{1.2}; // { dg-error "narrowing conversion" } + Y y{1.2}; // { dg-error "narrowing conversion" } + Z z{1.2}; // { dg-error "narrowing conversion" } + W w{1.2}; // { dg-error "narrowing conversion" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/Wnarrowing12.C b/gcc/testsuite/g++.dg/cpp0x/Wnarrowing12.C new file mode 100644 index 0000000..83b4d3a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/Wnarrowing12.C @@ -0,0 +1,32 @@ +// PR c++/65043 +// { dg-do compile { target c++11 } } +// { dg-options "-Wnarrowing" } + +struct X +{ + X(bool) { } +}; + +struct Y +{ + Y(char) { } +}; + +struct Z +{ + Z(char16_t) { } +}; + +struct W +{ + W(char32_t) { } +}; + +int main() +{ + double d = 1.2; + X x{d}; // { dg-warning "narrowing conversion" } + Y y{d}; // { dg-warning "narrowing conversion" } + Z z{d}; // { dg-warning "narrowing conversion" } + W w{d}; // { dg-warning "narrowing conversion" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/rv-cast5.C b/gcc/testsuite/g++.dg/cpp0x/rv-cast5.C index c2473e2..5233078 100644 --- a/gcc/testsuite/g++.dg/cpp0x/rv-cast5.C +++ b/gcc/testsuite/g++.dg/cpp0x/rv-cast5.C @@ -8,5 +8,5 @@ struct hold { int main() { - hold{42}(); + hold{static_cast(42)}(); } -- 2.7.4