PR c++/78244 - narrowing conversion in template not detected.
authorMarek Polacek <polacek@redhat.com>
Wed, 16 Jan 2019 15:58:34 +0000 (15:58 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Wed, 16 Jan 2019 15:58:34 +0000 (15:58 +0000)
* call.c (perform_implicit_conversion_flags): Set
IMPLICIT_CONV_EXPR_BRACED_INIT.
* cp-tree.h (IMPLICIT_CONV_EXPR_BRACED_INIT): New.
* pt.c (tsubst_copy_and_build): Use it.

* g++.dg/cpp0x/Wnarrowing13.C: New test.
* g++.dg/cpp0x/Wnarrowing14.C: New test.

From-SVN: r267976

gcc/cp/ChangeLog
gcc/cp/call.c
gcc/cp/cp-tree.h
gcc/cp/pt.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/Wnarrowing13.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/Wnarrowing14.C [new file with mode: 0644]

index 7cf0ffba4928f88d94eeb9b5c9a8d503935638eb..6adb0e44d7e5cbd5d5dd89dac795f29cbda0c5c7 100644 (file)
@@ -1,3 +1,11 @@
+2019-01-16  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/78244 - narrowing conversion in template not detected.
+       * call.c (perform_implicit_conversion_flags): Set
+       IMPLICIT_CONV_EXPR_BRACED_INIT.
+       * cp-tree.h (IMPLICIT_CONV_EXPR_BRACED_INIT): New.
+       * pt.c (tsubst_copy_and_build): Use it.
+
 2019-01-15  David Malcolm  <dmalcolm@redhat.com>
 
        PR c++/88795
index 8bc8566e8d64b50a8ca65e9710cb971229361fac..4f04b61000438e87b66597b48c885356b43b9ba1 100644 (file)
@@ -11018,6 +11018,8 @@ perform_implicit_conversion_flags (tree type, tree expr,
       expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
       if (!(flags & LOOKUP_ONLYCONVERTING))
        IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
+      if (flags & LOOKUP_NO_NARROWING)
+       IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
     }
   else
     expr = convert_like (conv, expr, complain);
index 6a2004330d23d0ac5ac7a12efb45159fb6d55bab..1c85b37ba7fb33a6a28b5458a545b2c4c91423e1 100644 (file)
@@ -442,6 +442,7 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
       OVL_HIDDEN_P (in OVERLOAD)
       SWITCH_STMT_NO_BREAK_P (in SWITCH_STMT)
       LAMBDA_EXPR_CAPTURE_OPTIMIZED (in LAMBDA_EXPR)
+      IMPLICIT_CONV_EXPR_BRACED_INIT (in IMPLICIT_CONV_EXPR)
    3: (TREE_REFERENCE_EXPR) (in NON_LVALUE_EXPR) (commented-out).
       ICS_BAD_FLAG (in _CONV)
       FN_TRY_BLOCK_P (in TRY_BLOCK)
@@ -4229,6 +4230,11 @@ more_aggr_init_expr_args_p (const aggr_init_expr_arg_iterator *iter)
 #define IMPLICIT_CONV_EXPR_NONTYPE_ARG(NODE) \
   (TREE_LANG_FLAG_1 (IMPLICIT_CONV_EXPR_CHECK (NODE)))
 
+/* True if NODE represents a conversion for braced-init-list in a
+   template.  Set by perform_implicit_conversion_flags.  */
+#define IMPLICIT_CONV_EXPR_BRACED_INIT(NODE) \
+  (TREE_LANG_FLAG_2 (IMPLICIT_CONV_EXPR_CHECK (NODE)))
+
 /* Nonzero means that an object of this type cannot be initialized using
    an initializer list.  */
 #define CLASSTYPE_NON_AGGREGATE(NODE) \
index c6fc1cfeffb2d77f864c29f51c30c78eff7f9637..83bceb26474226e374cb544f6e51a867eac94d17 100644 (file)
@@ -18210,6 +18210,8 @@ tsubst_copy_and_build (tree t,
        int flags = LOOKUP_IMPLICIT;
        if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
          flags = LOOKUP_NORMAL;
+       if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
+         flags |= LOOKUP_NO_NARROWING;
        RETURN (perform_implicit_conversion_flags (type, expr, complain,
                                                  flags));
       }
index d8f46a03378924be0b8acb1682c74ce26dade242..df36f5e8315d710bd28fa8d7b87c9464bf71c1b9 100644 (file)
@@ -1,3 +1,9 @@
+2019-01-16  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/78244 - narrowing conversion in template not detected.
+       * g++.dg/cpp0x/Wnarrowing13.C: New test.
+       * g++.dg/cpp0x/Wnarrowing14.C: New test.
+
 2019-01-16  Jakub Jelinek  <jakub@redhat.com>
 
        PR c/51628
diff --git a/gcc/testsuite/g++.dg/cpp0x/Wnarrowing13.C b/gcc/testsuite/g++.dg/cpp0x/Wnarrowing13.C
new file mode 100644 (file)
index 0000000..3750f29
--- /dev/null
@@ -0,0 +1,8 @@
+// PR c++/78244
+// { dg-do compile { target c++11 } }
+
+template<typename>
+struct S {
+  static const int i{1.1}; // { dg-error "narrowing conversion" }
+  static const int i2 = {1.1}; // { dg-error "narrowing conversion" }
+};
diff --git a/gcc/testsuite/g++.dg/cpp0x/Wnarrowing14.C b/gcc/testsuite/g++.dg/cpp0x/Wnarrowing14.C
new file mode 100644 (file)
index 0000000..1aa2219
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/78244
+// { dg-do compile { target c++11 } }
+
+using Number = unsigned int;
+
+template <int>
+struct S {
+  S() {
+    const Number x = {-1}; // { dg-error "narrowing conversion" }
+    (void)x;
+  }
+};
+
+int main()
+{
+  S<1> s;
+}