Commit 992931ba by Marek Polacek Committed by Marek Polacek

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
parent d3e993e9
2018-08-21 Marek Polacek <polacek@redhat.com> 2018-08-21 Marek Polacek <polacek@redhat.com>
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. * cp-tree.h: Fix typo.
2018-08-20 David Malcolm <dmalcolm@redhat.com> 2018-08-20 David Malcolm <dmalcolm@redhat.com>
......
...@@ -1388,6 +1388,8 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p, ...@@ -1388,6 +1388,8 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
conv->rank = cr_pbool; conv->rank = cr_pbool;
if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING)) if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
conv->bad_p = true; conv->bad_p = true;
if (flags & LOOKUP_NO_NARROWING)
conv->check_narrowing = true;
return conv; return conv;
} }
......
...@@ -913,7 +913,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain, bool const_only) ...@@ -913,7 +913,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain, bool const_only)
if (const_only && !TREE_CONSTANT (init)) if (const_only && !TREE_CONSTANT (init))
return ok; return ok;
if (TREE_CODE (type) == INTEGER_TYPE if (CP_INTEGRAL_TYPE_P (type)
&& TREE_CODE (ftype) == REAL_TYPE) && TREE_CODE (ftype) == REAL_TYPE)
ok = false; ok = false;
else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
......
2018-08-21 Marek Polacek <polacek@redhat.com>
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 <schonberg@adacore.com> 2018-08-21 Ed Schonberg <schonberg@adacore.com>
* gnat.dg/expr_func7.adb, gnat.dg/expr_func7.ads: New testcase. * gnat.dg/expr_func7.adb, gnat.dg/expr_func7.ads: New testcase.
......
...@@ -4,7 +4,7 @@ template <class X> concept bool allocatable = requires{{new X}->X * }; ...@@ -4,7 +4,7 @@ template <class X> concept bool allocatable = requires{{new X}->X * };
template <class X> concept bool semiregular = allocatable<X>; template <class X> concept bool semiregular = allocatable<X>;
template <class X> concept bool readable = requires{requires semiregular<X>}; template <class X> concept bool readable = requires{requires semiregular<X>};
template <class> int weak_input_iterator = requires{{0}->readable}; template <class> int weak_input_iterator = requires{{0}->readable};
template <class X> bool input_iterator{weak_input_iterator<X>}; template <class X> bool input_iterator{weak_input_iterator<X>}; // { dg-warning "narrowing conversion" }
template <class X> bool forward_iterator{input_iterator<X>}; template <class X> bool forward_iterator{input_iterator<X>};
template <class X> bool bidirectional_iterator{forward_iterator<X>}; template <class X> bool bidirectional_iterator{forward_iterator<X>};
template <class X> template <class X>
......
// 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" }
}
// 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" }
}
...@@ -8,5 +8,5 @@ struct hold { ...@@ -8,5 +8,5 @@ struct hold {
int main() int main()
{ {
hold<bool&&>{42}(); hold<bool&&>{static_cast<bool>(42)}();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment