public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for c++/78244 - narrowing conversion in template not detected, part 2
@ 2019-01-17 19:09 Marek Polacek
  2019-01-17 21:17 ` Jason Merrill
  0 siblings, 1 reply; 13+ messages in thread
From: Marek Polacek @ 2019-01-17 19:09 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

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  <polacek@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" }

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2019-01-27 20:14 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-17 19:09 C++ PATCH for c++/78244 - narrowing conversion in template not detected, part 2 Marek Polacek
2019-01-17 21:17 ` Jason Merrill
2019-01-18 14:12   ` Marek Polacek
2019-01-21 20:15     ` Jason Merrill
2019-01-22 21:12       ` Marek Polacek
2019-01-23 14:01         ` Jason Merrill
2019-01-23 18:29           ` Marek Polacek
2019-01-23 20:51             ` Jason Merrill
2019-01-25  0:51               ` Marek Polacek
2019-01-25 15:06                 ` Jason Merrill
2019-01-25 21:55                   ` Marek Polacek
2019-01-27  0:25                     ` Jason Merrill
2019-01-27 20:18                       ` Marek Polacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).