]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c++/65043
authormpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 21 Aug 2018 15:25:17 +0000 (15:25 +0000)
committermpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 21 Aug 2018 15:25:17 +0000 (15:25 +0000)
* 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: svn+ssh://gcc.gnu.org/svn/gcc/trunk@263739 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/cp/ChangeLog
gcc/cp/call.c
gcc/cp/typeck2.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/concepts/pr67595.C
gcc/testsuite/g++.dg/cpp0x/Wnarrowing11.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/Wnarrowing12.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/rv-cast5.C

index fc1b2555dc0731371e7b75e5343f99f482a86808..44be980985dc1c256fd5d9dbea2cf64193b502b6 100644 (file)
@@ -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>
index 1f72ac863bde4ebe54d946582ba3c6d4f307db3c..626830c0d9a43a26f7973b5d107309d37c49a524 100644 (file)
@@ -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;
        }
 
index f42f0c2bf0747b5a6f6dc6db58c5efe170738277..1e899ab17a1d22bbeaba668567dc04cd765ca293 100644 (file)
@@ -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)
index 6a6e2267a94eadba2ad9bab62b048f6e9910032a..7b54bc0a8681b9b03194de04130beb770d51d9d7 100644 (file)
@@ -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.
index 63162fb4c727c1e0658d757c11fc5b6130afc46f..76d1fe62132d9b2644c07ad24bdbc59675bdf346 100644 (file)
@@ -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 (file)
index 0000000..5b73236
--- /dev/null
@@ -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 (file)
index 0000000..83b4d3a
--- /dev/null
@@ -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" }
+}
index c2473e266b648d7d2e0c9497dd56f8ce8b0a8661..5233078f7b4a7a34fd8bb8577f8d6ba8b676b15f 100644 (file)
@@ -8,5 +8,5 @@ struct hold {
 
 int main()
 {
-  hold<bool&&>{42}();
+  hold<bool&&>{static_cast<bool>(42)}();
 }