This patch ought to fix the rest of 78244, a missing narrowing warning in
decltype.

As I explained in Bugzilla, there can be three scenarios:

1) decltype is in a template and it has no dependent expressions, which
is the problematical case.  finish_compound_literal just returns the
compound literal without checking narrowing if processing_template_decl.
But finish_decltype_type sees an expr that is not instantiation-dependent,
so it just takes its unlowered_expr_type and returns it, without calling
check_narrowing.

2) decltype is in a template and has dependent expressions
This works, because while finish_compound_literal just returns the compound
literal, finish_decltype_type sees an expr that is instantiation-dependent,
so it creates a DECLTYPE_TYPE.
tsubst_copy_and_build then calls
  RETURN (finish_compound_literal (type, r, complain, cl));
while substituting the CONSTRUCTOR.  Now processing_template_decl is 0, so
finish_compound_literal calls check_narrowing, so we detect it.

3) decltype is not in a template
finish_compound_literal calls check_narrowing and the narrowing is detected.


I think it's better not to blithely instantiate such decltypes just for the
benefit of check_narrowing, but we can still try to check narrowing even in a
template.  check_narrowing calls instantiation_dependent_expression_p, so it
does nothing when there are dependent expressions.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2019-01-17  Marek Polacek  <pola...@redhat.com>

        PR c++/78244 - narrowing conversion in template not detected.
        * semantics.c (finish_compound_literal): When processing a template,
        try to check narrowing.

        * g++.dg/cpp0x/Wnarrowing15.C: New test.
        * g++.dg/cpp1y/Wnarrowing1.C: New test.

diff --git gcc/cp/semantics.c gcc/cp/semantics.c
index bc9d53800f7..828f1578697 100644
--- gcc/cp/semantics.c
+++ gcc/cp/semantics.c
@@ -2797,6 +2797,14 @@ finish_compound_literal (tree type, tree 
compound_literal,
 
   if (processing_template_decl)
     {
+      /* If there are no dependent expressions, we can detect narrowing
+        conversions.  */
+      if (SCALAR_TYPE_P (type)
+         && CONSTRUCTOR_NELTS (compound_literal) == 1
+         && !check_narrowing (type,
+                              CONSTRUCTOR_ELT (compound_literal, 0)->value,
+                              complain))
+       return error_mark_node;
       TREE_TYPE (compound_literal) = type;
       /* Mark the expression as a compound literal.  */
       TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
diff --git gcc/testsuite/g++.dg/cpp0x/Wnarrowing15.C 
gcc/testsuite/g++.dg/cpp0x/Wnarrowing15.C
new file mode 100644
index 00000000000..4e7c17dcfca
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/Wnarrowing15.C
@@ -0,0 +1,14 @@
+// PR c++/78244
+// { dg-do compile { target c++11 } }
+
+template <typename T>
+auto f1() -> decltype(int{2.0}, void()) { } // { dg-error "narrowing 
conversion" }
+
+template <typename T>
+auto f2() -> decltype(int{2.0}) { return 1; } // { dg-error "narrowing 
conversion" }
+
+template <typename T>
+auto f3() -> decltype(void(), int{2.0}) { return 1; } // { dg-error "narrowing 
conversion" }
+
+template <typename T>
+auto f4() -> decltype((int{2.0})) { return 1; } // { dg-error "narrowing 
conversion" }
diff --git gcc/testsuite/g++.dg/cpp1y/Wnarrowing1.C 
gcc/testsuite/g++.dg/cpp1y/Wnarrowing1.C
new file mode 100644
index 00000000000..e1e499542f0
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp1y/Wnarrowing1.C
@@ -0,0 +1,5 @@
+// PR c++/78244
+// { dg-do compile { target c++14 } }
+
+template<typename>
+decltype(int{1.1}) v; // { dg-error "narrowing conversion" }

Reply via email to