aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2018-08-21 15:25:17 +0000
committerMarek Polacek <polacek@redhat.com>2018-08-21 15:25:17 +0000
commit48c2ad0a4e35c7ab9a1b01a0ce1d70eda9f7f1e2 (patch)
tree671d641b6208764e364a009dbd4bbc0ae066d46b
parentc6db5a50cef3c18def87fc3f61f7439ba8b2b5fa (diff)
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. git-svn-id: https://gcc.gnu.org/svn/gcc/trunk@263739 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--gcc/cp/ChangeLog5
-rw-r--r--gcc/cp/call.c2
-rw-r--r--gcc/cp/typeck2.c2
-rw-r--r--gcc/testsuite/ChangeLog8
-rw-r--r--gcc/testsuite/g++.dg/concepts/pr67595.C2
-rw-r--r--gcc/testsuite/g++.dg/cpp0x/Wnarrowing11.C30
-rw-r--r--gcc/testsuite/g++.dg/cpp0x/Wnarrowing12.C32
-rw-r--r--gcc/testsuite/g++.dg/cpp0x/rv-cast5.C2
8 files changed, 80 insertions, 3 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index fc1b2555dc0..44be980985d 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,5 +1,10 @@
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.
2018-08-20 David Malcolm <dmalcolm@redhat.com>
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 1f72ac863bd..626830c0d9a 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 f42f0c2bf07..1e899ab17a1 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 6a6e2267a94..7b54bc0a868 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,11 @@
+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>
* 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 63162fb4c72..76d1fe62132 100644
--- a/gcc/testsuite/g++.dg/concepts/pr67595.C
+++ b/gcc/testsuite/g++.dg/concepts/pr67595.C
@@ -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 readable = requires{requires semiregular<X>};
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 bidirectional_iterator{forward_iterator<X>};
template <class X>
diff --git a/gcc/testsuite/g++.dg/cpp0x/Wnarrowing11.C b/gcc/testsuite/g++.dg/cpp0x/Wnarrowing11.C
new file mode 100644
index 00000000000..5b7323633c7
--- /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 00000000000..83b4d3a58c6
--- /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 c2473e266b6..5233078f7b4 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<bool&&>{42}();
+ hold<bool&&>{static_cast<bool>(42)}();
}